Search code examples
rustborrow

Why is there a borrow error when no borrowing overlap is occurring?


The following code fails with a borrow error:

extern crate chrono; // 0.4.6

fn main() {
    let mut now = chrono::Local::today();
    now = std::mem::replace(&mut now, now.succ());
}

The error is:

error[E0502]: cannot borrow `now` as immutable because it is also borrowed as mutable
 --> src/lib.rs:5:39
  |
5 |     now = std::mem::replace(&mut now, now.succ());
  |           ----------------- --------  ^^^ immutable borrow occurs here
  |           |                 |
  |           |                 mutable borrow occurs here
  |           mutable borrow later used by call

Why is there a borrow error here? now.succ() returns a new object, and it would look like the succ() call should return the new object, end the immutable borrow before the mutable borrow occurs with replace.


Solution

  • The order of the arguments matters. For example this works:

    /// Same as `std::mem::replace`, but with the reversed parameter order.
    pub fn replace<T>(src: T, dest: &mut T) -> T {
        std::mem::replace(dest, src)
    }
    
    fn main() {
        let mut now = chrono::Local::today();
        now = replace(now.succ(), &mut now);
    }
    

    (link to playground)

    But in your example, &mut now appears first, and when evaluating the second parameter, it is already borrowed.