More concise HashMap initialization

后端 未结 3 854
滥情空心
滥情空心 2020-12-24 01:30

I\'m using a HashMap to count the occurrences of different characters in a string:

let text = \"GATTACA\";
let mut counts: HashMap

        
3条回答
  •  借酒劲吻你
    2020-12-24 02:01

    Another way that I see in the official documentation:

    use std::collections::HashMap;
    
    fn main() {
        let timber_resources: HashMap<&str, i32> =
        [("Norway", 100),
         ("Denmark", 50),
         ("Iceland", 10)]
         .iter().cloned().collect();
        // use the values stored in map
    }
    

提交回复
热议问题