如何遍历Hashmap,打印键/值并在Rust中删除值? [英] How to iterate through a Hashmap, print the key/value and remove the value in Rust?
问题描述
这应该是任何语言的琐碎任务.这在Rust中不起作用.
This should be a trivial task in any language. This isn't working in 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
为什么要尝试移动参考?从文档中,我不认为移动/借阅适用于引用.
Why it is trying to move a reference? From the documentation, I didn't think moving/borrowing applied to references.
推荐答案
不允许这样做的原因至少有两个:
There are at least two reasons why this is disallowed:
-
您将需要同时引用两个对
map
的可变引用-一个由for
循环中使用的迭代器保存,而另一个则在变量map
中以调用map.remove
.
You would need to have two concurrent mutable references to
map
— one held by the iterator used in thefor
loop and one in the variablemap
to callmap.remove
.
在尝试对地图进行变异时,您具有对地图中键和值 的引用.如果允许您以任何方式修改地图,则这些引用可能会失效,从而为内存不安全打开了大门.
You have references to the key and the value within the map when trying to mutate the map. If you were allowed to modify the map in any way, these references could be invalidated, opening the door for memory unsafety.
Rust的核心原则是别名XOR可变性.您可以对一个值有多个不可变的引用,也可以对它有一个可变的引用.
A core Rust principle is Aliasing XOR Mutability. You can have multiple immutable references to a value or you can have a single mutable reference to it.
我不认为移动/借阅适用于引用.
I didn't think moving/borrowing applied to references.
每种类型都受Rust的移动规则和可变别名的约束.请让我们知道文档的哪一部分说不是,所以我们可以解决这个问题.
Every type is subject to Rust's rules of moving as well as mutable aliasing. Please let us know what part of the documentation says it isn't so we can address that.
为什么要移动参考?
Why it is trying to move a reference?
这由两部分组成:
- 您只能有一个可变引用
-
for
循环将该值用于按值迭代
- You can only have a single mutable reference
for
loops take the value to iterate over by value
当您调用for (k, v) in map {}
时,map
的所有权被转移到for循环中,并且现在消失了.
When you call for (k, v) in map {}
, the ownership of map
is transferred to the for loop and is now gone.
我将对地图(&*map
)进行不可变的借用,并对其进行迭代.最后,我将清除所有内容:
I'd perform an immutable borrow of the map (&*map
) and iterate over that. At the end, I'd clear the whole thing:
fn do_it(map: &mut HashMap<String, String>) {
for (key, value) in &*map {
println!("{} / {}", key, value);
}
map.clear();
}
使用以字母"A"开头的键删除每个值
remove every value with a key that starts with the letter "A"
我会使用 HashMap::retain
:
I'd use HashMap::retain
:
fn do_it(map: &mut HashMap<String, String>) {
map.retain(|key, value| {
println!("{} / {}", key, value);
!key.starts_with("a")
})
}
这保证了key
和value
在实际修改地图时不再存在,因此,他们本来应该拥有的借用现在都消失了.
This guarantees that key
and value
no longer exist when the map is actually modified, thus any borrow that they would have had is now gone.
这篇关于如何遍历Hashmap,打印键/值并在Rust中删除值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!