Why does this code (mut borrowing) not compile

  1. Consider the folloiwng code:

pub struct Foo {}

impl Foo {

    pub fn a(&mut self) -> i32 { panic!() }
    pub fn b(&mut self) -> i32 { panic!() }
    pub fn c(&mut self, x: i32, y: i32) -> i32 { panic!() }

}


pub fn test() {
    let mut foo = Foo {};
    {
        let a = foo.a();
        let b = foo.b();
        let c = foo.c(a, b);
    }

    /*
    {
        foo.c( foo.a(), foo.b() );
    }
    */


}

The commented out code does NOT compile. Why? In the above, if we explicitly call foo.a(), foo.b() and assign them to vars, everything works fine, so why does the commented out code have a "mut borrow" conflict?

1 Like

@vitalyd : Thanks for the link! I was not aware this issue was that complex.

In light of all the things that can go wrong with automatic desugaring, I'm suddenly okay with explicitly defining temporaries to make the ordering explicit.