Rust語(yǔ)言中的哈希表
哈希表(Hash map)
哈希表也是集合中的一種,也是最常用的集合形式,目前Rust語(yǔ)言核心部分沒(méi)有對(duì)哈希表進(jìn)行實(shí)現(xiàn),是使用標(biāo)準(zhǔn)庫(kù)提供的。
一、新建哈希表
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}
}二、訪問(wèn)某個(gè)元素
索引訪問(wè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!("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
}以上兩種方法都必須保證訪問(wèn)的元素存在,否則會(huì)報(bào)錯(cuò)
二、插入新元素
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}
}這里可以看出,哈希表中的元素是沒(méi)有順序的
三、遍歷哈希表
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四、檢查某個(gè)元素是否存在
Rust語(yǔ)言中檢查哈希表元素是否存在,有兩種方法,
contains_key方法和entry
- contains_key方法用于檢查HashMap中是否包含特定的鍵。它返回一個(gè)布爾值,指示鍵是否存在。
- entry方法用于高效地處理鍵值對(duì)的插入和更新,它返回一個(gè)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 foundentry方法
entry方法多用于對(duì)值的更新,e
or_insert方法在鍵對(duì)應(yīng)的值存在時(shí)就返回這個(gè)值的可變引用,如果不存在則將參數(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}
}到此這篇關(guān)于Rust語(yǔ)言之哈希表的文章就介紹到這了,更多相關(guān)Rust哈希表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Rust中要用Struct和Enum組織數(shù)據(jù)的原因解析
在Rust中,Struct和Enum是組織數(shù)據(jù)的核心工具,Struct用于將相關(guān)字段封裝為單一實(shí)體,便于管理和擴(kuò)展,Enum用于明確定義所有可能的狀態(tài),本文將通過(guò)具體示例,深入探討為什么在Rust中必須使用struct和enum來(lái)管理數(shù)據(jù),感興趣的朋友一起學(xué)習(xí)吧2025-02-02
Rust使用lettre實(shí)現(xiàn)郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了Rust如何使用lettre實(shí)現(xiàn)郵件發(fā)送功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
Rust編譯報(bào)錯(cuò):link.exe未找到問(wèn)題分析及解決
文章介紹了兩個(gè)編譯錯(cuò)誤及其解決方法,第一個(gè)是關(guān)于`link.exe`未找到的問(wèn)題,解決方法是安裝Visual?Studio或構(gòu)建工具,選擇相應(yīng)的C++開(kāi)發(fā)負(fù)載并安裝所需組件,第二個(gè)是關(guān)于`std::str::FromStr`未解析的錯(cuò)誤,在Rust中可能是因?yàn)榘姹净蚺渲脝?wèn)題,但不影響編譯和運(yùn)行2026-04-04
Rust調(diào)用Windows API 如何獲取正在運(yùn)行的全部進(jìn)程信息
本文介紹了如何使用Rust調(diào)用WindowsAPI獲取正在運(yùn)行的全部進(jìn)程信息,通過(guò)引入winapi依賴(lài)并添加相應(yīng)的features,可以實(shí)現(xiàn)對(duì)不同API集的調(diào)用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-11-11
Rust 語(yǔ)言中的dyn 關(guān)鍵字及用途解析
在Rust中,"dyn"關(guān)鍵字用于表示動(dòng)態(tài)分發(fā)(dynamic dispatch),它通常與trait對(duì)象一起使用,以實(shí)現(xiàn)運(yùn)行時(shí)多態(tài), 在Rust中,多態(tài)是通過(guò)trait和impl來(lái)實(shí)現(xiàn)的,這篇文章主要介紹了Rust 語(yǔ)言中的 dyn 關(guān)鍵字,需要的朋友可以參考下2024-03-03
Rust生命周期之驗(yàn)證引用有效性與防止懸垂引用方式
本文介紹了Rust中生命周期注解的應(yīng)用,包括防止懸垂引用、在函數(shù)中使用泛型生命周期、生命周期省略規(guī)則、在結(jié)構(gòu)體中使用生命周期、靜態(tài)生命周期以及如何將生命周期與泛型和特質(zhì)約束結(jié)合,通過(guò)這些機(jī)制,Rust在編譯時(shí)就能捕獲內(nèi)存安全問(wèn)題2025-02-02

