I came across something that I just can't figure out :
I get that this code is rejected by the compiler
trait Borrow {
fn borrow_mut(&mut self) -> &i32;
fn borrow_ref(&self) -> &i32;
}
impl Borrow for i32 {
fn borrow_mut(&mut self) -> &i32 {
self
}
fn borrow_ref(&self) -> &i32 {
self
}
}
fn main() {
let mut x: i32 = 5;
let y1 = x.borrow_mut();
let y2 = x.borrow_ref();
println!("{} {}", y1, y2);
}
But should it be ? Since borrow_mut only returns an immutable reference, it seems like we should be able to borrow x again after it has executed...
More generally, if we have a function of type fn<'a>(x: &'a mut Type1) -> Type2<'a>, where all 'a references in Type2 are immutable, then is it possible to mutate x through Type2 ?