gpt4 book ai didi

rust - 链接 self 的生命周期和方法中的引用

转载 作者:行者123 更新时间:2023-11-29 07:46:58 25 4
gpt4 key购买 nike

我有this piece of code :

#[derive(Debug)]
struct Foo<'a> {
x: &'a i32,
}

impl<'a> Foo<'a> {
fn set(&mut self, r: &'a i32) {
self.x = r;
}
}

fn main() {
let v = 5;
let w = 7;
let mut f = Foo { x: &v };

println!("f is {:?}", f);

f.set(&w);

println!("now f is {:?}", f);
}

我的理解是,在第一次借用v的值时,struct声明上的泛型生命周期参数'a填入了该值的生命周期v。这意味着生成的 Foo 对象的生命周期不得超过此 'a 的生命周期,或者 v 的值必须至少与此生命周期一样长Foo 对象。

在方法set的调用中,使用了impl block 上的生命周期参数,w的值的生命周期为在方法签名中填写 for 'a&mut self 被编译器分配了不同的生命周期,即 f(Foo 对象)的生命周期。如果我在 main 函数中切换 wf 的绑定(bind)顺序,这将导致错误。

我想知道如果我在 中使用与 r 相同的生命周期参数 'a 注释 &mut self 引用会发生什么设置方法:

impl<'a> Foo<'a> {
fn set(&'a mut self, r: &'a i32) {
self.x = r;
}
}

这会导致以下错误:

error[E0502]: cannot borrow `f` as immutable because it is also borrowed as mutable
--> src/main.rs:21:31
|
19 | f.set(&w);
| - mutable borrow occurs here
20 |
21 | println!("now f is {:?}", f);
| ^ immutable borrow occurs here
22 | }
| - mutable borrow ends here

与上面的例子相反,f 在第二个 println! 时仍然被认为是可变借用的。被调用,所以它不能作为不可变的同时被借用。

这是怎么发生的?

通过不遗漏生命周期注释,编译器为我在第一个示例中为 &mut self 填充了一个。这是根据生命周期省略规则发生的。但是,在第二个示例中,通过将其显式设置为 'a,我将 f 的值和 w 的值的生命周期联系起来。

f 是否以某种方式被认为是借用的?

如果是这样,借用的范围是什么?是 min(lifetime of f, lifetime of w) -> lifetime of f?

我假设我还没有完全理解函数调用中的 &mut self 引用。我的意思是,函数返回了,但是 f 仍然被认为是借用的。

我正试图完全理解生命周期。我主要是在寻找有关我对这些概念的理解的纠正反馈。我非常感谢您的每一个建议和进一步的说明。

最佳答案

In the call to the method set the lifetime parameter on the impl block is used and the lifetime of the value of w is filled in for 'a in the method signature.

没有。生命周期参数 'a 的值在 Foo 结构的创建时固定,并且永远不会改变,因为它是其类型的一部分。

在您的情况下,编译器实际上为 'a 选择了一个与 vw 的生命周期兼容的值。如果那不可能,它将失败,例如在这个例子中:

fn main() {
let v = 5;
let mut f = Foo { x: &v };

println!("f is {:?}", f);
let w = 7;
f.set(&w);

println!("now f is {:?}", f);
}

哪些输出:

error[E0597]: `w` does not live long enough
--> src/main.rs:21:1
|
18 | f.set(&w);
| - borrow occurs here
...
21 | }
| ^ `w` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

正是因为 v 强加的 'a 生命周期与 w 的较短生命周期不兼容。

在第二个示例中,通过强制 self 的生命周期也为 'a,您将可变借用绑定(bind)到生命周期 'a 也是如此,因此当生命周期 'a 的所有项目都超出范围时,借用结束,即 vw

关于rust - 链接 self 的生命周期和方法中的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30273850/

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