HashMap
HashMap
HashMap::new()
创建一个新的.insert(key,value)
插入项目。
use std::collections::HashMap; // This is so we can just write HashMap instead of std::collections::HashMap every time
struct City {
name: String,
population: HashMap<u32, u32>, // This will have the year and the population for the year
}
fn main() {
let mut tallinn = City {
name: "Tallinn".to_string(),
population: HashMap::new(), // So far the HashMap is empty
};
tallinn.population.insert(1372, 3_250); // insert three dates
tallinn.population.insert(1851, 24_000);
tallinn.population.insert(2020, 437_619);
for (year, population) in tallinn.population { // The HashMap is HashMap<u32, u32> so it returns a two items each time
println!("In the year {} the city of {} had a population of {}.", year, tallinn.name, population);
}
}
In the year 1372 the city of Tallinn had a population of 3250.
In the year 2020 the city of Tallinn had a population of 437619.
In the year 1851 the city of Tallinn had a population of 24000.
#[derive(PartialEq, Eq, Hash)]
这样,即可将你的类型转换成一个可以作为Hash
这个
-
- 如果
Key1==Key2 , 那么一定有Hash(Key1) == Hash(Key2)
- 如果
-
- 你的
Hash 函数本身不能改变你的Key 值,否则将会引发一个逻辑错误(很难排查,遇到就完的那种)
- 你的
增删改查
use std::collections::HashMap;
// 声明
let mut come_from = HashMap::new();
// 插入
come_from.insert("WaySLOG", "HeBei");
come_from.insert("Marisa", "U.S.");
come_from.insert("Mike", "HuoGuo");
// 查找key
if !come_from.contains_key("elton") {
println!("Oh, 我们查到了{}个人,但是可怜的Elton猫还是无家可归", come_from.len());
}
// 根据key删除元素
come_from.remove("Mike");
println!("Mike猫的家乡不是火锅!不是火锅!不是火锅!虽然好吃!");
// 利用get的返回判断元素是否存在
let who = ["MoGu", "Marisa"];
for person in &who {
match come_from.get(person) {
Some(location) => println!("{} 来自: {}", person, location),
None => println!("{} 也无家可归啊.", person),
}
}
// 遍历输出
println!("那么,所有人呢?");
for (name, location) in &come_from {
println!("{}来自: {}", name, location);
}
Entry
entry
的
use std::collections::HashMap;
let mut letters = HashMap::new();
for ch in "a short treatise on fungi".chars() {
let counter = letters.entry(ch).or_insert(0);
*counter += 1;
}
assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);
BTreeMap
如果您希望可以对
use std::collections::BTreeMap; // Just change HashMap to BTreeMap
struct City {
name: String,
population: BTreeMap<u32, u32>, // Just change HashMap to BTreeMap
}
fn main() {
let mut tallinn = City {
name: "Tallinn".to_string(),
population: BTreeMap::new(), // Just change HashMap to BTreeMap
};
tallinn.population.insert(1372, 3_250);
tallinn.population.insert(1851, 24_000);
tallinn.population.insert(2020, 437_619);
for (year, population) in tallinn.population {
println!("In the year {} the city of {} had a population of {}.", year, tallinn.name, population);
}
}
In the year 1372 the city of Tallinn had a population of 3250.
In the year 1851 the city of Tallinn had a population of 24000.
In the year 2020 the city of Tallinn had a population of 437619.