Skip to content

Question about Iterator and for loops #5

@hmuendel

Description

@hmuendel

I just started learning rust and in my first project, I used your multi-map which worked like a charm.
However with std::collections::HashMap I can do a for loop over the map itself.

Trying the same with MultiMap:

use multi_map::MultiMap;

fn main() {
    let m = MultiMap::new();
    m.insert("k1", "k12", "v1");
    m.insert("k2", "k22", "v2");
    for (k, t) in m {
        println!("k: {} tuple: {:?}", k, t);
    }
}
error[E0277]: `multi_map::MultiMap<&str, &str, &str>` is not an iterator
 --> src/main.rs:7:19
  |
7 |     for (k, t) in m {
  |                   ^ `multi_map::MultiMap<&str, &str, &str>` is not an iterator
  |
  = help: the trait `std::iter::Iterator` is not implemented for `multi_map::MultiMap<&str, &str, &str>`
  = note: required by `std::iter::IntoIterator::into_iter`

I thought, easy: I will just implement the Iterator trait for MultiMap and contribute.

After forking, I saw that there is an .iter() method which returns an Iterator and which works fine iterating over. I still would like MultiMap to behave exactly like a HashMap as far as possible. So I started to look how HashMap implements Iterator and then my confusion was complete.
It appears that HashMap only implements IntoIter.?!?

So my noble intend to contribute, became a humble question about what makes sense.
Would it make sense to implement the trait, so MultiMap could be directly used in a for loop? I am happy to contribute, but hesitant due to my noobness.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions