无法在关于铁库的fn项目中捕获动态环境 [英] Can't capture dynamic environment in a fn item about iron lib

查看:106
本文介绍了无法在关于铁库的fn项目中捕获动态环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用c / c ++驱动程序的cassandra进行查询,然后返回数据。
因此,cass(LinkedList)和cass_it(Vec)都可以显示查询结果。
但是,我想使用json格式将结果显示到Web上,所以我选择使用vec重新组装数据。但是,存在一个问题:

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

怎么了?

代码:

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();
}

完整代码

推荐答案

错误说明了一切:


can't capture dynamic environment in a fn item


其中的 fn 项目问题是处理程序。即使您已经在另一个方法中定义了此函数,使用 fn fn 项)声明的函数也只是编译就像模块顶层的任何其他功能一样。函数无法从其环境中捕获自由变量;他们只能访问它们的显式参数和静态变量。

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();
    |                                    ^^^^^^^


变量 cass_it 是在封闭函数中定义的,不能从 handler 访问。

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


闭包可以从其环境中捕获变量。因此,您可以尝试使用闭包代替 fn

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)))
};

move 关键字将导致关闭取得 cass_it 的所有权,而不是尝试在外部函数中引用该变量。

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

这篇关于无法在关于铁库的fn项目中捕获动态环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆