add Rust codes for hash_map (#751)

This commit is contained in:
Kato0130 2023-09-14 14:09:16 -04:00 committed by GitHub
parent cb9c14f5ff
commit eba015c0bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -239,7 +239,26 @@
=== "Rust" === "Rust"
```rust title="hash_map.rs" ```rust title="hash_map.rs"
use std::collections::HashMap;
/* 初始化哈希表 */
let mut map: HashMap<i32, String> = HashMap::new();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.insert(12836, "小哈".to_string());
map.insert(15937, "小啰".to_string());
map.insert(16750, "小算".to_string());
map.insert(13279, "小法".to_string());
map.insert(10583, "小鸭".to_string());
/* 查询操作 */
// 向哈希表中输入键 key ,得到值 value
let _name: Option<&String> = map.get(&15937);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
let _removed_value: Option<String> = map.remove(&10583);
``` ```
=== "C" === "C"
@ -420,7 +439,21 @@
=== "Rust" === "Rust"
```rust title="hash_map.rs" ```rust title="hash_map.rs"
/* 遍历哈希表 */
// 遍历键值对 Key->Value
for (key, value) in &map {
println!("{key} -> {value}");
}
// 单独遍历键 Key
for key in map.keys() {
println!("{key}");
}
// 单独遍历值 Value
for value in map.values() {
println!("{value}");
}
``` ```
=== "C" === "C"