I was following some code examples on the Rust CSV crate's tutorial page, and the tutorial doesn't explain how this works:
type Record = HashMap<String, String>;
fn csv_to_record_table() -> Result<Vec<Record>, Box<dyn Error>> {
let mut rdr = csv::Reader::from_path(r"h:\code\rust\csvtutorial\uspop.csv")?;
let mut sheet = Vec::<Record>::new();
for result in rdr.deserialize() {
let record = result?;
sheet.push(record);
}
Ok(sheet)
}
My code works, so I don't need a fix, but what I'm trying to understand is how rdr.deserialize()
is able to infer that the type I want is HashMap<String, String>
. I get from the documentation that if you pass it a hashmap, then it knows to use the column name as the key, and the field data as the value.
What I don't get is how rdr.deserialize()
knows that that's what I'm trying to do without me rewriting that as rdr.deserialize::<Record>()
. Is this a feature of Rust? Or is this something specific to Serde? How does it also know that I'm not trying to give it a Vec<Record>
?