Because of reasons, I want to define a generic function that can iterate over key-value pairs expressed either as a mapping, or as a vector of 2-tuples (or anything else that satisfies IntoIterator<Item=(K, V)>, where K and V are stringy). Concretely, I want this to work:
use std::collections::HashMap; fn main() { let vc = vec![ ("a", "foo"), ("b", "bar"), ("c", "baz") ]; operate(&vc); let mut map = HashMap::new(); map.insert("d", "blurf"); map.insert("e", "quux"); map.insert("f", "xyzzy"); operate(&map); } I've got a definition of operate that works for the HashMap, but not for the vector:
fn operate<I, K, V>(x: I) where I: IntoIterator<Item=(K, V)>, K: AsRef<str>, V: AsRef<str> { for (ref k, ref v) in x { println!("{}: {}", k.as_ref(), v.as_ref()); } } The error message I get is
error[E0271]: type mismatch resolving `<&std::vec::Vec<(&str, &str)> as std::iter::IntoIterator>::Item == (_, _)` --> test.rs:18:5 | 18 | operate(&vc); | ^^^^^^^ expected reference, found tuple | = note: expected type `&(&str, &str)` = note: found type `(_, _)` = note: required by `operate` and I don't understand it at all. For one thing, it seems like it's backwards, and for another, why am I only getting an error for the Vec and not the HashMap?