Replies: 162 comments 91 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 main() {
let string1 = String::from("long string is long");
{
let string2 = String::from("xyz");
{
let result = longest(string1.as_str(), string2.as_str());
println!("The longest string is {}", result);
}
println!("{}", string1);
println!("{}", string2);
}
}
fn longest<'a, 'b>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
x
}
} 并不符合“返回值的生命周期也是 x 和 y 中作用域较小的那个。”吧?这里 'a 所代表的应该就是 result 自己的生命周期吧(因为它最短),然后在这种情况下,前面 'a string1 和 'a string2 就是说明它们的生命周期至少要大于等于 result 的,然后因为代码符合这些条件,所以通过编译? |
Beta Was this translation helpful? Give feedback.
-
感觉这个对理解会有点帮助: fn main() {
let string1 = String::from("long string is long");
{
let result;
{
let string2 = String::from("xyz");
result = longest(string1.as_str(), string2.as_str());
println!("The longest string is {}", result);
}
println!("{}", string1);
println!("{}", result);
// println!("{}", string2);
}
}
fn longest<'a>(x: &'a str, y: & str) -> &'a str {
x
} 在这种情况下,程序不会报错,因为和 string2 的生命周期/作用域没有任何关系。 fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
x
} 这种时候就会报错了,因为 'a 代表的是它们仨里最短的,在这种情况下也就是 string2 的作用域,仅限于内层花括号内。而虽然整个函数和 y(string2)没有任何关系,因为加了标注,然后实际情况(result 出了内层还被用了)和标注情况(result 应该和 string2 作用域一样)不一致,所以不予通过。 |
Beta Was this translation helpful? Give feedback.
-
眼睛看懂了 |
Beta Was this translation helpful? Give feedback.
-
'a: 'b,是生命周期约束语法,跟泛型约束非常相似,用于说明 'a 必须比 'b 活得久。 |
Beta Was this translation helpful? Give feedback.
-
'a: 'b 理解下来是 'a > 'b
…------------------ 原始邮件 ------------------
发件人: ***@***.***>;
发送时间: 2025年4月14日(星期一) 下午3:39
收件人: ***@***.***>;
抄送: ***@***.***>; ***@***.***>;
主题: Re: [sunface/rust-course] advance/lifetime/basic (Discussion #609)
这句是不是写错了,是不是应该是'b必须比'a 活得久
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
生命周期只是为了取悦编译器。这种事情有更好的实现方案吗?(比如引用计数?) |
Beta Was this translation helpful? Give feedback.
-
struct ImportantExcerpt<'a> { |
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.
-
生命周期,还有一点没看懂 |
Beta Was this translation helpful? Give feedback.
-
嘿,爷内存硬盘不值钱,每个变量都给我静态周期(doge)(好像和内存泄露就没区别了) |
Beta Was this translation helpful? Give feedback.
-
真的很好 |
Beta Was this translation helpful? Give feedback.
-
impl<'a: 'b, 'b> ImportantExcerpt<'a> {
fn announce_and_return_part(&'a self, announcement: &'b str) -> &'b str {
self.part
}
}
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
}
} 两者有细微区别?'b的在前者中是整个impl维度可用的? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
https://course.rs/advance/lifetime/basic.html
Beta Was this translation helpful? Give feedback.
All reactions