struct - '&self' 和 '&' a self' 有什么区别?

标签 struct rust lifetime

我最近遇到一个错误,只需更改即可解决

impl<'a> Foo<'a> {
    fn foo(&'a self, path: &str) -> Boo<'a> { /* */ }
}

impl<'a> Foo<'a> {
    fn foo(&self, path: &str) -> Boo { /* */ }
}

根据我的理解,这没有意义,因为我认为第二个版本与应用生命周期省略的第一个版本完全相同。


如果我们为该方法引入一个新的生命周期,根据 nomicon 中的这个例子,情况似乎就是这样。 .

fn get_mut(&mut self) -> &mut T;                        // elided
fn get_mut<'a>(&'a mut self) -> &'a mut T;              // expanded

那么这和我截取的第一个代码有什么区别。

最佳答案

生命周期 'afn foo(&'a self, ...) ...impl<'a> 定义, 即所有 foo 都是一样的电话。

生命周期 'afn get_mut<'a>(&'a mut self) ...是为函数定义的。 get_mut的不同调用'a 可以有不同的值.

你的代码

impl<'a> Foo<'a> {
    fn foo(&'a self, path: &str) -> Boo<'a> { /* */ }
}

不是省略生命周期的扩展。此代码绑定(bind)借用的生命周期 &'a self到结构的生命周期Foo<'a> .如果Foo<'a>'a 上不变, 然后 self只要 'a 就应该保持借用状态.

elided lifetime 的正确扩展是

impl<'a> Foo<'a> {
    fn foo<'b>(&'b self, path: &str) -> Boo<'b> { /* */ }
}

此代码不依赖于结构的变化 Foo能够借self缩短使用生命周期。

变体结构和不变结构之间的差异示例。

use std::cell::Cell;

struct Variant<'a>(&'a u32);

struct Invariant<'a>(Cell<&'a u32>);

impl<'a> Variant<'a> {
    fn foo(&'a self) -> &'a u32 {
        self.0
    }
}

impl<'a> Invariant<'a> {
    fn foo(&'a self) -> &'a u32 {
        self.0.get()
    }
}

fn main() {
    let val = 0;
    let mut variant = Variant(&val);// variant: Variant<'long>
    let mut invariant = Invariant(Cell::new(&val));// invariant: Invariant<'long>
    {
        let r = variant.foo();
        // Pseudocode to explain what happens here
        // let r: &'short u32 = Variant::<'short>::foo(&'short variant);
        // Borrow of `variant` ends here, as it was borrowed for `'short` lifetime

        // Compiler can do this conversion, because `Variant<'long>` is
        // subtype of Variant<'short> and `&T` is variant over `T`
        // thus `variant` of type `Variant<'long>` can be passed into the function 
        // Variant::<'short>::foo(&'short Variant<'short>)
    }
    // variant is not borrowed here
    variant = Variant(&val);

    {
        let r = invariant.foo();
        // compiler can't shorten lifetime of `Invariant`
        // thus `invariant` is borrowed for `'long` lifetime
    }
    // Error. invariant is still borrowed here
    //invariant = Invariant(Cell::new(&val));
}

Playground link

关于struct - '&self' 和 '&' a self' 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45898588/

相关文章:

c - mq_receive中的奇怪输出与Linux中的队列

ios - 如何使用字典中分组结构的数据填充 UITableView?

rust - `xargo build` 找不到库

rust - 是否有从 Rust 的 Ordering::* 到整数的内置转换?

rust - 有没有办法将 "check and set"的值设为 `std::cell::Cell` ?

C++ 虚拟继承 : static_cast "this" to virtual parent in initializer list of derived

c - 取消引用指针 C 时类型不完整

c - 遍历fread语句

c++ - 使用 const 引用延长临时对象的生命周期

c++ - 可以延长初始化列表中对象的生命周期吗?