3
votes

I'm running this hello world example code from the http://ironframework.io homepage:

extern crate iron;

use iron::prelude::*;
use iron::status;

fn main() {
    fn hello_world(_: &mut Request) -> IronResult<Response> {
        Ok(Response::with((status::Ok, "Hello World!")))
    }

    Iron::new(hello_world).http("localhost:3000").unwrap();
    println!("On 3000");
}

I expected to see "On 3000" appear on standard out, but it never appears. My guess is that the main thread is being blocked before it executes the println. Why is that happening?

If I use a temporary and call unwrap afterwards, I get the expected output:

fn main() {
    fn hello_world(_: &mut Request) -> IronResult<Response> {
        Ok(Response::with((status::Ok, "Hello World!")))
    }

    let result = Iron::new(hello_world).http("localhost:3000");
    println!("On 3000");
    result.unwrap();
}

Why is it that the behavior changes when unwrap is called on the return value?

I'm running rust 1.1.0 and iron 0.1.20.

1

1 Answers

3
votes

The answer occurred to me after I wrote the question.

The http function returns an HttpResult<Listening> The Listening type has a destructor that calls join on a thread, which blocks.

In the first case, the returned object's lifetime is complete after the unwrap call, so the destructor gets called which joins the thread. If I assign it to a variable, the destructor doesn't get invoked until after unwrap is called.