collections/hashmap #172
Replies: 13 comments 9 replies
-
题目2 的答案有误 |
Beta Was this translation helpful? Give feedback.
-
第五题从哪里可以体现出需要自反性呢? |
Beta Was this translation helpful? Give feedback.
-
有点奇怪, 而使用下面iter()代替into_iter()时候反而出现了,上面@quarksb |
Beta Was this translation helpful? Give feedback.
-
1、 use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
//需要保持<key,value>类型一致
scores.insert("Sunface", 98);
scores.insert("Daniel", 95);
scores.insert("Ashley", 69);
scores.insert("Katie", 58);
// get 返回一个 Option<&V> 枚举值
let score = scores.get("Sunface");
assert_eq!(score, Some(&98));
if scores.contains_key("Daniel") {
// 索引返回一个值 V
let score = scores["Daniel"];
assert_eq!(score, 95);
scores.remove("Daniel");
}
assert_eq!(scores.len(), 3);
for (name, score) in scores {
println!("The score of {} is {}", name, score)
}
} 2、 (1) use std::collections::HashMap;
fn main() {
let teams = [
("Chinese Team", 100),
("American Team", 10),
("France Team", 50),
];
let mut teams_map1 = HashMap::new();
for team in &teams {
teams_map1.insert(team.0, team.1);
}
// 使用两种方法实现 team_map2
// 提示:其中一种方法是使用 `collect` 方法
let teams_map2:HashMap<_,_>=teams.into_iter().collect();
assert_eq!(teams_map1, teams_map2);
println!("Success!")
} (2)暴力复制 use std::collections::HashMap;
fn main() {
let teams = [
("Chinese Team", 100),
("American Team", 10),
("France Team", 50),
];
let mut teams_map1 = HashMap::new();
for team in &teams {
teams_map1.insert(team.0, team.1);
}
// 使用两种方法实现 team_map2
// 提示:其中一种方法是使用 `collect` 方法
let teams_map2=teams_map1.clone();
assert_eq!(teams_map1, teams_map2);
println!("Success!")
} 3、理解 // 填空
use std::collections::HashMap;
fn main() {
// 编译器可以根据后续的使用情况帮我自动推断出 HashMap 的类型,当然你也可以显式地标注类型:HashMap<&str, u8>
let mut player_stats = HashMap::new();
// 查询指定的 key, 若不存在时,则插入新的 kv 值
player_stats.entry("health").or_insert(100);
assert_eq!(player_stats["health"], 100);
// 通过函数来返回新的值
player_stats.entry("health").or_insert_with(random_stat_buff);
assert_eq!(player_stats["health"], 100);
let health = player_stats.entry("health").or_insert(50);
assert_eq!(*health, 100);
*health -= 50;
assert_eq!(*health, 50);
println!("Success!")
}
fn random_stat_buff() -> u8 {
// 为了简单,我们没有使用随机,而是返回一个固定的值
42
} 4、 // 修复错误
// 提示: `derive` 是实现一些常用特征的好办法
use std::collections::HashMap;
#[derive(Eq,Hash,PartialEq,Debug)]
struct Viking {
name: String,
country: String,
}
impl Viking {
fn new(name: &str, country: &str) -> Viking {
Viking {
name: name.to_string(),
country: country.to_string(),
}
}
}
fn main() {
// 使用 HashMap 来存储 viking 的生命值
let vikings = HashMap::from([
(Viking::new("Einar", "Norway"), 25),
(Viking::new("Olaf", "Denmark"), 24),
(Viking::new("Harald", "Iceland"), 12),
]);
// 使用 derive 的方式来打印 viking 的当前状态
for (viking, health) in &vikings {
println!("{:?} has {} hp", viking, health);
}
} 5、所有权会被 // 修复错误,尽可能少的去修改代码
// 不要移除任何代码行!
use std::collections::HashMap;
fn main() {
let v1 = 10;
let mut m1 = HashMap::new();
m1.insert(v1, v1);
println!("v1 is still usable after inserting to hashmap : {}", v1);
let v2 = "hello".to_string();
let mut m2 = HashMap::new();
// 所有权在这里发生了转移
m2.insert(v2.clone(), v1);
assert_eq!(v2, "hello");
println!("Success!")
} |
Beta Was this translation helpful? Give feedback.
-
three way to solve question number 2: use std::collections::HashMap; fn main() {
} |
Beta Was this translation helpful? Give feedback.
-
fn random_stat_buff() -> u8 {
// 为了简单,我们没有使用随机,而是返回一个固定的值
42
} 宇宙的终极答案( |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
Day 10 |
Beta Was this translation helpful? Give feedback.
-
collections/hashmap
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/collections/hashmap.html
Beta Was this translation helpful? Give feedback.
All reactions