知识卡片
克隆解耦配置的生命周期
从借用的参数切片不能移出字符串。用 clone() 时 Config 可独立持有参数,代价是额外分配和复制。在启动时只复制两个短值,可以为清晰性接受这个成本。若拥有参数本身,则可考虑直接移动。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- I/O 项目:构建命令行程序 Refactoring to Improve Modularity and Error Handling · 查看原书依据
There are a number of ways we could manage the String data; the easiest, though somewhat inefficient, route is to call the clone method on the values ❼ ❽. This will make a full copy of the data for the Config instance to own, which takes more time and memory than storing a reference to the string data.
参考来源
- 位置:《The Rust Programming Language(第2版)》第12章 I/O 项目:构建命令行程序(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c12.xhtml`,块 0089)
- 结论依据:原文比较复制成本与生命周期简化,以少量小字符串为例。
- 原始内容:There are a number of ways we could manage the String data; the easiest, though somewhat inefficient, route is to call the clone method on the values ❼ ❽. This will make a full copy of the data for the Config instance to own, which takes more time and memory than storing a reference to the string data.