This code does not compile due to conflicting requirements for a lifetime parameter.
struct A<'a> {
i: &'a mut i32,
}
struct B<'b> {
a: &'b mut A<'b>,
}
let mut j = 42;
let mut a = A {
i: &mut j,
};
fn f(a: &mut A) {
let mut b = B {
a: &mut a,
};
g(&mut b);
}
fn g(b: &mut B) {
println!("{}", *b.a.i);
}
This is the resulting error message.
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements
--> src/main.rs:67:21
|
67 | let mut b = B {
| ^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 66:20...
--> src/main.rs:66:21
|
66 | fn f(a: &mut A) {
| _____________________^ starting here...
67 | | let mut b = B {
68 | | a: &mut a,
69 | | };
70 | |
71 | | g(&mut b);
72 | | }
| |_____^ ...ending here
note: ...so that reference does not outlive borrowed content
--> src/main.rs:68:16
|
68 | a: &mut a,
| ^^^^^^
note: but, the lifetime must be valid for the anonymous lifetime #2 defined on the body at 66:20...
--> src/main.rs:66:21
|
66 | fn f(a: &mut A) {
| _____________________^ starting here...
67 | | let mut b = B {
68 | | a: &mut a,
69 | | };
70 | |
71 | | g(&mut b);
72 | | }
| |_____^ ...ending here
note: ...so that expression is assignable (expected &mut main::A<'_>, found &mut main::A<'_>)
--> src/main.rs:68:16
|
68 | a: &mut a,
| ^^^^^^
Can someone explain what the probem is? The error message does not make sense to me.