知识卡片
静态借用要求长期数据
&‘static T 要求所指数据能够在整个程序期间有效。字面量所指内容存放在程序二进制中,符合该条件。局部所有者离开后的借用不能靠标注 ‘static 变得有效。遇到提示时,应检查实际所有者和数据能覆盖的使用范围。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 泛型类型、trait 与生命周期 Lifetime Annotations in Struct Definitions · 查看原书依据
You might see suggestions to use the 'static lifetime in error messages. But before specifying 'static as the lifetime for a reference, think about whether the reference you have actually lives the entire lifetime of your program or not, and whether you want it to.
参考来源
- 位置:《The Rust Programming Language(第2版)》第10章 泛型类型、trait 与生命周期(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c10.xhtml`,块 0323)
- 结论依据:原文对照字面量的真正静态存储与生命周期不匹配时生硬加标注的错误。
- 原始内容:You might see suggestions to use the 'static lifetime in error messages. But before specifying 'static as the lifetime for a reference, think about whether the reference you have actually lives the entire lifetime of your program or not, and whether you want it to.