知识卡片
直接消费迭代器解析参数
让配置解析器接收 impl Iterator<Item = String>,可直接传入 env::args()。依次 next() 跳过调用名并取得查询和路径,缺项时及时返回相应 Err。类型约束强调需要可移动的 String 项,省去收集整批参数后按索引借用再克隆。
相关卡片 [[选择迭代元素的所有权]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 函数式语言特性:迭代器与闭包 Improving Our I/O Project · 查看原书依据
Once Config::build takes ownership of the iterator and stops using indexing operations that borrow, we can move the String values from the iterator into Config rather than calling clone and making a new allocation.
参考来源
- 位置:《The Rust Programming Language(第2版)》第13章 函数式语言特性:迭代器与闭包(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c13.xhtml`,块 0162)
- 结论依据:原文指出拿到拥有值的迭代器后,配置可以移动字符串而省去克隆。
- 原始内容:Once Config::build takes ownership of the iterator and stops using indexing operations that borrow, we can move the String values from the iterator into Config rather than calling clone and making a new allocation.