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

て烟熏妆下的殇ゞ 提交于 2019-12-11 08:26:28

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/54342492/cant-capture-dynamic-environment-in-a-fn-item-about-iron-lib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!