【Rust】标准库-HashMap
环境
- Rust 1.56.1
- VSCode 1.61.2
概念
参考:https://doc.rust-lang.org/stable/rust-by-example/std/hash.html
示例
HashMap 和其它语言类型,存储键值对。
main.rs
use std::collections::HashMap;
fn call(number: &str) -> &str {
match number {
"798-1364" => {
"We're sorry, the call cannot be completed as dialed.
Please hang up and try again."
}
"645-7689" => {
"Hello, this is Mr. Awesome's Pizza. My name is Fred.
What can I get for you today?"
}
_ => "Hi! Who is this again?",
}
}
fn main() {
let mut contacts = HashMap::new();
contacts.insert("Daniel", "798-1364");
contacts.insert("Ashley", "645-7689");
contacts.insert("Katie", "435-8291");
contacts.insert("Robert", "956-1745");
match contacts.get("Daniel") {
Some(&number) => println!("Calling Daniel: {}", call(number)),
_ => println!("Don't have Daniel's number."),
}
contacts.remove("Ashley");
for (contact, &number) in contacts.iter() {
println!("Calling {}: {}", contact, call(number));
}
}
总结
了解了 Rust 中标准库中的 HashMap 的使用。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!