环境
- 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 的使用。
附录
|
请发表评论