mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 14:59:18 +00:00
翻譯
This commit is contained in:
parent
afad4b9601
commit
6dc6b4c61a
@ -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)
|
||||
- [Iterator documentation](https://doc.rust-lang.org/stable/std/iter/)
|
||||
- [迭代器](https://doc.rust-lang.org/book/ch13-02-iterators.html)
|
||||
- [迭代器文檔](https://doc.rust-lang.org/stable/std/iter/)
|
||||
|
||||
@ -1,13 +1,10 @@
|
||||
// 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
|
||||
// hint.
|
||||
// 執行 `rustlings hint iterators1` 或使用 `hint` 子命令獲取提示。
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
|
||||
@ -1,15 +1,13 @@
|
||||
// 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
|
||||
// hint.
|
||||
// 執行 `rustlings hint iterators2` 或使用 `hint` 子命令獲取提示。
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// Step 1.
|
||||
// Complete the `capitalize_first` function.
|
||||
// 步驟 1.
|
||||
// 完成 `capitalize_first` 函數。
|
||||
// "hello" -> "Hello"
|
||||
pub fn capitalize_first(input: &str) -> String {
|
||||
let mut c = input.chars();
|
||||
@ -19,17 +17,17 @@ pub fn capitalize_first(input: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2.
|
||||
// Apply the `capitalize_first` function to a slice of string slices.
|
||||
// Return a vector of strings.
|
||||
// 步驟 2.
|
||||
// 將 `capitalize_first` 函數應用於字串切片的切片。
|
||||
// 回傳一個字串向量。
|
||||
// ["hello", "world"] -> ["Hello", "World"]
|
||||
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
// Step 3.
|
||||
// Apply the `capitalize_first` function again to a slice of string slices.
|
||||
// Return a single string.
|
||||
// 步驟 3.
|
||||
// 再次將 `capitalize_first` 函數應用於字串切片的切片。
|
||||
// 回傳一個單一的字串。
|
||||
// ["hello", " ", "world"] -> "Hello World"
|
||||
pub fn capitalize_words_string(words: &[&str]) -> String {
|
||||
String::new()
|
||||
|
||||
@ -1,13 +1,10 @@
|
||||
// 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. Complete the divide function to get the first four tests to pass.
|
||||
// 2. Get the remaining tests to pass by completing the result_with_list and
|
||||
// list_of_results functions.
|
||||
// 這是一個比其他大多數練習都要大的練習!你能做到的!如果你選擇接受它,這是你的任務:
|
||||
// 1. 完成 `divide` 函數,使前四個測試通過。
|
||||
// 2. 完成 `result_with_list` 和 `list_of_results` 函數,使剩餘的測試通過。
|
||||
//
|
||||
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
// 執行 `rustlings hint iterators3` 或使用 `hint` 子命令獲取提示。
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
@ -23,23 +20,21 @@ pub struct NotDivisibleError {
|
||||
divisor: i32,
|
||||
}
|
||||
|
||||
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
||||
// Otherwise, return a suitable error.
|
||||
// 計算 `a` 除以 `b`,如果 `a` 可以被 `b` 整除。
|
||||
// 否則,回傳一個合適的錯誤。
|
||||
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
||||
todo!();
|
||||
}
|
||||
|
||||
// Complete the function and return a value of the correct type so the test
|
||||
// passes.
|
||||
// Desired output: Ok([1, 11, 1426, 3])
|
||||
// 完成該函數並回傳正確類型的值,使測試通過。
|
||||
// 期望的輸出:Ok([1, 11, 1426, 3])
|
||||
fn result_with_list() -> () {
|
||||
let numbers = vec![27, 297, 38502, 81];
|
||||
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.
|
||||
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
|
||||
// 完成該函數並回傳正確類型的值,使測試通過。
|
||||
// 期望的輸出:[Ok(1), Ok(11), Ok(1426), Ok(3)]
|
||||
fn list_of_results() -> () {
|
||||
let numbers = vec![27, 297, 38502, 81];
|
||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||
|
||||
@ -1,20 +1,19 @@
|
||||
// iterators4.rs
|
||||
//
|
||||
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
// 執行 `rustlings hint iterators4` 或使用 `hint` 子命令獲取提示。
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub fn factorial(num: u64) -> u64 {
|
||||
// Complete this function to return the factorial of num
|
||||
// Do not use:
|
||||
// - early returns (using the `return` keyword explicitly)
|
||||
// Try not to use:
|
||||
// - imperative style loops (for, while)
|
||||
// - additional variables
|
||||
// For an extra challenge, don't use:
|
||||
// - recursion
|
||||
// Execute `rustlings hint iterators4` for hints.
|
||||
// 完成此函數以回傳 num 的階乘
|
||||
// 不要使用:
|
||||
// - 提前返回(顯式使用 `return` 關鍵字)
|
||||
// 嘗試不要使用:
|
||||
// - 命令式風格的循環(for、while)
|
||||
// - 其他變量
|
||||
// 額外挑戰,不要使用:
|
||||
// - 遞迴
|
||||
// 執行 `rustlings hint iterators4` 獲取提示。
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@ -1,15 +1,10 @@
|
||||
// iterators5.rs
|
||||
//
|
||||
// Let's define a simple model to track Rustlings exercise progress. Progress
|
||||
// will be modelled using a hash map. The name of the exercise is the key and
|
||||
// the progress is the value. Two counting functions were created to count the
|
||||
// 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.
|
||||
// 定義一個簡單的模型來跟蹤 Rustlings 練習的進度。進度將使用雜湊表來建模。練習的名稱是鍵,進度是值。
|
||||
// 創建了兩個計數函數來計算具有給定進度的練習數量。使用迭代器重新創建此計數功能。嘗試不要使用命令式循環(for、while)。
|
||||
// 只需修改兩個迭代器方法(count_iterator 和 count_collection_iterator)。
|
||||
//
|
||||
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
// 執行 `rustlings hint iterators5` 或使用 `hint` 子命令獲取提示。
|
||||
|
||||
// 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 {
|
||||
// map is a hashmap with String keys and Progress values.
|
||||
// map 是一個具有 String 鍵和 Progress 值的雜湊表。
|
||||
// map = { "variables1": Complete, "from_str": None, ... }
|
||||
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 {
|
||||
// collection is a slice of hashmaps.
|
||||
// collection 是一個雜湊表的切片。
|
||||
// collection = [{ "variables1": Complete, "from_str": None, ... },
|
||||
// { "variables2": Complete, ... }, ... ]
|
||||
todo!();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user