Why can a Range be collected into a HashMap or Vec in Rust?
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.
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.