考虑以下几点:
struct Str<'a> {
s: &'a str,
}
fn f1<'a>(_: &'a mut Str<'a>) {}
fn f2<'a, 'b>(_: &'a mut Str<'b>) {}
fn main() {
let s = "hello".to_string();
let mut a = Str {
s: &s,
};
f1(&mut a);
// f2(&mut a);
let t: &Str = &a;
}
f2
使用两个不同的生命周期,就像我省略它们时一样,效果很好。
此时,我以为生命'a
指的是 &mut a
的生命周期, 和 'b
指的是 &s
的生命周期.
然后我写了f1
它使用单个生命周期参数,怀疑生命周期 'a
将引用 &mut a
的生命周期中较短的一个和 &s
的生命周期.
然而,这 f1
失败并出现以下错误:
error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable
--> src/main.rs:21:19
|
18 | f1(&mut a);
| ------ mutable borrow occurs here
...
21 | let t: &Str = &a;
| ^^
| |
| immutable borrow occurs here
| mutable borrow later used here
这个错误让我很困惑:为什么是a
调用 f1
后仍作为可变借用?
为什么会失败,错误消息的含义是什么?
最佳答案
Why is
a
still borrowed as mutable after callingf1
?
fn main() {
// scope of s ---\
let s = "hello".to_string(); // |
let mut a = Str { // |
s: &s, // |
}; // |
// |
// f1 borrows a until ---\ |
f1(&mut a); // | |
// but it's used here \ | |
let t: &Str = &a; // X // | |
// X X
}
s
的范围一直到main
结束。由于 f1
参数上的生命周期注释,可变引用 &mut a
的生命周期与 s
的范围相关联,这意味着f1
正在为 s
的整个范围借用 a
。
这不是不可变引用的问题,因为较长的生命周期可能会被强制转换为较短的生命周期;换句话说,不可变引用的生命周期是协变的。但是可变 引用的生命周期是不变。这意味着他们不能被迫缩短(或延长)生命周期。
关于rust - 对指针和它指向的结构的生命周期参数使用相同的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61477993/