basic/ownership/borrowing #645
Replies: 144 comments 137 replies
-
同一时刻,你只能拥有要么一个可变引用, 要么任意多个不可变引用。这个同一时刻怎么理解?还是同一作用域? |
Beta Was this translation helpful? Give feedback.
-
可变引用与不可变引用不能同时存在小节下的
这一段是否应该改为
|
Beta Was this translation helpful? Give feedback.
-
引用、借用,这章感觉有点绕。引用是语法层面的,借用是概念层面的,可以这么理解么 |
Beta Was this translation helpful? Give feedback.
-
无法在函数结束后返回引用,正如无法在法国投降前攻下巴黎 |
Beta Was this translation helpful? Give feedback.
-
感觉跟指针差不多, 但是没有 |
Beta Was this translation helpful? Give feedback.
-
看来之前学习Rust走错路了,之前直接看英文教程,本母语为中文的开发者,理解是真不到位,现在重新学一遍,瞬间豁然开朗了 |
Beta Was this translation helpful? Give feedback.
-
感谢,确实比官方的文档更细致入微,之前看了几遍官方文档也没真正弄懂,现在终于搞懂了! |
Beta Was this translation helpful? Give feedback.
-
同一作用域,特定数据只能有一个可变引用 |
Beta Was this translation helpful? Give feedback.
-
fn main() {
let mut s = String::from("456");
let mut n = &mut s;
foo(&mut n);
}
fn foo(s: &mut String) {
let m = s;
m.push_str("asd")
} fn main() {
let mut s = String::from("456");
let n = &mut s;
foo(n);
}
fn foo(s: &mut String) {
let m = s;
m.push_str("asd")
}
这三个都可以运行🤨 |
Beta Was this translation helpful? Give feedback.
-
借用在值的改变上,既然String可以使用&mut进行改变,为什么同样使用基本类型却没办法进行改变了,有点一丢丢感觉不合理,这不是欺负老实人嘛! |
Beta Was this translation helpful? Give feedback.
-
所以借用的目的就是为了让某些变量,不会在调用了某一个函数之后就失去作用吗? |
Beta Was this translation helpful? Give feedback.
-
引用必须有效的有点怪,不是因为引用必须发生在所有者生命周期内,才出现的引用肯定是有效的吗? |
Beta Was this translation helpful? Give feedback.
-
补充一些关于可变引用的内容: 防止出现数据竞争不光是多线程的事情,在单线程时依然有用武之地。单线程时假设同时出现两个引用:一个可变,一个不可变,则可能会造成混乱。 fn main() {
let mut x = 42;
let y = &x; // 不可变引用
change_x(&mut x); // 可变引用(编译错误)
println!("{}", y);
}
fn change_x(z: &mut i32) {
*z = 99;
} 这个代码里y拿到了x的值42,而且z修改了x的值为99。 如果这份代码能编译通过,虽然实际中y的值也会随之变化为99,但程序员可能会假定拿到的是开始时的值(假如修改做得更隐蔽一些,就很难看出来x的值已经改变了),随后导致各种逻辑错误。这就是单线程也需要防止数据竞争:防止使用了过期的值、引发数据错乱等。 变量本身和可变引用之间是否也是一种数据竞争?是,所以实际上在声明了一个可变引用后,使用这个可变引用时会独占这个变量的访问权。 下面这份代码是会报错的: let mut x = 10;
let y = &mut x; // 可变引用
x = 20; // 尝试直接修改变量(违反规则)
*y = 30; 所以不光是可变引用和不可变引用不可以在一个作用域内使用,甚至变量本身和可变引用都不可以在一个作用域内使用。 |
Beta Was this translation helpful? Give feedback.
-
关于“所有权”小节提到的:“但是注意: 可变引用 &mut T 是不可以 Copy的”,我有一些疑惑,既然可变引用 &mut T没有copy 特征,那么不就意味着可变引用传入函数后,后续代码将无法继续使用这个可变引用吗?但是我实际测试发现,函数调用并没有发生可变引用的move: let s1 = &mut s; // 可变引用 let l = str_len(s1); // 传入参数(如果没有copy特征,不就意味着会变量s1会发生move,进而失效?) // 正当我怀疑可变引用具有copy特征时,发现下面的两行代码会提示s1被move而报错(黑人问号脸) } |
Beta Was this translation helpful? Give feedback.
-
感觉和C++的指针差不多啊,只是不再用释放内存了 |
Beta Was this translation helpful? Give feedback.
-
有点像互斥锁与共享锁 |
Beta Was this translation helpful? Give feedback.
-
Rust 1.81 允许这样写了,是借用检查器更智能了? let mut s = String::from("hello");
let r1 = &mut s;
let r2 = &mut s; |
Beta Was this translation helpful? Give feedback.
-
Rust 1.81 这样也可以了 let mut s = String::from("hello");
let r1 = &mut s;
let r2 = &mut s;
println!("{},", r2); 更加智能,只要不同时用就可以? |
Beta Was this translation helpful? Give feedback.
-
文章里,s、s1、“hello”字符串,3者的关系有问题。s应该也指向“hello”而不是s1. |
Beta Was this translation helpful? Give feedback.
-
未曾拥有过,所以也不会失去 |
Beta Was this translation helpful? Give feedback.
-
悬垂引用的那部分的意思是 借用不会改变原主人的生命周期? |
Beta Was this translation helpful? Give feedback.
-
讲解的很好,下次继续讲解 |
Beta Was this translation helpful? Give feedback.
-
basic/ownership/borrowing
https://course.rs/basic/ownership/borrowing.html
Beta Was this translation helpful? Give feedback.
All reactions