生命周期的限制(Limits of Lifetimes)

给出以下代码:

  1. #[derive(Debug)]
  2. struct Foo;
  3. impl Foo {
  4. fn mutate_and_share(&mut self) -> &Self { &*self }
  5. fn share(&self) {}
  6. println!("{:?}", loan);
  7. }
  8. fn main() {
  9. let mut foo = Foo;
  10. let loan = foo.mutate_and_share();
  11. foo.share();
  12. }

人们可能期望它能够编译.我们调用mutate_and_share,它暂时地可变借用foo,但后来只返回一个共享引用.因此我们希望foo.share()成功,因为foo不应该被可变地借用.

但是当我们尝试编译它时:

  1. error[E0502]: cannot borrow `foo` as immutable because it is also borrowed as mutable
  2. --> src/main.rs:12:5
  3. |
  4. 11 | let loan = foo.mutate_and_share();
  5. | --- mutable borrow occurs here
  6. 12 | foo.share();
  7. | ^^^ immutable borrow occurs here
  8. 13 | println!("{:?}", loan);

发生了什么?好吧,我们得到了与上一节中的示例2完全相同的推理.我们脱糖该程序,得到以下结果:

  1. struct Foo;
  2. impl Foo {
  3. fn mutate_and_share<'a>(&'a mut self) -> &'a Self { &'a *self }
  4. fn share<'a>(&'a self) {}
  5. }
  6. fn main() {
  7. 'b: {
  8. let mut foo: Foo = Foo;
  9. 'c: {
  10. let loan: &'c Foo = Foo::mutate_and_share::<'c>(&'c mut foo);
  11. 'd: {
  12. Foo::share::<'d>(&'d foo);
  13. }
  14. println!("{:?}", loan);
  15. }
  16. }
  17. }

由于loan的生命周期和mutate_and_share的签名,生命周期系统被迫将&mut foo扩展为以具有生命周期'c.然后,当我们试图调用share时,它看到我们正试图别名那个&'c mut foo,我们就失败了.

根据我们实际关注的引用语义,这个程序显然是正确的,但是生命周期系统太粗粒度,无法处理.

不当减少借用(Improperly reduced borrows)

下面的代码无法编译,因为Rust不知道不再需要借用,因此保守地退回到使用整个作用域. 这最终将得到解决.

  1. # use std::collections::HashMap;
  2. # use std::hash::Hash;
  3. fn get_default<'m, K, V>(map: &'m mut HashMap<K, V>, key: K) -> &'m mut V
  4. where
  5. K: Clone + Eq + Hash,
  6. V: Default,
  7. {
  8. match map.get_mut(&key) {
  9. Some(value) => value,
  10. None => {
  11. map.insert(key.clone(), V::default());
  12. map.get_mut(&key).unwrap()
  13. }
  14. }
  15. }

由于强加的生命周期限制,&mut map 的生命周期与其他可变借用重叠,导致编译错误:

  1. error[E0499]: cannot borrow `*map` as mutable more than once at a time
  2. --> src/main.rs:12:13
  3. |
  4. 4 | fn get_default<'m, K, V>(map: &'m mut HashMap<K, V>, key: K) -> &'m mut V
  5. | -- lifetime `'m` defined here
  6. ...
  7. 9 | match map.get_mut(&key) {
  8. | - --- first mutable borrow occurs here
  9. | _____|
  10. | |
  11. 10 | | Some(value) => value,
  12. 11 | | None => {
  13. 12 | | map.insert(key.clone(), V::default());
  14. | | ^^^ second mutable borrow occurs here
  15. 13 | | map.get_mut(&key).unwrap()
  16. 14 | | }
  17. 15 | | }
  18. | |_____- returning this value requires that `*map` is borrowed for `'m`