Rust-由于需求冲突,无法为autoref推断合适的寿命

时间:2019-05-10 15:41:18

标签: rust

我对Rust的一生的概念是陌生的,无法将自己的头缠在特定的代码上。我一直在使用Rust的SDL绑定及其Texture / TextureCreator类,问题基本上可以归结为以下方案:

struct Creator;

impl Creator {
    fn create(&self) -> Creature {
        Creature { creator: &self }
    }
}

struct Creature<'a> {
    creator: &'a Creator,
}

struct MyStruct<'a> {
    creator: Creator,
    creatures: Vec<Creature<'a>>,
}

impl<'a> MyStruct<'a> {
    fn new() -> MyStruct<'a> {
        MyStruct {
            creatures: Vec::new(),
            creator: Creator,
        }
    }

    fn create(&mut self) {
        let new_creature = self.creator.create();
        self.creatures.push(new_creature);
    }
}

fn main() {
    let mut my_struct = MyStruct::new();
    my_struct.create();
}

实际问题出在MyStruct::create方法内部。如果我尝试编译以上代码,则会得到:

error[E0495]: cannot infer an appropriate lifetime for autoref due to con
flicting requirements
  --> src/main.rs:27:41
   |
27 |         let new_creature = self.creator.create();
   |                                         ^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 define
d on the method body at 26:5...
  --> src/main.rs:26:5
   |
26 | /     fn create(&mut self) {
27 | |         let new_creature = self.creator.create();
28 | |         self.creatures.push(new_creature);
29 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:27:28
   |
27 |         let new_creature = self.creator.create();
   |                            ^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on t
he impl at 18:6...
  --> src/main.rs:18:6
   |
18 | impl<'a> MyStruct<'a> {
   |      ^^
   = note: ...so that the expression is assignable:
           expected Creature<'a>
              found Creature<'_>

有趣的是,当我在MyStruct::create内注释此行时:

self.creatures.push(new_creature);

它可以编译。

我的假设是,它可能导致以下问题-新创建的new_creature引用了creator拥有的my_struct。现在,如果new_creaturecreatures向量所有,那么我无法真正在my_struct上调用借用mut的任何方法,因为到那时是:

  • creator内对my_struct的引用
  • 在方法主体内对my_struct的可变引用(因此对my_struct.creator的引用)

但是,从我的角度来看,这就是我的看法。从编译器的角度来看,这是关于生存期的,并不是很明白。所以我有以下两个问题:

  1. 为什么在这种情况下编译器会抱怨生存期?
  2. 如何将Vec<Creature>存储在结构中?我是否应该确保不要将CreatorVec<Creature>保留在同一结构中,以避免出现上述问题?

1 个答案:

答案 0 :(得分:0)

编译器试图告诉您,它期望<nav class="top"> <ul> <li> <a href="">Item</a> <div class="sub-content"> <div> <ul> <li><a href="">Sub Item</a></li> <li><a href="">Sub Item</a></li> </ul> </div> </div> </li> </ul> </nav> <main> <nav class="right"> <ul> <li><a href="">Abc</a></li> <li><a href="">Abc</a></li> <li><a href="">Abc</a></li> <li><a href="">Abc</a></li> <li><a href="">Abc</a></li> <li><a href="">Abc</a></li> <li><a href="">Abc</a></li> <li><a href="">Abc</a></li> <li><a href="">Abc</a></li> <li><a href="">Abc</a></li> </ul> </nav> <div class="content"> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> <p>Dummy Text</p> </div> </main>中有非常特定的生存期,但是代码实际上产生了通用生存期Creature<'a>。也就是说,Creature<'_>上的生命周期绑定必须为Creature,但是您正在为{em> any 生命周期'a编写方法create()

'_更改为

create()

它将编译:您现在拥有的“ fn create(&'a mut self) { let new_creature = self.creator.create(); self.creatures.push(new_creature); } 必须具有生命周期“ a”,而不是“任何生命周期&mut self都没有”。