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
2
2
prove that some time has a long enough lifetime.
3
3
4
4
Erroneous code example:
5
5
6
- ``` compile_fail, E0311
6
+ ``` compile_fail,E0311
7
7
use std::borrow::BorrowMut;
8
8
9
9
trait NestedBorrowMut<U, V> {
@@ -13,20 +13,19 @@ trait NestedBorrowMut<U, V> {
13
13
impl<T, U, V> NestedBorrowMut<U, V> for T
14
14
where
15
15
T: BorrowMut<U>,
16
- U: BorrowMut<V>, // missing lifetime specifier here --> compile fail
16
+ U: BorrowMut<V>, // error: missing lifetime specifier
17
17
{
18
18
fn nested_borrow_mut(&mut self) -> &mut V {
19
19
self.borrow_mut().borrow_mut()
20
20
}
21
21
}
22
22
```
23
23
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.
30
29
31
30
Working implementation of the ` NestedBorrowMut ` trait:
32
31
@@ -40,7 +39,7 @@ trait NestedBorrowMut<'a, U, V> {
40
39
impl<'a, T, U, V> NestedBorrowMut<'a, U, V> for T
41
40
where
42
41
T: BorrowMut<U>,
43
- U: BorrowMut<V> + 'a,
42
+ U: BorrowMut<V> + 'a, // Adding lifetime specifier
44
43
{
45
44
fn nested_borrow_mut(&'a mut self) -> &'a mut V {
46
45
self.borrow_mut().borrow_mut()
0 commit comments