Perhaps I do not see a forest for trees, but I wonder how I actually develop my methods so as not to work against hard collection types, but instead of Iterators. Consider this method.
pub fn print_strings(strings: Vec<String>) { for val in strings.iter() { println!("{}", val); } }
Obviously this is not suitable if I want to use it with a HashSet or HashMap .
So, I tried this:
use std::collections::*; fn main () { let strings = vec!("Foo", "Bar"); let mut more_strings = HashMap::new(); more_strings.insert("foo", "bar"); more_strings.insert("bar", "foo"); print_strings(&strings.iter()); print_strings(&more_strings.values()) } fn print_strings(strings: &Iterator<Item=&str>) { for val in strings { println!("{}", val); } }
Playpen (also for viewing a long compiler error)
http://is.gd/EYIK11
Unfortunately, this also doesn't seem like a trick. What am I missing?
source share