gpt4 book ai didi

来自工厂函数的 Rust 闭包

转载 作者:行者123 更新时间:2023-11-29 08:16:37 25 4
gpt4 key购买 nike

我有一些 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/38748054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com