知识卡片
ToOwned产生拥有形式
ToOwned从借用视图生成拥有形式,例如由str得到String,扩大了只在同一类型上复制的Clone使用范围。转换后调用方负责拥有结果,通常也承担复制或分配成本。它适合确需独立存活数据的边界,不应仅为绕过借用而无条件复制。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types More Pointer Traits · 查看原书依据
ToOwned trait builds on the Borrow trait, adding a to_owned() method that produces a new owned item of the underlying type. This is a generalization of the Clone trait: where Clone specifically requires a Rust reference &T, ToOwned instead copes with things that implement Borrow. This gives a couple of possibilities for handling both references and moved ite
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《More Pointer Traits》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u045-ch01-062)。
- 结论依据:原文在“ToOwned”附近的定义、例子或代码支撑本卡核心说明:ToOwned从借用视图生成拥有形式,例如由str得到String,扩大了只在同一类型上复制的Clone使用范围。边界按已读前提解释;示例未执行。
- 原始内容:ToOwned trait builds on the Borrow trait, adding a to_owned() method that produces a new owned item of the underlying type. This is a generalization of the Clone trait: where Clone specifically requires a Rust reference &T, ToOwned instead copes with things that implement Borrow. This gives a couple of possibilities for handling both references and moved ite