rust - 对指针和它指向的结构的生命周期参数使用相同的生命周期

标签 rust lifetime borrowing

考虑以下几点:

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 calling f1?

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/

相关文章:

rust - 如何在最新版本(3.3.2)中实现Actix-web中间件?

rust - 迭代递归结构时无法获取可变引用 : cannot borrow as mutable more than once at a time

rust - 避免同时借用可变的和不变的

rust - 奇怪的错误 : use of partially moved value

windows - 如何编译文档中的示例?

multithreading - “lock”应用程序的UI线程是什么意思?

rust - 使用生命周期参数声明特征对象的向量

Rust 生命周期 - 变量的生命周期不够长错误

rust - 如何返回具有多个生命周期的 impl Iterator?

rust - 当一个对象可能实现多个特征时,如何创建对特征的引用结构?