Replies: 130 comments 74 replies
-
谢谢大佬,这本书真的写的很好😉 |
Beta Was this translation helpful? Give feedback.
-
感谢鼓励 :) |
Beta Was this translation helpful? Give feedback.
-
感谢大佬 收获好大 弄明白了很多以前糊里糊涂的概念 |
Beta Was this translation helpful? Give feedback.
-
感谢大佬,写的通俗易懂,比什么心智模型balabala一堆术语好太多了。 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
我在想结构体上的生命周期为什么不能省略,在例子中结构体只有一个引用的时候,这个引用的有效性其实编译器是可以推断出来的啊 |
Beta Was this translation helpful? Give feedback.
-
大佬,能不能写些实际的例子?比如 teacher {vector<student&>} 另外所有的方法和结构体的生命周期均要求teacher大于student |
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() { 以下代码也可运行: fn main() { |
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 useless<'a>(first: &'a i32, second: &'a i32) {
let tmp = 1;
first = &tmp; // 如没有这代码,first的被引用对象的生命周期至少和 'a 一样长,接下改变了
} 他会报如下错误:
至于 |
Beta Was this translation helpful? Give feedback.
-
666,写的真好 |
Beta Was this translation helpful? Give feedback.
-
写的很细。 |
Beta Was this translation helpful? Give feedback.
-
"例如一个变量,只能活一个花括号,那么就算你给它标注一个活全局的生命周期,它还是会在前面的花括号结束处被释放掉,并不会真的全局存活。" 那可以全部标注 'static 吗? |
Beta Was this translation helpful? Give feedback.
-
谢谢大佬 学习了 |
Beta Was this translation helpful? Give feedback.
-
longest函数编译器完全可以做出判断返回值的生命周期,为什么不自动加上非要人去写 |
Beta Was this translation helpful? Give feedback.
-
感觉这个例子可能更好 impl<'a> ImportantExcerpt<'a> {
fn announce_and_return_part<'b, 'c>(&'a self, announcement: &'c str) -> &'b str
where
'a: 'b,
{
println!("Attention please: {announcement}");
self.part
}
} 从返回值来看,这个函数的返回值的生命周期应该由 let i = ImportantExcerpt { part: "part" };
let s;
{
let novel = "Hello, What's your name?".to_string();
s = i.announce_and_return_part(&novel);
};
println!("{:?}", s); 编译器当然要报错,毕竟 |
Beta Was this translation helpful? Give feedback.
-
'''struct ImportantExcerpt<'a> { |
Beta Was this translation helpful? Give feedback.
-
静态生命周期举一个这样的例子,感觉会更加生动: fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
fn main() {
let str1 = String::from("abcd");
let result;
{
let str2 = String::from("xyz");
result = longest(&str1, &str2);
}
// 编译错误,result可能会引用销毁的变量str2
println!("The longest string is {}", result);
} 但将变量str2的类型改成 fn main() {
let str1 = String::from("a");
let result;
{
let str2 = "xyz"; // 关键改动
result = longest(&str1, &str2);
}
println!("The longest string is {}", result);
} 这是因为str2是 |
Beta Was this translation helpful? Give feedback.
-
理解生命周期的基础概念:
|
Beta Was this translation helpful? Give feedback.
-
生命周期的标注像极了iOS的约束布局。 |
Beta Was this translation helpful? Give feedback.
-
https://course.rs/advance/lifetime/basic.html
Beta Was this translation helpful? Give feedback.
All reactions