functionrustclosuresiron

Can't capture dynamic environment in a fn item about iron lib


I use the cassandra of the c/c++ driver to query, and then return the data. Therefore, both cass (LinkedList) and cass_it (Vec) can show the result of the query. However, I want to display the results to the web using the json format, so I chose to reassemble the data using vec. However, there is a problem:

error[E0434]: can't capture dynamic environment in a fn item
   --> src/main.rs:306:42
    |
306 |         let out = serde_json::to_string(&cass_it).unwrap();
    |                                          ^^^^^^^
    |
    = help: use the `|| { ... }` closure form instead

What is wrong? Formatting issues?

Code:

fn main() {
    let mut cass = unsafe { cass_connect() };
    let mut cass_it = Vec::new();

    for cc in cass.iter() {
        cass_it.push(cc);
    }

    println!("{:?}", cass_it);

    let mut router = Router::new();
    let localhost = "localhost:3009".to_string();

    fn handler(req: &mut Request) -> IronResult<Response> {
        // convert the response struct to JSON
        let out = serde_json::to_string(&cass_it).unwrap();

        let content_type = "application/json".parse::<Mime>().unwrap();

        Ok(Response::with((content_type, status::Ok, out)))
    }

    router.get("/", handler, "index");

    info!("Listening on {}", localhost);
    Iron::new(router).http(localhost).unwrap();
}

Complete code


Solution

  • The error says it all:

    can't capture dynamic environment in a fn item
    

    The fn item in question is handler. Even though you have defined this function inside another method, a function declared with fn (a fn item) is compiled just like any other function on the top level of a module. Functions can't capture free variables from their environment; they can only access their explicit arguments and static variables.

    The error goes on to say exactly which variable is the problem:

    306 |   let out = serde_json::to_string(&cass_it).unwrap();
        |                                    ^^^^^^^
    

    The variable cass_it is defined in the enclosing function and cannot be accessed from handler.

    The note at the end of the error message then gives you a suggestion for how to fix the problem:

    = help: use the `|| { ... }` closure form instead
    

    A closure can capture variables from its environment. So you can try replacing the fn with a closure instead:

    let handler = move |req: &mut Request| {
        // convert the response struct to JSON
        let out = serde_json::to_string(&cass_it).unwrap();
        let content_type = "application/json".parse::<Mime>().unwrap();
        Ok(Response::with((content_type, status::Ok, out)))
    };
    

    The move keyword will cause the closure to take ownership of cass_it instead of trying to reference the variable in the outer function.