This commit is contained in:
TimLai666 2024-06-18 18:10:13 +08:00 committed by GitHub
parent afad4b9601
commit 6dc6b4c61a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 44 additions and 60 deletions

View File

@ -1,8 +1,8 @@
# Iterators # 迭代器
This section will teach you about Iterators. 本章將教您關於迭代器的知識。
## Further information ## 進一步了解
- [Iterator](https://doc.rust-lang.org/book/ch13-02-iterators.html) - [迭代器](https://doc.rust-lang.org/book/ch13-02-iterators.html)
- [Iterator documentation](https://doc.rust-lang.org/stable/std/iter/) - [迭代器文檔](https://doc.rust-lang.org/stable/std/iter/)

View File

@ -1,13 +1,10 @@
// iterators1.rs // iterators1.rs
// //
// When performing operations on elements within a collection, iterators are // 當對集合中的元素執行操作時,迭代器是必不可少的。此模組幫助您熟悉使用迭代器的結構以及如何遍歷可迭代集合中的元素。
// essential. This module helps you get familiar with the structure of using an
// iterator and how to go through elements within an iterable collection.
// //
// Make me compile by filling in the `???`s // 填寫 `???` 使我編譯通過
// //
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a // 執行 `rustlings hint iterators1` 或使用 `hint` 子命令獲取提示。
// hint.
// I AM NOT DONE // I AM NOT DONE

View File

@ -1,15 +1,13 @@
// iterators2.rs // iterators2.rs
// //
// In this exercise, you'll learn some of the unique advantages that iterators // 在這個練習中,你將學習迭代器所能提供的一些獨特優勢。按照步驟完成這個練習。
// can offer. Follow the steps to complete the exercise.
// //
// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a // 執行 `rustlings hint iterators2` 或使用 `hint` 子命令獲取提示。
// hint.
// I AM NOT DONE // I AM NOT DONE
// Step 1. // 步驟 1.
// Complete the `capitalize_first` function. // 完成 `capitalize_first` 函數。
// "hello" -> "Hello" // "hello" -> "Hello"
pub fn capitalize_first(input: &str) -> String { pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars(); let mut c = input.chars();
@ -19,17 +17,17 @@ pub fn capitalize_first(input: &str) -> String {
} }
} }
// Step 2. // 步驟 2.
// Apply the `capitalize_first` function to a slice of string slices. // 將 `capitalize_first` 函數應用於字串切片的切片。
// Return a vector of strings. // 回傳一個字串向量。
// ["hello", "world"] -> ["Hello", "World"] // ["hello", "world"] -> ["Hello", "World"]
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> { pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
vec![] vec![]
} }
// Step 3. // 步驟 3.
// Apply the `capitalize_first` function again to a slice of string slices. // 再次將 `capitalize_first` 函數應用於字串切片的切片。
// Return a single string. // 回傳一個單一的字串。
// ["hello", " ", "world"] -> "Hello World" // ["hello", " ", "world"] -> "Hello World"
pub fn capitalize_words_string(words: &[&str]) -> String { pub fn capitalize_words_string(words: &[&str]) -> String {
String::new() String::new()

View File

@ -1,13 +1,10 @@
// iterators3.rs // iterators3.rs
// //
// This is a bigger exercise than most of the others! You can do it! Here is // 這是一個比其他大多數練習都要大的練習!你能做到的!如果你選擇接受它,這是你的任務:
// your mission, should you choose to accept it: // 1. 完成 `divide` 函數,使前四個測試通過。
// 1. Complete the divide function to get the first four tests to pass. // 2. 完成 `result_with_list` 和 `list_of_results` 函數,使剩餘的測試通過。
// 2. Get the remaining tests to pass by completing the result_with_list and
// list_of_results functions.
// //
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a // 執行 `rustlings hint iterators3` 或使用 `hint` 子命令獲取提示。
// hint.
// I AM NOT DONE // I AM NOT DONE
@ -23,23 +20,21 @@ pub struct NotDivisibleError {
divisor: i32, divisor: i32,
} }
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`. // 計算 `a` 除以 `b`,如果 `a` 可以被 `b` 整除。
// Otherwise, return a suitable error. // 否則,回傳一個合適的錯誤。
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> { pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
todo!(); todo!();
} }
// Complete the function and return a value of the correct type so the test // 完成該函數並回傳正確類型的值,使測試通過。
// passes. // 期望的輸出Ok([1, 11, 1426, 3])
// Desired output: Ok([1, 11, 1426, 3])
fn result_with_list() -> () { fn result_with_list() -> () {
let numbers = vec![27, 297, 38502, 81]; let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27)); let division_results = numbers.into_iter().map(|n| divide(n, 27));
} }
// Complete the function and return a value of the correct type so the test // 完成該函數並回傳正確類型的值,使測試通過。
// passes. // 期望的輸出:[Ok(1), Ok(11), Ok(1426), Ok(3)]
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
fn list_of_results() -> () { fn list_of_results() -> () {
let numbers = vec![27, 297, 38502, 81]; let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27)); let division_results = numbers.into_iter().map(|n| divide(n, 27));

View File

@ -1,20 +1,19 @@
// iterators4.rs // iterators4.rs
// //
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a // 執行 `rustlings hint iterators4` 或使用 `hint` 子命令獲取提示。
// hint.
// I AM NOT DONE // I AM NOT DONE
pub fn factorial(num: u64) -> u64 { pub fn factorial(num: u64) -> u64 {
// Complete this function to return the factorial of num // 完成此函數以回傳 num 的階乘
// Do not use: // 不要使用:
// - early returns (using the `return` keyword explicitly) // - 提前返回(顯式使用 `return` 關鍵字)
// Try not to use: // 嘗試不要使用:
// - imperative style loops (for, while) // - 命令式風格的循環for、while
// - additional variables // - 其他變量
// For an extra challenge, don't use: // 額外挑戰,不要使用:
// - recursion // - 遞迴
// Execute `rustlings hint iterators4` for hints. // 執行 `rustlings hint iterators4` 獲取提示。
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,15 +1,10 @@
// iterators5.rs // iterators5.rs
// //
// Let's define a simple model to track Rustlings exercise progress. Progress // 定義一個簡單的模型來跟蹤 Rustlings 練習的進度。進度將使用雜湊表來建模。練習的名稱是鍵,進度是值。
// will be modelled using a hash map. The name of the exercise is the key and // 創建了兩個計數函數來計算具有給定進度的練習數量。使用迭代器重新創建此計數功能。嘗試不要使用命令式循環for、while
// the progress is the value. Two counting functions were created to count the // 只需修改兩個迭代器方法count_iterator 和 count_collection_iterator
// number of exercises with a given progress. Recreate this counting
// functionality using iterators. Try not to use imperative loops (for, while).
// Only the two iterator methods (count_iterator and count_collection_iterator)
// need to be modified.
// //
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a // 執行 `rustlings hint iterators5` 或使用 `hint` 子命令獲取提示。
// hint.
// I AM NOT DONE // I AM NOT DONE
@ -33,7 +28,7 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
} }
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize { fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
// map is a hashmap with String keys and Progress values. // map 是一個具有 String 鍵和 Progress 值的雜湊表。
// map = { "variables1": Complete, "from_str": None, ... } // map = { "variables1": Complete, "from_str": None, ... }
todo!(); todo!();
} }
@ -51,7 +46,7 @@ fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progres
} }
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize { fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
// collection is a slice of hashmaps. // collection 是一個雜湊表的切片。
// collection = [{ "variables1": Complete, "from_str": None, ... }, // collection = [{ "variables1": Complete, "from_str": None, ... },
// { "variables2": Complete, ... }, ... ] // { "variables2": Complete, ... }, ... ]
todo!(); todo!();