basic/ownership/borrowing #645
Replies: 118 comments 122 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.
-
struct S;
fn add(v: &mut Vec<S>, x: S) -> &mut S {
v.push(x);
v.last_mut().unwrap()
}
fn use_(v: &mut Vec<S>, x: &S) {}
fn borrow(s: &S) {}
fn main() {
let mut v = Vec::new();
let added_s = {
add(&mut v, S)
};
borrow(added_s);
use_(&mut v, added_s);
} 这段代码编译器说是多个可变借用:
但我感觉奇怪的是, |
Beta Was this translation helpful? Give feedback.
-
这一小节好像没有讲到 ref 关键字的部分,但是在课后练习的部分发现了有相关的题目,这是写漏了? |
Beta Was this translation helpful? Give feedback.
-
啊...这令人着迷的安全性 |
Beta Was this translation helpful? Give feedback.
-
fn main() {
let a = String::from("something");
foo(&a);
}
fn foo(s: &String) {
println!("s is {}", *s);
println!("s is {}", s);
} 为什么在 foo 里面的s可以不用解引用自动就可以获取到值,而且手动加上解引用也没有报错? |
Beta Was this translation helpful? Give feedback.
-
Nice !!! |
Beta Was this translation helpful? Give feedback.
-
但是,可变的s,和可变的s的引用s1;这俩都能修改s的内容,这不也是一种数据不安全么?本质上,与两个可变的s引用s1,s2不能同时使用,感觉是一样的道理呀~ 为啥第一种情况被允许呢 |
Beta Was this translation helpful? Give feedback.
-
fn main() {
let mut s1 = String::from("hello");
let r1 = &mut s1;
let r2 = &mut s1;
} 不是不能有多个可变引用么,我这段代码在本地可以过编译是为什么呀 |
Beta Was this translation helpful? Give feedback.
-
绝了,这语言简直就是为并行设计的,这种引用设计直接避免竞争问题 |
Beta Was this translation helpful? Give feedback.
-
建议可以单独说一下数组的引用,特别是如果多个可变/可变和不可变同时引用数组的不同元素的时候为什么会编译错误。rust会把数组看成一个整体,但是会有人会把数组每个元素都看成一个独立的内存产生迷惑。是的,那个人是我 |
Beta Was this translation helpful? Give feedback.
-
有个不太理解的问题,为什么会出现不可变和可变借用冲突。我大概的理解是 u_mut_ref 有对 name的可变借用,而 if let Some(ref name) 这句会生成一个对 name 的 不可变借用,然后在调用 modify_user(u_mut_ref, name) 这个函数的时候,同时有对name的可变借用和不可变借用的作用域冲突吗?
【错误提示】
|
Beta Was this translation helpful? Give feedback.
-
请问下,NLL编译器优化是不是还存在漏洞?
这是什么原因导致的? |
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