知识卡片
选择拥有或借用的迭代
遍历 Vec 时,for s in strings 将向量交给迭代器,s 拿到每个拥有的 String,之后不能再读取原向量。for s in &strings 给出共享引用 &String,适合检查后继续使用原向量。for s in &mut strings 给出独占引用 &mut String,可对原向量里的每个字符串就地修改。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Expressions 按终止条件和所有权选择循环 · 查看原书依据
The loop variable, then, will be a reference to each item in the collection:
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第05章《Expressions》|按终止条件和所有权选择循环,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch05.xhtml#loops`
- 结论依据:原文以按值遍历后的编译错误为对照,展示共享借用和可变借用迭代返回的元素类型。
- 原始内容:The loop variable, then, will be a reference to each item in the collection: