> rustc --explain E0106This error indicates that a lifetime is missing from a type.If it is an errorinside a function signature, the problem may be with failing to adhere to thelifetime elision rules (see below).Here are some simple examples of where you'll run into this error:structFoo { x:&bool } // errorstructFoo<'a> { x:&'abool } // correctenumBar { A(u8), B(&bool), } // errorenumBar<'a> { A(u8), B(&'abool), } // correcttypeMyStr=&str; // errortypeMyStr<'a> =&'astr; //correct...
这个,呃... ... 这并没有真正说清楚(这些文件认为我们现在更了解 Rust)。但是看起来我们应该把 'a 加到我们的结构中?我们来试试。
// Iter is generic over *some* lifetime, it doesn't carepubstructIter<'a, T> { next:Option<&'aNode<T>>,}// No lifetime here, List doesn't have any associated lifetimesimpl<T> List<T> {// We declare a fresh lifetime here for the *exact* borrow that// creates the iter. Now &self needs to be valid as long as the// Iter is around.pubfniter<'a>(&'a self) ->Iter<'a, T> {Iter { next: self.head.map(|node|&node) } }}// We *do* have a lifetime here, because Iter has one that we need to defineimpl<'a, T> IteratorforIter<'a, T> {// Need it here too, this is a type declarationtypeItem=&'aT;// None of this needs to change, handled by the above.// Self continues to be incredibly hype and amazingfnnext(&mut self) ->Option<Self::Item> { self.next.map(|node| { self.next = node.next.map(|node|&node);&node.elem }) }}
好了,我想这次我们成功了。
cargo builderror[E0308]: mismatched types--> src/second.rs:77:22|77|Iter { next: self.head.map(|node|&node) }|^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `second::Node`, found struct `std::boxed::Box`|= note: expected type `std::option::Option<&second::Node<T>>` found type `std::option::Option<&std::boxed::Box<second::Node<T>>>`error[E0308]: mismatched types--> src/second.rs:85:25|85| self.next = node.next.map(|node|&node);|^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `second::Node`, found struct `std::boxed::Box`|= note: expected type `std::option::Option<&'a second::Node<T>>` found type `std::option::Option<&std::boxed::Box<second::Node<T>>>`
cargo buildCompiling lists v0.1.0 (/Users/ABeingessner/dev/temp/lists)error[E0515]: cannot return reference to local data `*node`--> src/second.rs:77:43|77|Iter { next: self.head.map(|node|&*node) }|^^^^^^ returns a reference to data owned by the current functionerror[E0507]: cannot move out of borrowed content--> src/second.rs:77:22|77|Iter { next: self.head.map(|node|&*node) }|^^^^^^^^^ cannot move out of borrowed contenterror[E0515]: cannot return reference to local data `*node`--> src/second.rs:85:46|85| self.next = node.next.map(|node|&*node);|^^^^^^ returns a reference to data owned by the current functionerror[E0507]: cannot move out of borrowed content--> src/second.rs:85:25|85| self.next = node.next.map(|node|&*node);|^^^^^^^^^ cannot move out of borrowed content