borrow

What happens on the stack when one value shadows another in Rust? [duplicate]

这一生的挚爱 提交于 2020-02-03 10:36:50
问题 This question already has answers here : Does Rust free up the memory of overwritten variables? (2 answers) In Rust, what's the difference between “shadowing” and “mutability”? (1 answer) Closed 8 months ago . I am reading Mastering Rust. There is an exercise at the end of the first chapter where sample code is provided, and the task is to fix it, iterating by using the generally quite helpful compiler error messages. I was expecting that the following was an error but it is not : for line in

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

Rust error: borrow occurs after drop a mutable borrow [duplicate]

拟墨画扇 提交于 2019-11-28 11:30:59
问题 This question already has answers here : What are non-lexical lifetimes? (1 answer) Moved variable still borrowing after calling `drop`? (2 answers) What are the options to end a mutable borrow in Rust? (1 answer) Closed 8 months ago . My test code: let mut c = 0; let mut inc = || { c += 1; c }; drop(inc); println!("{}", c); rustc says: error[E0502]: cannot borrow `c` as immutable because it is also borrowed as mutable --> .\src\closure.rs:20:24 | 12 | let mut inc = || { c += 1; c }; | -- ---