// error[E0502]: cannot borrow `a` as mutable because it is also borrowed as immutable
// --> src/main.rs:13:17
// |
// 12 | let mut a_mut = &a;
// | -- immutable borrow occurs here
// 13 | let a_st = &mut a;
// | ^^^^^^ mutable borrow occurs here
// 14 | println!("a_mut {}", a_mut);
// | ----- immutable borrow later used here
fn main() {
let mut a = 123;
let mut a_mut = &a;
let a_st = &mut a;
println!("a_mut {}", a_mut);
println!("a_st {}", a_st);
}
// 而下面的交换输出顺序,则运行正确
// println!("a_mut {}", a_mut);使用后 a_mut 已经结束,则不影响a_st的使用。即作⽤域不能交叠。
fn main() {
let mut a = 123;
let mut a_mut = &a;
println!("a_mut {}", a_mut);
let a_st = &mut a;
println!("a_st {}", a_st);
}
同⼀个所有权变量的可变引用作用域不可交叠,就是防止在交叠部分互相有可能发生变量的修改。
// error[E0499]: cannot borrow `a` as mutable more than once at a time
// --> src/main.rs:4:22
// |
// 3 | let mut a_mut1 = &mut a;
// | ------ first mutable borrow occurs here
// 4 | let mut a_mut2 = &mut a;
// | ^^^^^^ second mutable borrow occurs here
// 5 | println!("a_mut2 {}", a_mut2);
// 6 | println!("a_mut1 {}", a_mut1);
// | ------ first borrow later used here
fn main() {
let mut a = 123;
let mut a_mut1 = &mut a;
let mut a_mut2 = &mut a; //-> 防止在这跟下面对a_mut2输出中间对a_mut1做改变。
println!("a_mut2 {}", a_mut2); //-> 进而可能会导致程序的不正确。因为两个都是对a的可变引用
println!("a_mut1 {}", a_mut1);
}
// 修改如下则正常。
fn main() {
let mut a = 123;
let mut a_mut1 = &mut a;
println!("a_mut1 {}", a_mut1);
let mut a_mut2 = &mut a;
println!("a_mut2 {}", a_mut2);
}
在有借⽤的情况下,不能对所有权变量进⾏更改值的操作。
// 正常
fn main() {
let mut a = 123;
let a_mut = &mut a; // 因为这一行a_mut是最后的使用结束,后面对a更改则对a_mut没影响
a = 321;
println!("{}", a)
}
// error[E0506]: cannot assign to `a` because it is borrowed
// --> src/main.rs:4:5
// |
// 3 | let a_mut = &mut a;
// | ------ `a` is borrowed here
// 4 | a = 321;
// | ^^^^^^^ `a` is assigned to here but it was already borrowed
// 5 | println!("{}", a_mut)
// | ----- borrow later used here
fn main() {
let mut a = 123;
let a_mut = &mut a;
a = 321;
println!("{}", a_mut) // 在有借⽤的情况下,不能对所有权变量进⾏更改值的操作
}
// 更别提a_mut对a是不可变引用了,下面也报错。
fn main() {
let mut a = 123;
let a_mut = &a;
a = 321; // 在有借⽤的情况下,不能对所有权变量进⾏更改值的操作
println!("{}", a_mut)
}
// 这正常是因为a_mut在其定义的那一行就已经生命周期结束了。b_mut同理。
fn main() {
let mut a = 123;
let a_mut = &mut a;
let b_mut = &mut a;
}
// 上面正确,下面报错
// error[E0499]: cannot borrow `a` as mutable more than once at a time
// --> src/main.rs:4:17
// |
// 3 | let a_mut = &mut a;
// | ------ first mutable borrow occurs here
// 4 | let b_mut = &mut a;
// | ^^^^^^ second mutable borrow occurs here
// 5 | println!("{}", a_mut)
// | ----- first borrow later used here
// 可以说是作用域重叠,也可以说是不能存在两个对a的可变引用。
fn main() {
let mut a = 123;
let a_mut = &mut a;
let b_mut = &mut a;
println!("{}", a_mut)
}
// error[E0506]: cannot assign to `a` because it is borrowed
// --> src/main.rs:4:5
// |
// 3 | let a_mut = &mut a;
// | ------ `a` is borrowed here
// 4 | a = 456;
// | ^^^^^^^ `a` is assigned to here but it was already borrowed
// 5 | println!("{}", a_mut)
// | ----- borrow later used here
fn main() {
let mut a = 123;
let a_mut = &mut a;
a = 456;
println!("{}", a_mut)
}
//正常,是因为a_mut在其定义的那一行就已经生命周期结束了,即借⽤完成了。
fn main() {
let mut a = 123;
let a_mut = &mut a;
a = 456;
println!("{}", a)
}
// error[E0382]: borrow of moved value: `b`
// --> src/main.rs:5:20
// |
// 3 | let b = &mut a;
// | - move occurs because `b` has type `&mut i32`, which does
// not implement the `Copy` trait
// 4 | let c = b;
// | - value moved here
// 5 | println!("{}", b)
// | ^ value borrowed here after move
// |
fn main() {
let mut a = 123;
let b = &mut a;
let c = b;
println!("{}", b) // 改为c即正确
}