'&self' 和 '&'a self' 和有什么不一样? [英] What is the difference between '&self' and '&'a self'?

查看:14
本文介绍了'&self' 和 '&'a self' 和有什么不一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

<小时>

如果我们为该方法引入新的生命周期,根据 经济.

fn get_mut(&mut self) ->&mut T;//省略fn get_mut<'a>(&'a mut self) ->&'a mut T;//展开

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

解决方案

Lifetime 'a in fn foo(&'a self, ...) ... 是为 impl<'a> 定义的,也就是说对于所有的 foo 调用都是一样的.

Lifetime 'a in fn get_mut<'a>(&'a mut self) ... 是为函数定义的.get_mut 的不同调用可以有不同的 'a 值.

您的代码

<块引用>

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

不是省略生命周期的扩展.此代码将借用 &'a self 的生命周期与结构 Foo<'a> 的生命周期联系起来.如果 Foo<'a>'a 是不变的,那么只要 'aself 就应该保持借用.

省略生命周期的正确展开是

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

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

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

使用 std::cell::Cell;struct Variant<'a>(&'a u32);struct Invariant<'a>(Cell<&'a u32>);impl'a>变体a{fn foo(&'a self) ->&'a u32 {自我.0}}impl'a>不变量a{fn foo(&'a self) ->&'a u32 {self.0.get()}}fn 主(){让 val = 0;let mut variant = Variant(&val);//变体:Variant<'long>let mut invariant = Invariant(Cell::new(&val));//invariant: Invariant<'long>{让 r = variant.foo();//解释这里发生了什么的伪代码//let r: &'short u32 = Variant::<'short>::foo(&'short variant);//`variant` 的借用到此结束,因为它是为 `'short` 生命周期借用的//编译器可以做这个转换,因为 `Variant<'long>` 是//Variant<'short>的子类型并且 `&T` 是 `T` 的变体//因此可以将类型为 `Variant<'long>` 的 `variant` 传递给函数//变体::<'short>::foo(&'short Variant<'short>)}//这里没有借用变体变体 = Variant(&val);{让 r = invariant.foo();//编译器不能缩短 `Invariant` 的生命周期//因此 `invariant` 被借用了 `'long` 生命周期}//错误.这里仍然借用了不变式//invariant = Invariant(Cell::new(&val));}

游乐场链接

I recently had an error which was simply resolved by changing

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

to

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

which did not make sense according to my understanding, as I thought that the second version is exactly the same as the first with applied lifetime elision.


In case we introduce a new lifetime for the method this seems to be the case according this example from the nomicon.

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

So what are the differences between this and my first code snipped.

解决方案

Lifetime 'a in fn foo(&'a self, ...) ... is defined for impl<'a>, that is it is the same for all foo calls.

Lifetime 'a in fn get_mut<'a>(&'a mut self) ... is defined for the function. Different calls of get_mut can have different values for 'a.

Your code

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

is not the expansion of elided lifetime. This code ties lifetime of borrow &'a self to the lifetime of structure Foo<'a>. If Foo<'a> is invariant over 'a, then self should remain borrowed as long as 'a.

Correct expansion of elided lifetime is

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

This code doesn't depend on variance of structure Foo to be able to borrow self for shorter lifetimes.

Example of differences between variant and invariant structures.

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

这篇关于'&amp;self' 和 '&amp;'a self' 和有什么不一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆