Rust中的collections
collections
HashMap
概述: 用于存储键值对的数据结构,底层实现是哈希表,因此是无序的。使用开放寻址法处理冲突。
插入数据:
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
scores.insert("Alice", 50);
scores.insert("Bob", 60);
scores.insert("Charlie", 70);
println!("scores: {:?}", scores);
}
如果insert的key已经存在,那就会更新value
获取值:
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
scores.insert("Alice", 50);
scores.insert("Bob", 60);
scores.insert("Charlie", 70);
match scores.get("Bob") {
Some(&score) => println!("Bob's score: {}", score),
None => println!("Bob not found"),
}
}
如果key存在,则返回对应的值的引用;否则返回None
检查key是否存在:
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
scores.insert("Alice", 50);
scores.insert("Bob", 60);
scores.insert("Charlie", 70);
if scores.contains_key("Alice") {
println!("Alice is in the map");
} else {
println!("Alice is not in the map");
}
}
移除元素:
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
scores.insert("Alice", 50);
scores.insert("Bob", 60);
scores.insert("Charlie", 70);
if let Some(score) = scores.remove("Alice") {
println!("Removed Alice with score: {}", score);
} else {
println!("Alice not found");
}
println!("scores: {:?}", scores);
}
删除成功返回该键对应的值(Some(value)),否则返回None
遍历HashMap:
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
scores.insert("Alice", 50);
scores.insert("Bob", 60);
scores.insert("Charlie", 70);
for (key, value) in scores {
println!("{}: {}", key, value);
}
}
获取或插入值:
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
// 如果键不存在,插入一个默认值
scores.entry("Alice").or_insert(50);
println!("socres: {:?}", scores);
// 如果键已经存在,修改它的值
*scores.entry("Alice").or_insert(0) += 10;
println!("scores: {:?}", scores);
}
在键不存在时插入默认值,或者获取某个键的值并进行修改
LinkedList
概述: 双向链表,在插入和删除元素时不会导致元素的移动(相对于Vec),从而提供了更好的性能,特别是对大规模数据结构
创建LinkedList:
use std::collections::LinkedList;
fn main() {
// 正常创建
let list: LinkedList<i32> = LinkedList::new();
println!("{:?}", list);
// 从现有的Vec创建
let vec = vec![1, 2, 3, 4, 5];
let list2 = vec.into_iter().collect::<LinkedList<i32>>();
println!("{:?}", list2);
}
插入元素:
use std::collections::LinkedList;
fn main() {
let mut list = LinkedList::new();
// 插入到list前面
list.push_front(1);
// 插入到list后面
list.push_back(2);
println!("{:?}", list);
}
删除元素:
use std::collections::LinkedList;
fn main() {
let mut list = LinkedList::new();
list.push_back(1);
list.push_back(2);
list.push_back(3);
// 删除前面的元素
let mut removed = list.pop_front();
println!("Removed: {:?}", removed);
// 删除后面的元素
removed = list.pop_back();
println!("Removed: {:?}", removed);
println!("{:?}", list);
}
访问元素:
use std::collections::LinkedList;
fn main() {
let mut list = LinkedList::new();
list.push_back(1);
list.push_back(2);
list.push_back(3);
// 通过迭代器遍历元素
for value in &list {
print!("{} ", value);
}
println!();
// 获取头尾元素
if let Some(front) = list.front() {
println!("Front: {}", front);
}
if let Some(back) = list.back() {
println!("Back: {}", back);
}
}