【发布时间】:2018-01-25 05:50:45
【问题描述】:
这在任何语言中都应该是一项微不足道的任务。这在 Rust 中不起作用。
use std::collections::HashMap;
fn do_it(map: &mut HashMap<String, String>) {
for (key, value) in map {
println!("{} / {}", key, value);
map.remove(key);
}
}
fn main() {}
这是编译器错误:
error[E0382]: use of moved value: `*map`
--> src/main.rs:6:9
|
4 | for (key, value) in map {
| --- value moved here
5 | println!("{} / {}", key, value);
6 | map.remove(key);
| ^^^ value used here after move
|
= note: move occurs because `map` has type `&mut std::collections::HashMap<std::string::String, std::string::String>`, which does not implement the `Copy` trait
为什么它试图移动一个引用?从文档中,我不认为移动/借用适用于引用。
【问题讨论】:
标签: hashmap rust iteration mutability