The following program is not allowed due to a violation of the borrow checking rules:
struct Foo;
impl Foo {
fn bar(&mut self, i: u8) { }
fn baz(&mut self) -> u8 { 0 }
}
fn main() {
let mut foo = Foo;
foo.bar(foo.baz())
// --------^^^^^^^^^-
// | | |
// | | second mutable borrow occurs here
// | first borrow later used by call
// first mutable borrow occurs here
}
But you can work around this by binding to a temporary variable, like:
fn main() {
let mut foo = Foo;
let t = foo.baz();
foo.bar(t);
}
Why does the borrow checker start at the outer most function call? Shouldn't it be the other way around?