- HashMap
- from
- keys
Create immutable hash with data
- We can also create a HashMap from existing data. In this case the hash does not have to be mutable.
examples/hashes/create-hash-with-data/src/main.rs
use std::collections::HashMap; fn main() { let counter = HashMap::from([("foo", 1), ("bar", 2)]); println!("{:?}", counter); println!("{:?}", counter.keys()); // counter.insert("other", 3); // cannot borrow `counter` as mutable, as it is not declared as mutable }
{"foo": 1, "bar": 2} ["foo", "bar"]