gpt4 book ai didi

rust - 如何返回借用状态以便稍后使用的 Rust 闭包?

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

我有一段相当简单的代码。我有一种感觉,我需要用一生的时间来完成这个任务,但我现在很困惑。

parse_string 是一个函数,它接受对字符串的引用,并返回一个稍后使用的闭包,代码如下:

fn main() {
let parse_this = parse_string(&String::from("Hello!"));
println!("{}", parse_this("goodbye!"));
}

fn parse_string(string: &String) -> impl Fn(&str) -> &String {
return |targetString| {
// pretend there is parsing logic
println!("{}", targetString);
return string;
};
}

编译器错误:

error: cannot infer an appropriate lifetime
--> src/main.rs:7:12
|
6 | fn parse_string(string: &String) -> impl Fn(&str) -> &String {
| ------------------------ this return type evaluates to the `'static` lifetime...
7 | return |targetString| {
| ____________^
8 | | // pretend there is parsing logic
9 | | println!("{}", targetString);
10 | | return string;
11 | | };
| |_____^ ...but this borrow...
|
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 6:1
--> src/main.rs:6:1
|
6 | / fn parse_string(string: &String) -> impl Fn(&str) -> &String {
7 | | return |targetString| {
8 | | // pretend there is parsing logic
9 | | println!("{}", targetString);
10 | | return string;
11 | | };
12 | | }
| |_^
help: you can add a constraint to the return type to make it last less than `'static` and match the anonymous lifetime #1 defined on the function body at 6:1
|
6 | fn parse_string(string: &String) -> impl Fn(&str) -> &String + '_ {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src/main.rs:10:16
|
10 | return string;
| ^^^^^^
|
note: ...the reference is valid for the anonymous lifetime #2 defined on the body at 7:12...
--> src/main.rs:7:12
|
7 | return |targetString| {
| ____________^
8 | | // pretend there is parsing logic
9 | | println!("{}", targetString);
10 | | return string;
11 | | };
| |_____^
note: ...but the borrowed content is only valid for the anonymous lifetime #1 defined on the function body at 6:1
--> src/main.rs:6:1
|
6 | / fn parse_string(string: &String) -> impl Fn(&str) -> &String {
7 | | return |targetString| {
8 | | // pretend there is parsing logic
9 | | println!("{}", targetString);
10 | | return string;
11 | | };
12 | | }
| |_^

最佳答案

您有许多复杂的问题:

  1. 您需要一个显式的生命周期来将string参数的生命周期连接到返回闭包的返回值的生命周期。现在,lifetime elision导致它被推断为与闭包的参数相同。

  2. 您无法通过函数返回对临时对象的引用。它必须是一个不同的变量。

  3. 您必须将 string 移至闭包中,以防止再次引用它,否则它的生存时间不够长。

另外...

  1. targetString 应为 target_string 以遵循 Rust 习惯用法。
  2. return 不应在 block 末尾使用以遵循 Rust 习惯用法。
  3. &str 通常优先于 &String
fn main() {
let s = String::from("Hello!");
let parse_this = parse_string(&s);
println!("{}", parse_this("goodbye!"));
}

fn parse_string<'a>(string: &'a String) -> impl Fn(&str) -> &'a String {
return move |target_string| {
// pretend there is parsing logic
println!("{}", target_string);
string
};
}

另请参阅:

关于rust - 如何返回借用状态以便稍后使用的 Rust 闭包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58207846/

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