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
.