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

谁都会走 提交于 2019-12-10 17:35:15

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/55922926/why-is-there-a-borrow-error-when-no-borrowing-overlap-is-occurring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!