是否可以明确指定循环迭代的生命周期?
Is it possible to specify the lifetime of an iteration of a loop explicitly?
以下代码利用生命周期省略进行编译:
fn g(a: &mut str, closure: impl Fn(&mut str, &str) -> ()) {
let data = vec![];
for d in data.iter() {
closure(a, d);
}
}
但是,假设我需要在闭包中明确指定变量的生命周期(在我的例子中,除非我这样做,否则调用点是不明确的)。如果我显式添加生命周期,编译器会抱怨 a
在之前的闭包迭代中被可变借用。
fn g<'b, 'd>(a: &'b mut str, closure: impl Fn(&'d mut str, &str) -> ()) {
let data: Vec<&str> = vec![];
'd: for d in data.iter() {
closure(a, d);
}
}
warning: label name `'d` shadows a lifetime name that is already in scope
--> src/lib.rs:3:5
|
1 | fn g<'b, 'd>(a: &'b mut str, closure: impl Fn(&'d mut str, &str) -> ()) {
| -- first declared here
2 | let data: Vec<&str> = vec![];
3 | 'd: for d in data.iter() {
| ^^ lifetime 'd already in scope
error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src/lib.rs:4:17
|
4 | closure(a, d);
| ^
|
note: ...the reference is valid for the lifetime 'd as defined on the function body at 1:10...
--> src/lib.rs:1:10
|
1 | fn g<'b, 'd>(a: &'b mut str, closure: impl Fn(&'d mut str, &str) -> ()) {
| ^^
note: ...but the borrowed content is only valid for the lifetime 'b as defined on the function body at 1:6
--> src/lib.rs:1:6
|
1 | fn g<'b, 'd>(a: &'b mut str, closure: impl Fn(&'d mut str, &str) -> ()) {
| ^^
我认为那是因为编译器不会猜测我只打算 'd
在循环的每次迭代中持续。我搜索了一种引用生命周期的方法,但我所能找到的只是我有能力在 Rust 参考书中指定一个 "lifetime or label" ,这似乎只是所有意图和目的的标签。
有没有什么方法可以显式指定每个循环迭代的生命周期,或者显式写出编译器在第一个示例中省略的内容?
Is it possible to specify the lifetime of an iteration of a loop explicitly?
没有。这种东西根本不需要。
您没有提供与您的原始问题相对应的 MCVE,因此不可能提供解决该问题的解决方案。我的直觉和你的评论 "write out what the compiler has elided" 告诉我你应该看看:
长话短说:
fn g(a: &mut str, closure: impl for<'a, 'd> Fn(&'a mut str, &'d str) -> ())
以下代码利用生命周期省略进行编译:
fn g(a: &mut str, closure: impl Fn(&mut str, &str) -> ()) {
let data = vec![];
for d in data.iter() {
closure(a, d);
}
}
但是,假设我需要在闭包中明确指定变量的生命周期(在我的例子中,除非我这样做,否则调用点是不明确的)。如果我显式添加生命周期,编译器会抱怨 a
在之前的闭包迭代中被可变借用。
fn g<'b, 'd>(a: &'b mut str, closure: impl Fn(&'d mut str, &str) -> ()) {
let data: Vec<&str> = vec![];
'd: for d in data.iter() {
closure(a, d);
}
}
warning: label name `'d` shadows a lifetime name that is already in scope
--> src/lib.rs:3:5
|
1 | fn g<'b, 'd>(a: &'b mut str, closure: impl Fn(&'d mut str, &str) -> ()) {
| -- first declared here
2 | let data: Vec<&str> = vec![];
3 | 'd: for d in data.iter() {
| ^^ lifetime 'd already in scope
error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src/lib.rs:4:17
|
4 | closure(a, d);
| ^
|
note: ...the reference is valid for the lifetime 'd as defined on the function body at 1:10...
--> src/lib.rs:1:10
|
1 | fn g<'b, 'd>(a: &'b mut str, closure: impl Fn(&'d mut str, &str) -> ()) {
| ^^
note: ...but the borrowed content is only valid for the lifetime 'b as defined on the function body at 1:6
--> src/lib.rs:1:6
|
1 | fn g<'b, 'd>(a: &'b mut str, closure: impl Fn(&'d mut str, &str) -> ()) {
| ^^
我认为那是因为编译器不会猜测我只打算 'd
在循环的每次迭代中持续。我搜索了一种引用生命周期的方法,但我所能找到的只是我有能力在 Rust 参考书中指定一个 "lifetime or label" ,这似乎只是所有意图和目的的标签。
有没有什么方法可以显式指定每个循环迭代的生命周期,或者显式写出编译器在第一个示例中省略的内容?
Is it possible to specify the lifetime of an iteration of a loop explicitly?
没有。这种东西根本不需要。
您没有提供与您的原始问题相对应的 MCVE,因此不可能提供解决该问题的解决方案。我的直觉和你的评论 "write out what the compiler has elided" 告诉我你应该看看:
长话短说:
fn g(a: &mut str, closure: impl for<'a, 'd> Fn(&'a mut str, &'d str) -> ())