来自工厂函数的 Rust 闭包

标签 rust

我有一些 Rust 代码正在尝试开始工作,但我不确定如何去做。

fn main() {
    let names = vec!["foo", "bar", "baz"];
    let print = printer(names);
    let result = print();
    println!("{}", result);
    do_other_thing(names.as_slice());
}

fn printer(names: Vec<&str>) -> Box<Fn() -> String> {
    Box::new(move || {
        let text = String::new();
        for name in names {
            text = text + name;
        }
        text
    })
}

fn do_other_thing(names: &[&str]) {}

这编译:

error[E0477]: the type `[closure@src/main.rs:10:14: 16:6 names:std::vec::Vec<&str>]` does not fulfill the required lifetime
  --> src/main.rs:10:5
   |
10 |       Box::new(move || {
   |  _____^ starting here...
11 | |         let text = String::new();
12 | |         for name in names {
13 | |             text = text + name;
14 | |         }
15 | |         text
16 | |     })
   | |______^ ...ending here
   |
   = note: type must outlive the static lifetime

我对发生的事情有一个模糊的想法。看起来闭包有可能比 names 参数更有效。我可以注释为 'static 但感觉不对,即便如此我也不想移动矢量以便 do_other_thing 起作用。我需要以某种方式复制。

最佳答案

错误说 names 必须超过静态生命周期,这是因为装箱的 Fn 有静态生命周期。您有两个选择:

  1. 'static 生命周期添加到names:

    fn printer(names: Vec<&'static str>) -> Box<Fn() -> String>{
        Box::new(move|| {
            // ...
        })
    }
    
  2. 更改盒装 Fn 的生命周期以匹配 names 生命周期:

    fn printer<'a>(names: Vec<&'a str>) -> Box<Fn() -> String + 'a>{
        Box::new(move|| {
            // ...
        })
    }
    

请注意,闭包体需要调整,并且您正在将 names 的所有权授予 printer,因此您不能使用 namesdo_other_thing 中。这是一个固定版本:

fn main() {
    let names = vec!["foo", "bar", "baz"];
    let print = printer(&names);
    let result = print();
    println!("{}", result);
    do_other_thing(names.as_slice());
}

fn printer<'a>(names: &'a Vec<&str>) -> Box<Fn() -> String + 'a>{
    Box::new(move || {
        // this is more idiomatic
        // map transforms &&str to &str
        names.iter().map(|s| *s).collect()
    })
}

关于来自工厂函数的 Rust 闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43661262/

相关文章:

floating-point - 如何获取包含 float 的迭代器的最小值或最大值?

closures - 类型变量中的闭包特征界限与高阶函数参数中的闭包特征界限

multithreading - 如何将函数发送到另一个线程?

pointers - 原始指针没有产生预期的效果

generics - 为什么我不能在带有类型参数的特征上添加一揽子暗示?

rust - 为什么从 Hyper crate 多次导入失败?

pattern-matching - 匹配表达式落空?

python - Poetry install --no-dev 需要安装 Rust 吗?

rust - 生成 rlib 时没有散列也没有版本

rust - 为什么我的变量生命周期不够长?