【发布时间】:2017-04-20 15:19:59
【问题描述】:
我正在尝试 Rust,但在理解“借用”方面遇到了一些问题。
fn main() {
let mut x = 10;
let mut a = 6;
let mut y = &mut x;
*y = 6;
y = &mut a;
x = 15;
println!("{}", x);
}
我有一个错误:
error[E0506]: cannot assign to `x` because it is borrowed
--> <anon>:9:5
|
4 | let mut y = &mut x;
| - borrow of `x` occurs here
...
9 | x = 15;
| ^^^^^^ assignment to borrowed `x` occurs here
error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
--> <anon>:10:20
|
4 | let mut y = &mut x;
| - mutable borrow occurs here
...
10 | println!("{}", x);
| ^ immutable borrow occurs here
11 | }
| - mutable borrow ends here
如何从“y-borrowing”释放x?
【问题讨论】:
-
您只需启动thinking in scopes。
标签: rust borrow-checker