let mut s1 = "10";
let s2 = &mut s1;
*s2 = "0";
println!("addr:{:p}", s1);
println!("addr:{:p}", *s2);
cannot borrow s1
as immutable because it is also borrowed as mutable
--> src/gus/test/t1.rs:57:27
|
53 | let s2 = &mut s1;
| ------- mutable borrow occurs here
...
57 | println!("addr:{:p}", s1);
| ^^ immutable borrow occurs here
58 |
59 | println!("addr:{:p}", *s2);
| --- mutable borrow later used here
最后两行打印语句顺序互换,没问题,这是所有权里面的什么原则???
1
共 2 条评论, 1 页
评论区
写评论醍醐灌顶!
但凡仔细看过:https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html
都不会问这个问题。