知识卡片
保存借用会传播生命周期约束
结构体保存&Item时需声明引用寿命,使用它的外层容器也需满足该约束。来自两个独立输入的借用可用不同寿命参数,来自同一输入的多处切片可共享参数。约束向上传播会增加接口负担,若不需要零复制,拥有数据常更简单;固定static借用等情况也不必机械增加泛型参数。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 3. Concepts Lifetimes in Data Structures · 查看原书依据
ReferenceHolder { pub index: usize, pub item: &Item, } error[E0106]: missing lifetime specifier --> src/main.rs:548:19 | 548 | pub item: &Item, | ^ expected named lifetime parameter | help: consider introducing a named lifetime parameter | 546 ~ pub struct ReferenceHolder<'a> { 547 | pub index: usize, 548 ~ pub item: &'a Item, | As usual, the compiler error
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Lifetimes in Data Structures》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u085-ch03-011)。
- 结论依据:原文在“ReferenceHolder”附近的定义、例子或代码支撑本卡核心说明:结构体保存&Item时需声明引用寿命,使用它的外层容器也需满足该约束。边界按已读前提解释;示例未执行。
- 原始内容:ReferenceHolder { pub index: usize, pub item: &Item, } error[E0106]: missing lifetime specifier --> src/main.rs:548:19 | 548 | pub item: &Item, | ^ expected named lifetime parameter | help: consider introducing a named lifetime parameter | 546 ~ pub struct ReferenceHolder<'a> { 547 | pub index: usize, 548 ~ pub item: &'a Item, | As usual, the compiler error