1- E0311 occurs when there is insufficient information for the rust compiler to
1+ This error occurs when there is insufficient information for the rust compiler to
22prove that some time has a long enough lifetime.
33
44Erroneous code example:
55
6- ``` compile_fail, E0311
6+ ``` compile_fail,E0311
77use std::borrow::BorrowMut;
88
99trait NestedBorrowMut<U, V> {
@@ -13,20 +13,19 @@ trait NestedBorrowMut<U, V> {
1313impl<T, U, V> NestedBorrowMut<U, V> for T
1414where
1515 T: BorrowMut<U>,
16- U: BorrowMut<V>, // missing lifetime specifier here --> compile fail
16+ U: BorrowMut<V>, // error: missing lifetime specifier
1717{
1818 fn nested_borrow_mut(&mut self) -> &mut V {
1919 self.borrow_mut().borrow_mut()
2020 }
2121}
2222```
2323
24- In this example we have a trait that borrows some inner data element of type V
25- from an outer type T, through an intermediate type U. The compiler is unable to
26- prove that the livetime of U is long enough to support the reference, so it
27- throws E0311. To fix the issue we can explicitly add lifetime specifiers to the
28- trait, which link the lifetimes of the various data types and allow the code
29- to compile.
24+ In this example we have a trait that borrows some inner data element of type ` V `
25+ from an outer type ` T ` , through an intermediate type ` U ` . The compiler is unable to
26+ prove that the livetime of ` U ` is long enough to support the reference. To fix the
27+ issue we can explicitly add lifetime specifiers to the ` NestedBorrowMut ` trait, which
28+ link the lifetimes of the various data types and allow the code to compile.
3029
3130Working implementation of the ` NestedBorrowMut ` trait:
3231
@@ -40,7 +39,7 @@ trait NestedBorrowMut<'a, U, V> {
4039impl<'a, T, U, V> NestedBorrowMut<'a, U, V> for T
4140where
4241 T: BorrowMut<U>,
43- U: BorrowMut<V> + 'a,
42+ U: BorrowMut<V> + 'a, // Adding lifetime specifier
4443{
4544 fn nested_borrow_mut(&'a mut self) -> &'a mut V {
4645 self.borrow_mut().borrow_mut()
0 commit comments