This is not the compiler being restrictive,
when you declare
struct B<'a> {
b: Option<&'a B<'a>>,
}
you are telling the compiler that B.b is going to live as long as B
and what you want to do is tell the compiler that b has a lifetime of b and B as a lifetime of a.
#[derive(Debug)]
struct Foo {
b: i32,
}
struct Wrap<'a, 'b>{
a: Option<&'a Foo>,
b: Option<&'b Foo>,
}
fn main() {
let mut w = Wrap {
a: None,
b: None,
};
w.b = Some(&Foo{b:0});
{
w.a = Some(&Foo{b: 1});
w.a.take();
}
println!("b = {:?}", w.b);
}
I don't know if this code will help but TLDR you have to specify a second lifetime.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…