3 ms·
The borrow checker does allow that, though, as long the uses don't interleave and the references created correctly. As long as `f` is not used between `a`'s cre
by Measter 4y ago
The borrow checker does allow that, though, as long the uses don't interleave and the references created correctly. As long as `f` is not used between `a`'s creation and last use, and `a` comes from `f`, it's valid for that alias to exist. You can see that with this code example, which is accepted:
#[derive(Debug)]
pub struct Foo {
a: i32
}
impl Foo {
fn get_a(&mut self) -> &mut i32 {
&mut self.a
}
}
pub fn thing(mut foo: &mut Foo) {
let f = &mut foo;
let a = f.get_a();
if *a > 5 {
println!("{:?}", f);
}
}
That's why I was so surprised the compiler rejected it.