I have a code like below:
#[derive(Debug)]
struct Entry {
name:String,
year:u32
}
fn main() {
let mut set = HashSet::new();
set.insert(Entry {name:"hongkong".to_string(), year: 1957});
set.insert(Entry {name:"dubai".to_string(), year: 1987});
assert_eq!{false,set.insert(Entry {name:"dubai".to_string(), year: 2002})}
for entry in &mut set {
entry.year = 2025;
println!{"{entry:?}"}
}
}
impl PartialEq for Entry {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for Entry {}
impl Hash for Entry {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}
It gives me a compilation error like:
Compiling main ...
error[E0277]:&mut HashSet<Entry>
is not an iterator
--> main.rs:54:18
|
54 | f
or entry in &mut set {
| ^^^^^^^^&mut HashSet<Entry>
is not an iterator
|help: the trait
Iterator
is not implemented forHashSet<Entry>
, which is required by&mut HashSet<Entry>: IntoIterator
= note: required for&mut HashSet<Entry>
to implement
Iterator
= note: required for&mut HashSet<Entry>
to implementIntoIterator
I assume that Rust wants to avoid that HashSet entry is changed and can issue a clash of duplicate entries. Is there a way of a simple work around without a creation of own HashSet implementation? Or maybe I just overlooked a simple mistake?