Rust語言中的哈希表
哈希表(Hash map)
哈希表也是集合中的一種,也是最常用的集合形式,目前Rust語言核心部分沒有對哈希表進行實現(xiàn),是使用標準庫提供的。
一、新建哈希表
fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); println!("{:?}",scores); //{"Yellow": 50, "Blue": 10} }
二、訪問某個元素
索引訪問
fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); println!("value: {}",scores["Blue"]); // 10 }
GET方法
官方教程上的方法
fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); let team_name = String::from("Blue"); let score = scores.get(&team_name).copied().unwrap_or(0); println!("value: {}",score); // 10 }
以上兩種方法都必須保證訪問的元素存在,否則會報錯
二、插入新元素
fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); println!("{:?}",scores); //{"Yellow": 50, "Blue": 10} scores.insert(String::from("Red"), 100); println!("{:?}",scores);// {"Red": 100, "Yellow": 50, "Blue": 10} }
這里可以看出,哈希表中的元素是沒有順序的
三、遍歷哈希表
fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); for (key, value) in &scores { println!("{key}: {value}"); } } => Yellow: 50 Blue: 10
四、檢查某個元素是否存在
Rust語言中檢查哈希表元素是否存在,有兩種方法,
contains_key
方法和entry
- contains_key方法用于檢查HashMap中是否包含特定的鍵。它返回一個布爾值,指示鍵是否存在。
- entry方法用于高效地處理鍵值對的插入和更新,它返回一個Entry枚舉,可以是Occupied(鍵已存在)或Vacant(鍵不存在)。
contains_key方法
fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); if scores.contains_key("Red"){ println!("value :{}",scores["Red"]); }else { println!("Red is not found") } } =>Red is not found
entry方法
entry方法多用于對值的更新,e
or_insert
方法在鍵對應的值存在時就返回這個值的可變引用,如果不存在則將參數(shù)作為新值插入并返回新值的可變引用
fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); scores.entry(String::from("Red")).or_insert(100); scores.entry(String::from("Blue")).or_insert(50); println!("{:?}", scores);//{"Blue": 10, "Red": 100, "Yellow": 50} }
五、元素更新
使用contains_key+insert 的方法
fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); let team_list =["Blue","Red"]; for i in team_list{ if scores.contains_key(i){ scores.insert(i.to_string(), scores[i]+50); }else{ scores.insert(i.to_string(), 50); } } println!("{:?}",scores);//{"Red": 50, "Blue": 60, "Yellow": 50} }
使用entry方法
fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); let team_list =["Blue","Red"]; for i in team_list{ let count = scores.entry(i.to_string()).or_insert(0); *count += 50; } println!("{:?}",scores);//{"Red": 50, "Blue": 60, "Yellow": 50} }
相比使用
contains_key+insert
的方法,這種方法更優(yōu)雅。
六、刪除元素
fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); scores.insert(String::from("Red"), 80); println!("{:?}",scores);//{"Blue": 10, "Yellow": 50, "Red": 80} scores.remove("Red"); println!("{:?}",scores);//{"Blue": 10, "Yellow": 50} }
到此這篇關于Rust語言之哈希表的文章就介紹到這了,更多相關Rust哈希表內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
深入探究在Rust中函數(shù)、方法和關聯(lián)函數(shù)有什么區(qū)別
在 Rust 中,函數(shù)、方法和關聯(lián)函數(shù)都是用來封裝行為的,它們之間的區(qū)別主要在于它們的定義和調用方式,本文將通過一個簡單的rust代碼示例來給大家講講Rust中函數(shù)、方法和關聯(lián)函數(shù)區(qū)別,需要的朋友可以參考下2023-08-08Windows系統(tǒng)下安裝Rust環(huán)境超詳細教程
這篇文章主要介紹了如何在Windows系統(tǒng)上安裝mingw64和Rust,mingw64是一個輕便的C語言編譯環(huán)境,可以替代Rust默認使用的Visual?Studio,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2025-02-02