mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-29 07:19:17 +00:00
152 lines
4.3 KiB
Rust
152 lines
4.3 KiB
Rust
// iterators5.rs
|
||
//
|
||
// 定義一個簡單的模型來跟蹤 Rustlings 練習的進度。進度將使用雜湊表來建模。練習的名稱是鍵,進度是值。
|
||
// 創建了兩個計數函數來計算具有給定進度的練習數量。使用迭代器重新創建此計數功能。嘗試不要使用命令式循環(for、while)。
|
||
// 只需修改兩個迭代器方法(count_iterator 和 count_collection_iterator)。
|
||
//
|
||
// 執行 `rustlings hint iterators5` 或使用 `hint` 子命令獲取提示。
|
||
|
||
// I AM NOT DONE
|
||
|
||
use std::collections::HashMap;
|
||
|
||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||
enum Progress {
|
||
None,
|
||
Some,
|
||
Complete,
|
||
}
|
||
|
||
fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||
let mut count = 0;
|
||
for val in map.values() {
|
||
if val == &value {
|
||
count += 1;
|
||
}
|
||
}
|
||
count
|
||
}
|
||
|
||
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||
// map 是一個具有 String 鍵和 Progress 值的雜湊表。
|
||
// map = { "variables1": Complete, "from_str": None, ... }
|
||
todo!();
|
||
}
|
||
|
||
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||
let mut count = 0;
|
||
for map in collection {
|
||
for val in map.values() {
|
||
if val == &value {
|
||
count += 1;
|
||
}
|
||
}
|
||
}
|
||
count
|
||
}
|
||
|
||
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||
// collection 是一個雜湊表的切片。
|
||
// collection = [{ "variables1": Complete, "from_str": None, ... },
|
||
// { "variables2": Complete, ... }, ... ]
|
||
todo!();
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn count_complete() {
|
||
let map = get_map();
|
||
assert_eq!(3, count_iterator(&map, Progress::Complete));
|
||
}
|
||
|
||
#[test]
|
||
fn count_some() {
|
||
let map = get_map();
|
||
assert_eq!(1, count_iterator(&map, Progress::Some));
|
||
}
|
||
|
||
#[test]
|
||
fn count_none() {
|
||
let map = get_map();
|
||
assert_eq!(2, count_iterator(&map, Progress::None));
|
||
}
|
||
|
||
#[test]
|
||
fn count_complete_equals_for() {
|
||
let map = get_map();
|
||
let progress_states = vec![Progress::Complete, Progress::Some, Progress::None];
|
||
for progress_state in progress_states {
|
||
assert_eq!(
|
||
count_for(&map, progress_state),
|
||
count_iterator(&map, progress_state)
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn count_collection_complete() {
|
||
let collection = get_vec_map();
|
||
assert_eq!(
|
||
6,
|
||
count_collection_iterator(&collection, Progress::Complete)
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn count_collection_some() {
|
||
let collection = get_vec_map();
|
||
assert_eq!(1, count_collection_iterator(&collection, Progress::Some));
|
||
}
|
||
|
||
#[test]
|
||
fn count_collection_none() {
|
||
let collection = get_vec_map();
|
||
assert_eq!(4, count_collection_iterator(&collection, Progress::None));
|
||
}
|
||
|
||
#[test]
|
||
fn count_collection_equals_for() {
|
||
let progress_states = vec![Progress::Complete, Progress::Some, Progress::None];
|
||
let collection = get_vec_map();
|
||
|
||
for progress_state in progress_states {
|
||
assert_eq!(
|
||
count_collection_for(&collection, progress_state),
|
||
count_collection_iterator(&collection, progress_state)
|
||
);
|
||
}
|
||
}
|
||
|
||
fn get_map() -> HashMap<String, Progress> {
|
||
use Progress::*;
|
||
|
||
let mut map = HashMap::new();
|
||
map.insert(String::from("variables1"), Complete);
|
||
map.insert(String::from("functions1"), Complete);
|
||
map.insert(String::from("hashmap1"), Complete);
|
||
map.insert(String::from("arc1"), Some);
|
||
map.insert(String::from("as_ref_mut"), None);
|
||
map.insert(String::from("from_str"), None);
|
||
|
||
map
|
||
}
|
||
|
||
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
|
||
use Progress::*;
|
||
|
||
let map = get_map();
|
||
|
||
let mut other = HashMap::new();
|
||
other.insert(String::from("variables2"), Complete);
|
||
other.insert(String::from("functions2"), Complete);
|
||
other.insert(String::from("if1"), Complete);
|
||
other.insert(String::from("from_into"), None);
|
||
other.insert(String::from("try_from_into"), None);
|
||
|
||
vec![map, other]
|
||
}
|
||
}
|