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

拟墨画扇 提交于 2019-11-28 11:30:59

问题


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 };
   |                       --   ----- previous borrow occurs due to use of `c` in closure
   |                       |
   |                       mutable borrow occurs here
...
20 |         println!("{}", c);
   |                        ^^^^^ immutable borrow occurs here
21 |     }
   |     - mutable borrow ends here

But inc is dropped manually before println! borrow c, isn't it?

So what's the problem with my code? Please help.


回答1:


Your understanding of how scopes and lifetimes work is correct. In Rust Edition 2018, they enabled non-lexical lifetimes by default. Prior to that, the lifetime of inc would have been to the end of the current lexical scope (i.e. the end of the block) even though its value was moved before that.

If you can use Rust version 1.31 or later, then just specify the edition in your Cargo.toml:

[package]
edition = "2018"

If you are using the latest Rust, this will be put there automatically when you create a new crate with cargo new.


If you are not using Cargo, rustc defaults to Edition 2015, so you need to explicitly provide the edition:

rustc --edition 2018 main.rs

If you are using an older nightly build of Rust for some reason, then you can enable non-lexical lifetimes by adding this in your main file:

#![feature(nll)]

If you are stuck on an older release build, you can usually fix these errors by introducing a shorter scope, using a block like this:

let mut c = 0;
{
    let mut inc = || { c += 1; c };
    drop(inc);
    // scope of inc ends here
}
println!("{}", c);


来源:https://stackoverflow.com/questions/55285018/rust-error-borrow-occurs-after-drop-a-mutable-borrow

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