Ich versuche Rust und habe ein paar Probleme mit dem Verständnis von "Borrowing".Ausleihe in Rust freigeben
fn main() {
let mut x = 10;
let mut a = 6;
let mut y = &mut x;
*y = 6;
y = &mut a;
x = 15;
println!("{}", x);
}
Und ich habe einen Fehler bekam:
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
Wie kann ich loslassen x
von "y
-borrowing"?
Sie müssen nur beginnen [in Bereichen denken] (https://doc.rust-lang.org/stable/book/references-and-borrowing.html#thinking-in-scopes). –