12月 20, 2023 LaGee
HashMap 3
HashMap有幾個常見的刪除資料方法
分別是clear、drain、remove、retain
其中remove又分成remove和remove_entry
可以根據不同的情況選擇想要的刪除方式
pub fn clear(&mut self)
clear最簡單也是最暴力
就是直接把所有東西刪掉
use std::collections::HashMap;
fn main() {
let mut score_map: HashMap<&str, u8> = HashMap::from([("Chinese", 64), ("Math", 58)]);
println!("before clear: {:?}", score_map);
score_map.clear();
println!("after clear: {:?}", score_map);
}
before clear: {"Math": 58, "Chinese": 64}
after clear: {}
原本clear前有Math和Chinese
clear就都沒了
pub fn drain(&mut self) -> Drain<'_, K, V>
drain也會刪除所有東西
但會回傳一個Drain
Drain是一個迭代器
裡面存放Key和Value
use std::collections::HashMap;
fn main() {
let mut score_map: HashMap<&str, u8> = HashMap::from([("Chinese", 64), ("Math", 58)]);
println!("before drain: {:?}", score_map);
for (subject, score) in score_map.drain() {
println!("{}, {}", subject, score);
}
}
before drain: {"Chinese": 64, "Math": 58}
Chinese, 64
Math, 58
透過drain,可以刪除資料的同時把資料放入iter作運用
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
remove分為兩個
一個是remove,另一個是remove_entry
remove為輸入一個key,會把該key刪除
並且回傳該key的Option<Value>
use std::collections::HashMap;
fn main() {
let mut score_map: HashMap<&str, u8> = HashMap::from([("Chinese", 64), ("Math", 58)]);
println!("before remove: {:?}", score_map);
let chinese_score = score_map.remove("Chinese").unwrap();
println!("after remove: {:?}", score_map);
println!("Chinese score: {}", chinese_score);
}
before remove: {"Math": 58, "Chinese": 64}
after remove: {"Math": 58}
Chinese score: 64
pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
remove_entry一樣是輸入key
一樣回傳Option
不同的是Option是一個元組
第一個是key,第二個是value
用程式示範會長這樣
use std::collections::HashMap;
fn main() {
let mut score_map: HashMap<&str, u8> = HashMap::from([("Chinese", 64), ("Math", 58)]);
println!("before remove_entry: {:?}", score_map);
let chinese_score = score_map.remove_entry("Chinese").unwrap();
println!("after remove_entry: {:?}", score_map);
println!("{} score: {}", chinese_score.0, chinese_score.1);
}
before remove_entry: {"Math": 58, "Chinese": 64}
after remove_entry: {"Math": 58}
Chinese score: 64
最後可以同時獲得key 和 value
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&K, &mut V) -> bool,
retain為使用閉包
可以有條件的刪除想要的數據
例如我想要刪除分數60分以下的
可以這樣打
use std::collections::HashMap;
fn main() {
let mut score_map: HashMap<&str, u8> =
HashMap::from([("Chinese", 64), ("Math", 58), ("English", 80)]);
println!("before retain: {:?}", score_map);
score_map.retain(|k,v| *v > 60);
println!("after retain: {:?}", score_map);
}
before retain: {"Chinese": 64, "Math": 58, "English": 80}
after retain: {"Chinese": 64, "English": 80}
符合條件的數據會留下
而條件以外的會刪除
相關文章
Rust HashMap概述1 - key與value集合 ~ LaGee-Blog (lageeblog.blogspot.com)
Rust HashMap概述2 - get、get_key_value、雙重HashMap ~ LaGee-Blog (lageeblog.blogspot.com)
參考資料
[1] https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.clear
[2] https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.drain
[3] https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.remove
[4] https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.remove_entry
[5] https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.retain
0 comments:
張貼留言