Replies: 137 comments 81 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.
-
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.
-
个人认为函数的生命周期标注,是为了解决Rust的所有权特性引发的问题。 |
Beta Was this translation helpful? Give feedback.
-
不做完不爽夫斯~~终于看完这节了 休息休息 |
Beta Was this translation helpful? Give feedback.
-
太强了,看AI,看官方文档看了两天也没整明白,大佬写的一下子就看明白了,幸好是在你写了圣经之后开始学的Rust |
Beta Was this translation helpful? Give feedback.
-
rust使用生命周期标注来防止引用失效。—— Mr.Krabs |
Beta Was this translation helpful? Give feedback.
-
有一点让我很困惑的是文中的这个例子: impl<'a> ImportantExcerpt<'a> {
fn announce_and_return_part<'b>(&'a self, announcement: &'b str) -> &'b str
where
'a: 'b,
{
println!("Attention please: {}", announcement);
self.part
}
} 这里不是要求生命周期 'a 只少活的和 'b一样久么,为什么我下面的测试代码还是可以编译呢 fn test_1() {
let mut announce = String::from("announcement"); // 这里应该是生命周期'b开始的地方
{
let part = String::from("this is part"); // 这里是生命周期'a开始的地方
let i = ImportantExcerpt { part: &part };
println!("{}", i.announce_and_return_part(&announce));
} // 'a在这里结束
// println!("{}", part); 很明显这里的part已经不能使用了
// 这很明显不能满足 'a: 'b 这个生命周期的约束了吧
announce.push('😯');
println!("{}", announce); // 'b在这里结束
} 有没有大佬可以帮忙解释一下,是我哪里理解的有问题么 |
Beta Was this translation helpful? Give feedback.
-
本书写的确实很好,很快就学到了本章。对于本章中的有一段,个人觉得有点晦涩了: 章节原文:该函数签名表明对于某些生命周期 'a,函数的两个参数都至少跟 'a 活得一样久,同时函数的返回引用也至少跟 'a 活得一样久。 |
Beta Was this translation helpful? Give feedback.
-
https://course.rs/advance/lifetime/basic.html
Beta Was this translation helpful? Give feedback.
All reactions