advance/lifetime/static #673
Replies: 51 comments 62 replies
-
从正常的设计逻辑上看 |
Beta Was this translation helpful? Give feedback.
-
还是没明白 &'static 和 T: 'static 他俩的区别 😂 |
Beta Was this translation helpful? Give feedback.
-
'static : 我没有针对谁,我只是说在座的都是 XX |
Beta Was this translation helpful? Give feedback.
-
都是高手啊。学到了好多东西 |
Beta Was this translation helpful? Give feedback.
-
我还是没太理解什么是泛型和程序活的一样久,还是指的这个入參的值本身是吧?那我也不理解 &T 这个引用符合的是哪个生命周期? |
Beta Was this translation helpful? Give feedback.
-
我觉得这个章节对T: 'static的描述有误 [首先,在以下两种情况下,T: 'static 与 &'static 有相同的约束:T 必须活得和程序一样久。] [这段代码竟然不报错了!原因在于我们约束的是 T,但是使用的却是它的引用 &T,换而言之,我们根本没有直接使用 T,因此编译器就没有去检查 T 的生命周期约束!它只要确保 &T 的生命周期符合规则即可,在上面代码中,它自然是符合的。] |
Beta Was this translation helpful? Give feedback.
-
那, 如果这个函数中, T类型, 一个参数想是'static, 另一个参数没有限制该怎么写呢... |
Beta Was this translation helpful? Give feedback.
-
看了收益匪浅,作者大大继续加油!下面有两个小问题 原文:“&'static 对于生命周期有着非常强的要求:一个引用必须要活得跟剩下的程序一样久,才能被标注为 &'static。” 另外还有一处拼写错误,在Rust By Practice( Rust 练习实践 )中第一题 题目的最后一个static拼写错误。 |
Beta Was this translation helpful? Give feedback.
-
写了几个月rust转头来看文章和评论收获不小 |
Beta Was this translation helpful? Give feedback.
-
我迷了,过两天再回过头再看一遍。。。 |
Beta Was this translation helpful? Give feedback.
-
感觉作者大大在这里把问题讲复杂了,反而让读者云里雾里的。其实T:'static约束的理解很简单,即T类型不持有非'static生命周期的引用。 fn static_bound<T:'static>(t: &T) {} |
Beta Was this translation helpful? Give feedback.
-
我觉得介绍这章的时候需要再次highlight或者强调一个点就是 -> binary static memory,在两种情况下代码会存在这里:
涉及到引用生命周期时,凡是像如上两种情况,那必是 另外提一点就是上面的陈述反过来不一定成立,即 用我的这个解释去套用
和以及它下面的, 就很容易理解 |
Beta Was this translation helpful? Give feedback.
-
为什么空结构体是'static生命周期,不是很理解 |
Beta Was this translation helpful? Give feedback.
-
关于
|
Beta Was this translation helpful? Give feedback.
-
但是,&'static 生命周期针对的仅仅是引用,而不是持有该引用的变量,对于变量来说,还是要遵循相应的作用域规则 : static 到底针对谁? 答案是引用指向的数据,而引用本身是要遵循其作用域范围的,我们来简单验证下: @作者,这两句话应该表述有矛盾。 |
Beta Was this translation helpful? Give feedback.
-
我不知道是作者写得乱还是写错了,我的理解是这样的: 在下面这个函数里面传入&i32的时候,T = &i32,则 &i32 : Debug + 'static,因此要求传入的得是&'static i32。如果传入的是单纯的i32,则是i32 : Debug + 'static,就会发现编译通过了,因为'static这种生命周期作为模板约束不会干涉普通所有权变量。 fn print_it<T: Debug + 'static>(input: T) {
println!( "'static value passed in is: {:?}", input );
} 然后,在修改后的函数里面,传入&i32的话,显然T会被推导成i32而不是&i32,所以和上面说的一样不会被'static约束。 fn print_it<T: Debug + 'static>(input: &T) {
println!( "'static value passed in is: {:?}", input );
} |
Beta Was this translation helpful? Give feedback.
-
我整理了這裡的一些回答,加上一些自己的實驗以及想法,有錯歡迎指教😅 Two case:
Example:
|
Beta Was this translation helpful? Give feedback.
-
"到底是 &'static 这个引用还是该引用指向的数据活得跟程序一样久呢?答案是引用指向的数据" |
Beta Was this translation helpful? Give feedback.
-
自己测试的例子,相信大家看完会有很清晰认识。只有最后的两个s32的打印是失败的,其他情况都是成功的。 #[cfg(test)]
} |
Beta Was this translation helpful? Give feedback.
-
经过测试,只要解析出来的T不是或不包含引用类型,即&开头的类型,就不会检查变量是不是static的,自己创建的结构体啥的肯定不是static的,都能编译通过。 |
Beta Was this translation helpful? Give feedback.
-
T: 'static 我的理解是要求T如果是引用类型, 则指向的变量必须是全生命周期的, 如果不是引用类型忽略生命周期约束检查。 fn print_it<T: Debug + 'static>( input: T) {
println!( "'static value passed in is: {:?}", input );
}
fn print_it2<T: Debug + 'static>( input: &T) {
println!( "'static value passed in is: {:?}", input );
}
let i = 12;
print_it(&i); // 此时 T == &i32 满足要求,进行生命周期约束检查,i必须是static的 所以编译不通过
print_it(i); // 此时 T == i32 不满足T是引用类型要求,不需要进行生命周期约束检查 编译通过
print_it(&&i); // 此时 T == &&i32 满足要求,检查&i32是否是全生命周期,编译不通过
fn print_it2<T: Debug + 'static>( input: &T) {
println!( "'static value passed in is: {:?}", input );
}
print_it2(&i); // 此时 T == i32 不需要进行检查 编译通过
print_it2(&&i); // 此时 T == &&i32 满足要求,检查&i32是否是全生命周期,编译不通过
static s_iptr : &i32 = &5;
print_it2(&s_iptr); // s_iptr 是满足static 所以编译通过 |
Beta Was this translation helpful? Give feedback.
-
虽然我写了好几年的Rust,但是我发现我很难总结出Rust的知识点,我写Rust全凭肌肉记忆! 对于
至于为什么,我试图总结一下,发现无法自洽。 |
Beta Was this translation helpful? Give feedback.
-
我常常在文章中看到字面字符串,因为打包进二进制文件了所以不会 drop,我感觉,字面量被编译到二进制文件和这个字面量一旦加载到内存就不会 drop 之间没什么关系啊,或许中间还有其他的什么逻辑,但是如果说是直接相关真的难以理解 |
Beta Was this translation helpful? Give feedback.
-
有点懵,下面这代码该如何解释:
至于
|
Beta Was this translation helpful? Give feedback.
-
变量名只有作用域没有周期,变量名只是个标记符号哪来的周期啊 |
Beta Was this translation helpful? Give feedback.
-
我的理解是T: 'static是
在使用泛型时,很多时候并不需要考虑有生命周期的情况,所以可以使用T: 'static来“满足和取悦编译器” |
Beta Was this translation helpful? Give feedback.
-
还得翻翻评论区,原文把我看晕了 |
Beta Was this translation helpful? Give feedback.
-
我的理解是,T: Debug + 'static,的意思是T要么不包含引用,要么包含的引用生命周期必须长于'static |
Beta Was this translation helpful? Give feedback.
-
首先感谢大佬的无私奉献!不过感觉这一章说的不太明白,看下面的代码: use std::fmt::Debug; fn main() { |
Beta Was this translation helpful? Give feedback.
-
“大家有没有想过,到底是 &'static 这个引用还是该引用指向的数据活得跟程序一样久呢?”为什么和前面的"但是,&'static 生命周期针对的仅仅是引用,而不是持有该引用的变量,对于变量来说,还是要遵循相应的作用域规则 :“ ,怎么矛盾了?有点混乱。还有,前面的“get_memory_location”为什么不会立即回收?还能用指针指出?但我写了这段:
它的表现就是s2的内存,准确准时被回收,但我实在不明白,为什么上次我提问时的get_memory_location又不会及时回收的? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
advance/lifetime/static
https://course.rs/advance/lifetime/static.html
Beta Was this translation helpful? Give feedback.
All reactions