on-internet
← Community

Why can a Range be collected into a HashMap or Vec in Rust?

Stack Overflow ·

I recently encountered a problem while learning about iterators. I attempted to convert a list of numbers into a Vec, but when I changed the type to HashMap, no error occurred. For example: type Test = Vec<(usize, Vec<usize>)>; // type TestHashMap = HashMap<usize, Vec<usize>>; ... fn make_heavy_things() -> Test { let x = (1..=10).map(|v| (v, vec![v])).collect(); x } // x can be a HashMap or Vec depending on the return type Both types seem to work without any issues, and common methods like len() work as expected. I'm curious about the reason behind this behavior.

Comments

Sign in or sign up to comment and upvote.

No comments yet.