From 973e0f7cac0c71891c52158841c590be288bc648 Mon Sep 17 00:00:00 2001 From: mg-chao Date: Wed, 12 Jan 2022 21:42:47 +0800 Subject: [PATCH] update: translate collections --- exercise/exercises/collections/README.md | 27 +++++++++------------ exercise/exercises/collections/hashmap1.rs | 20 +++++++--------- exercise/exercises/collections/hashmap2.rs | 20 ++++++---------- exercise/exercises/collections/vec1.rs | 11 ++++----- exercise/exercises/collections/vec2.rs | 13 ++++------ exercise/info.toml | 28 ++++++++++------------ 6 files changed, 48 insertions(+), 71 deletions(-) diff --git a/exercise/exercises/collections/README.md b/exercise/exercises/collections/README.md index b6d62acb..152bc5ba 100644 --- a/exercise/exercises/collections/README.md +++ b/exercise/exercises/collections/README.md @@ -1,23 +1,18 @@ -# Collections +# 集合(Collections) -Rust’s standard library includes a number of very useful data -structures called collections. Most other data types represent one -specific value, but collections can contain multiple values. Unlike -the built-in array and tuple types, the data these collections point -to is stored on the heap, which means the amount of data does not need -to be known at compile time and can grow or shrink as the program -runs. +Rust 的标准库包含了很多有用的数据结构,它们称作为集合。 +大多其它的数据类型通常仅表示一个特定的值,但集合可以包含多个值。 +内置的数组和元组类型指向的数据存储在堆上,这意味着存储的数据不必在编译时确定, +并可以根据程序的运行来增加或减少。 -This exercise will get you familiar with two fundamental data -structures that are used very often in Rust programs: +本次练习将带你熟悉 Rust 程序中两个特别常用的基本数据结构: -* A *vector* allows you to store a variable number of values next to - each other. -* A *hash map* allows you to associate a value with a particular key. - You may also know this by the names [*unordered map* in C++](https://en.cppreference.com/w/cpp/container/unordered_map), - [*dictionary* in Python](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) or an *associative array* in other languages. +* *vector* 能够存储一段连续且数量不定的值。 -## Further information +* *散列表(hash map)* 能够将某个值与一个特定的键关联起来。 + 你可能也知道它们:[C++ 中的 *unordered map*](https://en.cppreference.com/w/cpp/container/unordered_map)、[Python 的 *dictionary*](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) 或其它语言中的 *associative array(译:关联数组、map、映射)*。 + +## 更多信息 - [Storing Lists of Values with Vectors](https://doc.rust-lang.org/stable/book/ch08-01-vectors.html) - [Storing Keys with Associated Values in Hash Maps](https://doc.rust-lang.org/book/ch08-03-hash-maps.html) diff --git a/exercise/exercises/collections/hashmap1.rs b/exercise/exercises/collections/hashmap1.rs index 64b5a7f3..342ea743 100644 --- a/exercise/exercises/collections/hashmap1.rs +++ b/exercise/exercises/collections/hashmap1.rs @@ -1,27 +1,23 @@ // hashmap1.rs -// A basket of fruits in the form of a hash map needs to be defined. -// The key represents the name of the fruit and the value represents -// how many of that particular fruit is in the basket. You have to put -// at least three different types of fruits (e.g apple, banana, mango) -// in the basket and the total count of all the fruits should be at -// least five. + +// 用散列表定义一个水果篮。以键表示水果的名称,值来代表篮子里对应水果的个数。 +// 要求必须在篮子里放至少三种水果(如苹果、香蕉、芒果),每种水果的总数也应不少于五个。 // -// Make me compile and pass the tests! +// 让我通过编译和测试! // -// Execute the command `rustlings hint hashmap1` if you need -// hints. +// 如果需要提示,可以执行命令 `rustlings hint hashmap1`。 // I AM NOT DONE use std::collections::HashMap; fn fruit_basket() -> HashMap { - let mut basket = // TODO: declare your hash map here. + let mut basket = // TODO:在这声明个散列表 - // Two bananas are already given for you :) + // 给你两个香蕉 basket.insert(String::from("banana"), 2); - // TODO: Put more fruits in your basket here. + // TODO:在这往篮子里添加更多的水果 basket } diff --git a/exercise/exercises/collections/hashmap2.rs b/exercise/exercises/collections/hashmap2.rs index 0abe19ab..95774546 100644 --- a/exercise/exercises/collections/hashmap2.rs +++ b/exercise/exercises/collections/hashmap2.rs @@ -1,16 +1,11 @@ // hashmap2.rs - -// A basket of fruits in the form of a hash map is given. The key -// represents the name of the fruit and the value represents how many -// of that particular fruit is in the basket. You have to put *MORE -// THAN 11* fruits in the basket. Three types of fruits - Apple (4), -// Mango (2) and Lychee (5) are already given in the basket. You are -// not allowed to insert any more of these fruits! +// 给你一个用散列表表示的水果篮,它的键表示水果的名称,值表示篮子里对应水果的个数。 +// 现在需要往篮子添加至少 11 种水果。篮子里已有 - 苹果 (4), +// 芒果 (2) 和荔枝 (5) 三种水果,你不能再添加这些水果。 // -// Make me pass the tests! +// 让我通过测试! // -// Execute the command `rustlings hint hashmap2` if you need -// hints. +// 如果需要提示,可以执行命令 `rustlings hint hashmap2`。 // I AM NOT DONE @@ -35,9 +30,8 @@ fn fruit_basket(basket: &mut HashMap) { ]; for fruit in fruit_kinds { - // TODO: Put new fruits if not already present. Note that you - // are not allowed to put any type of fruit that's already - // present! + // TODO:如果篮子里没有某种水果,就把它放入篮子。 + // 注意,你不能放入篮子中任何已有的水果。 } } diff --git a/exercise/exercises/collections/vec1.rs b/exercise/exercises/collections/vec1.rs index b144fb94..0ade6fd2 100644 --- a/exercise/exercises/collections/vec1.rs +++ b/exercise/exercises/collections/vec1.rs @@ -1,14 +1,13 @@ // vec1.rs -// Your task is to create a `Vec` which holds the exact same elements -// as in the array `a`. -// Make me compile and pass the test! -// Execute the command `rustlings hint vec1` if you need hints. +// 你的任务是创建一个与数组 `a` 中的元素完全相同的 `Vec`。 +// 让我通过编译和测试! +// 如果需要提示,可以执行命令 `rustlings hint vec1`。 // I AM NOT DONE fn array_and_vec() -> ([i32; 4], Vec) { - let a = [10, 20, 30, 40]; // a plain array - let v = // TODO: declare your vector here with the macro for vectors + let a = [10, 20, 30, 40]; // 一个普通的数组 + let v = // TODO:在这里用 vectors 的宏来声明你的 vector (a, v) } diff --git a/exercise/exercises/collections/vec2.rs b/exercise/exercises/collections/vec2.rs index 6595e401..ba9a2415 100644 --- a/exercise/exercises/collections/vec2.rs +++ b/exercise/exercises/collections/vec2.rs @@ -1,21 +1,18 @@ // vec2.rs -// A Vec of even numbers is given. Your task is to complete the loop -// so that each number in the Vec is multiplied by 2. +// 给定一个全是偶数的 Vec 。你的任务是完成一个循环,做到将 Vec 中的每个数字都乘以 2 。 // -// Make me pass the test! +// 让我通过编译和测试! // -// Execute the command `rustlings hint vec2` if you need -// hints. +// 如果需要提示,可以执行命令 `rustlings hint vec2`。 // I AM NOT DONE fn vec_loop(mut v: Vec) -> Vec { for i in v.iter_mut() { - // TODO: Fill this up so that each element in the Vec `v` is - // multiplied by 2. + // TODO:将 Vec `v` 中的每个元素都乘以 2 。 } - // At this point, `v` should be equal to [4, 8, 12, 16, 20]. + // 此时 `v' 应该等于 [4, 8, 12, 16, 20] 。 v } diff --git a/exercise/info.toml b/exercise/info.toml index 41474569..2804ab00 100644 --- a/exercise/info.toml +++ b/exercise/info.toml @@ -362,13 +362,12 @@ name = "vec1" path = "exercises/collections/vec1.rs" mode = "test" hint = """ -In Rust, there are two ways to define a Vector. -1. One way is to use the `Vec::new()` function to create a new vector - and fill it with the `push()` method. -2. The second way, which is simpler is to use the `vec![]` macro and - define your elements inside the square brackets. -Check this chapter: https://doc.rust-lang.org/stable/book/ch08-01-vectors.html -of the Rust book to learn more. +Rust 有以下两种方式定义一个 vector 。 +1. 一种方法是使用 `Vec::new()` 来创建一个新的 vector,然后 + 使用 `push()` 方法往里面添加元素。 +2. 第二种方法更简单,那就是使用 `vec![]` 宏,接着在宏的方括号内定义需要的元素。 +阅读这本书的此章节:https://doc.rust-lang.org/stable/book/ch08-01-vectors.html +来了解更多。 """ [[exercises]] @@ -376,9 +375,8 @@ name = "vec2" path = "exercises/collections/vec2.rs" mode = "test" hint = """ -Hint 1: `i` is each element from the Vec as they are being iterated. - Can you try multiplying this? -Hint 2: Check the suggestion from the compiler error ;) +提示 1 :`i` 是 Vec 中被迭代的每个元素,你可以乘以它吗? +提示 2 : 研究下编译错误给出的建议 ;) """ [[exercises]] @@ -386,10 +384,8 @@ name = "hashmap1" path = "exercises/collections/hashmap1.rs" mode = "test" hint = """ -Hint 1: Take a look at the return type of the function to figure out - the type for the `basket`. -Hint 2: Number of fruits should be at least 5. And you have to put - at least three different types of fruits. +提示 1 :看看函数的返回类型,推测出 `basket` 的类型。 +提示 2 :水果的数量应该不少于 5 个。并且需要确保篮子里至少有三种水果。 """ [[exercises]] @@ -397,8 +393,8 @@ name = "hashmap2" path = "exercises/collections/hashmap2.rs" mode = "test" hint = """ -Use the `entry()` and `or_insert()` methods of `HashMap` to achieve this. -Learn more at https://doc.rust-lang.org/stable/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value +使用 `HashMap` 中的 `entry()` 和 `or_insert()` 来完成目标。 +了解更多:https://doc.rust-lang.org/stable/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value """ # STRINGS