知识卡片
借用区间可在最后使用处结束
非词法生命周期允许编译器在引用最后一次使用后结束相应借用,而不是一律等到变量作用域末尾。先用可变切片修改字符串,之后不再使用该切片时,可能再共享借用字符串。缩短的是借用约束区间,不代表拥有的字符串也在那一行析构。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 3. Concepts Scope of a Lifetime · 查看原书依据
non-lexical lifetimes, allows the borrow checker to be a little bit more generous: { // `s` owns the `String`. let mut s: String = "Hello, world".to_string(); // Create a mutable reference to the `String`. let greeting = &mut s[..5]; greeting.make_ascii_uppercase(); // .. no use of `greeting` after this point // Creating an immutable reference to the `String
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Scope of a Lifetime》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u080-ch03-004)。
- 结论依据:原文在“non-lexical lifetimes”附近的定义、例子或代码支撑本卡核心说明:非词法生命周期允许编译器在引用最后一次使用后结束相应借用,而不是一律等到变量作用域末尾。边界按已读前提解释;示例未执行。
- 原始内容:non-lexical lifetimes, allows the borrow checker to be a little bit more generous: { // `s` owns the `String`. let mut s: String = "Hello, world".to_string(); // Create a mutable reference to the `String`. let greeting = &mut s[..5]; greeting.make_ascii_uppercase(); // .. no use of `greeting` after this point // Creating an immutable reference to the `String