Rust – HashMaps
HashMap 的概念几乎存在于Java、C++、 Python等所有编程语言中,它具有键值对,通过键,我们可以获取映射的值。键是唯一的,键中不允许重复,但值可以重复。
1. 在 HashMap 中插入:
要在 Rust 中的 HashMap 中插入一个值,请按照以下步骤操作:
- 导入哈希映射
- 创建名为gfg 的HashMap
- 使用gfg.insert(key, value)插入记录
句法 :
// import HashMap
use std::collections::HashMap;
// initialize the HashMap
let mut gfg=HashMap::new();
//insert the values In HashMap
gfg.insert("Data Structures","90");
Rust
// Inserting in HashMap Rust
use std::collections::HashMap;
fn main() {
// initialize the HashMap
// mut means we can reasign to something else
let mut gfg=HashMap::new();
// inserting records
gfg.insert("Data Structures","90");
gfg.insert("Algorithms","99");
gfg.insert("Interview Preparations","100");
gfg.insert("FAANG","97");
gfg.insert("CP","99");
// for printing all the records "{:?}" this is must
println!("{:?}",gfg );
}
Rust
// Rust Program to Iterating over HashMap
// import HashMap
use std::collections::HashMap;
fn main() {
// create HashMap
let mut gfg=HashMap::new();
// inserting over
gfg.insert("Data Structures","90");
gfg.insert("Algorithms","99");
gfg.insert("Interview Preparations","100");
gfg.insert("FAANG","97");
gfg.insert("CP","99");
// iterating using iter() method on gfg
for (key, val) in gfg.iter() {
println!("{} {}", key, val);
}
}
Rust
// Rust program for contains Key
// importing hashmap
use std::collections::HashMap;
fn main() {
// creating hashMap
// mut means we can reassign gfg
let mut gfg=HashMap::new();
gfg.insert("Data Structures","90");
gfg.insert("Algorithms","99");
gfg.insert("Interview Preparations","100");
gfg.insert("FAANG","97");
gfg.insert("CP","99");
// contains_key() method to check
// if key present or not
if gfg.contains_key( & "FAANG")
{
println!("yes it contains the given key well done gfg");
}
}
Rust
// Rust program for length of HashMap
// importing hashmap
use std::collections::HashMap;
fn main() {
// creating hashMap
// mut means we can reassign gfg
let mut gfg=HashMap::new();
gfg.insert("Data Structures","90");
gfg.insert("Algorithms","99");
gfg.insert("Interview Preparations","100");
gfg.insert("FAANG","97");
gfg.insert("CP","99");
// length of HashMap using len()
println!("len of gfg HashMap={}",gfg.len());
}
Rust
// Rust program for removing from HashMap
// importing hashmap
use std::collections::HashMap;
fn main() {
// creating hashMap
// mut means we can reassign gfg
let mut gfg=HashMap::new();
gfg.insert("Data Structures","90");
gfg.insert("Algorithms","99");
gfg.insert("Interview Preparations","100");
gfg.insert("FAANG","97");
gfg.insert("CP","99");
// remove using remove() method
gfg.remove( & "CP");
println!("len of gfg HashMap={}",gfg.len());
}
Rust
// Rust program for getting value by key
// importing hashmap
use std::collections::HashMap;
fn main() {
// creating hashMap
// mut means we can reassign gfg
let mut gfg=HashMap::new();
gfg.insert("Data Structures","90");
gfg.insert("Algorithms","99");
gfg.insert("Interview Preparations","100");
gfg.insert("FAANG","97");
gfg.insert("CP","99");
let value= gfg.get(&"Algorithms");
println!("value={:?}",value)
}
输出 :
{"CP": "99", "Algorithms": "99",
"Data Structures": "90",
"FAANG": "97",
"Interview Preparations": "100"}
2. 迭代 HashMap :
要在 Rust 中迭代 HashMap,请按照以下步骤操作:
- 导入哈希映射
- 在 HashMap 中插入记录
- 使用带有 for 循环的iter () 方法进行迭代
句法 :
// here gfg is HashMap
for (key, val) in gfg.iter() {
println!("{} {}", key, val);
}
锈
// Rust Program to Iterating over HashMap
// import HashMap
use std::collections::HashMap;
fn main() {
// create HashMap
let mut gfg=HashMap::new();
// inserting over
gfg.insert("Data Structures","90");
gfg.insert("Algorithms","99");
gfg.insert("Interview Preparations","100");
gfg.insert("FAANG","97");
gfg.insert("CP","99");
// iterating using iter() method on gfg
for (key, val) in gfg.iter() {
println!("{} {}", key, val);
}
}
输出 :
Algorithms 99
FAANG 97
CP 99
Interview Preparations 100
Data Structures 90
3. 检查钥匙:
要检查 HashMap 中是否存在键,请按照以下步骤操作:
- 导入哈希映射
- 在 HashMap 中插入记录
- 使用 contains_key(& key) 检查密钥是否存在
句法 :
if gfg.contains_key( & "FAANG")
{
println!("yes it contains the given key well done gfg");
}
锈
// Rust program for contains Key
// importing hashmap
use std::collections::HashMap;
fn main() {
// creating hashMap
// mut means we can reassign gfg
let mut gfg=HashMap::new();
gfg.insert("Data Structures","90");
gfg.insert("Algorithms","99");
gfg.insert("Interview Preparations","100");
gfg.insert("FAANG","97");
gfg.insert("CP","99");
// contains_key() method to check
// if key present or not
if gfg.contains_key( & "FAANG")
{
println!("yes it contains the given key well done gfg");
}
}
输出 :
yes it contains the given key well done gfg
4. HashMap 的长度:
要在 Rust 中检查 HashMap 的长度,请按照以下步骤操作:
- 导入哈希映射
- 在 HashMap 中插入记录
- 在HashMap上使用len ()方法获取HashMap的长度
句法 :
//for length of HashMap
gfg.len();
锈
// Rust program for length of HashMap
// importing hashmap
use std::collections::HashMap;
fn main() {
// creating hashMap
// mut means we can reassign gfg
let mut gfg=HashMap::new();
gfg.insert("Data Structures","90");
gfg.insert("Algorithms","99");
gfg.insert("Interview Preparations","100");
gfg.insert("FAANG","97");
gfg.insert("CP","99");
// length of HashMap using len()
println!("len of gfg HashMap={}",gfg.len());
}
输出 :
len of gfg HashMap=5
5. 从 Hashmap 中删除:
要从 Hashmap 中删除元素:
- 导入哈希映射。
- 在 HashMap 中插入记录。
- 使用remove (key) 方法从 HashMap 中删除。
句法 :
// length of HashMap
// here gfg is HashMap
gfg.remove(& "key")
锈
// Rust program for removing from HashMap
// importing hashmap
use std::collections::HashMap;
fn main() {
// creating hashMap
// mut means we can reassign gfg
let mut gfg=HashMap::new();
gfg.insert("Data Structures","90");
gfg.insert("Algorithms","99");
gfg.insert("Interview Preparations","100");
gfg.insert("FAANG","97");
gfg.insert("CP","99");
// remove using remove() method
gfg.remove( & "CP");
println!("len of gfg HashMap={}",gfg.len());
}
输出 :
len of gfg HashMap=4
6. 使用HashMap 中的键获取值:
要使用键访问 HashMap 中的值:
- 导入哈希映射。
- 在 HashMap 中插入记录。
- 使用get ( & key) 获取值。
句法 :
// for getting through key
gfg.get( & key)
锈
// Rust program for getting value by key
// importing hashmap
use std::collections::HashMap;
fn main() {
// creating hashMap
// mut means we can reassign gfg
let mut gfg=HashMap::new();
gfg.insert("Data Structures","90");
gfg.insert("Algorithms","99");
gfg.insert("Interview Preparations","100");
gfg.insert("FAANG","97");
gfg.insert("CP","99");
let value= gfg.get(&"Algorithms");
println!("value={:?}",value)
}
输出 :
value=Some("99")