知识卡片
借用诊断先找规则再改代码
读借用错误时先找目标何时释放、引用何时还被使用,再看编译器建议。简单情况可把临时拥有值绑定到变量,或缩短借用区间;复杂表达式可逐步拆成带类型的局部值。建议是定位线索,不保证机械套用就能保持原有语义。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 3. Concepts Local code refactoring · 查看原书依据
Local code refactoring The first tactic is to pay attention to the compiler’s error messages, because the Rust developers have put a lot of effort into making them as helpful as possible: /// If `needle` is present in `haystack`, return a slice containing it. pub fn find<'a, 'b>(haystack: &'a str, needle: &'b str) -> Option<&'a str> { haystack .find(needle)
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Local code refactoring》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u093-ch03-016)。
- 结论依据:原文在“Local code refactoring”附近的定义、例子或代码支撑本卡核心说明:读借用错误时先找目标何时释放、引用何时还被使用,再看编译器建议。边界按已读前提解释;示例未执行。
- 原始内容:Local code refactoring The first tactic is to pay attention to the compiler’s error messages, because the Rust developers have put a lot of effort into making them as helpful as possible: /// If `needle` is present in `haystack`, return a slice containing it. pub fn find<'a, 'b>(haystack: &'a str, needle: &'b str) -> Option<&'a str> { haystack .find(needle)