在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1. new 2. 所有权 3. get 4. iterator 5. insert 6. or_insert ============================================================== 1. new let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); 2. 所有权 let key: String = String::from("key1"); let value: String = String::from("value2"); let mut scores = HashMap::new(); // 对于像 String 这样拥有所有权的值,其值将被移动而哈希 map 会成为这些值的所有者 scores.insert(key, value); // println!("{},{}",key,value); // key和value不再有效 3. get let mut scores = HashMap::new(); scores.insert(String::from("name"), String::from("Tom Smith")); let k = String::from("name"); let v: Option<&String> = scores.get(&k); match v { Some(x) => println!("{}",x), None => println!("none value"), } 4. iterator let mut scores = HashMap::new(); scores.insert(String::from("name"), String::from("Tom Smith")); for (key, value) in &scores { println!("{}: {}", key, value); } 5. insert let mut scores = HashMap::new(); scores.insert(String::from("name"), String::from("Tom Smith")); scores.insert(String::from("name"), String::from("Tom Smith 2"));// 使用新值覆盖老值 println!("{:?}", scores); 6. or_insert let mut scores = HashMap::new(); scores.insert(String::from("name"), String::from("Tom Smith")); scores.entry(String::from("name")).or_insert(String::from("Tom Smith 2"));// 不存在时才插入,存在返回这个值的可变引用 println!("{:?}", scores);
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论