Does Rust have an equivalent to Python's dictionary comprehension syntax?
In Rust you can create Hashmap which is an (Key, Value
) pair, each key is unique and you can't have same key multiple times, the value can exist more than one time.Example of Rust Hasmap
use std::collections::HashMap;
fn main() {
println!(
"{:?}",
(1..5).map(|i| (i + i, i * i)).collect::<HashMap<_, _>>()
);
}
Output
{4: 4, 6: 9, 8: 16, 2: 1}