According to the documentation, this method https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect can create different collections based on the type of the variable its return value is assigned to. I've been researching this for a while now and I can't find the key reason it's able to do this. Is it overloaded generically somehow? How is it able to create different types based on the variable type?
I understand type inference is playing a role here, but I'm more confused about how that works in tandem with the actual underlying implementation of collect()
I'm beginning to learn rust so this concept is new to me. I am reading the rust book and have searched for this, but can't find what I'm looking for.
Thank you in advance!
Example from the link:
// This
let a = [1, 2, 3];
let doubled: Vec<i32> = a.iter()
.map(|&x| x * 2)
.collect();
assert_eq!(vec![2, 4, 6], doubled);
// Compared to this
use std::collections::VecDeque;
let a = [1, 2, 3];
let doubled: VecDeque<i32> = a.iter().map(|&x| x * 2).collect();
assert_eq!(2, doubled[0]);
assert_eq!(4, doubled[1]);
assert_eq!(6, doubled[2]);