Merge pull request #189 from mg-chao/20220112_collections

[rust-exercise] 翻译 collections 部分
pull/190/head
Sunface 3 years ago committed by GitHub
commit 3dab25c7ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,23 +1,18 @@
# Collections # 集合(Collections
Rusts standard library includes a number of very useful data Rust 的标准库包含了很多有用的数据结构,它们称作为集合。
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.
This exercise will get you familiar with two fundamental data 本次练习将带你熟悉 Rust 程序中两个特别常用的基本数据结构:
structures that are used very often in Rust programs:
* A *vector* allows you to store a variable number of values next to * *vector* 能够存储一段连续且数量不定的值。
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.
## 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 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) - [Storing Keys with Associated Values in Hash Maps](https://doc.rust-lang.org/book/ch08-03-hash-maps.html)

@ -1,27 +1,23 @@
// hashmap1.rs // 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 // 如果需要提示,可以执行命令 `rustlings hint hashmap1`。
// hints.
// I AM NOT DONE // I AM NOT DONE
use std::collections::HashMap; use std::collections::HashMap;
fn fruit_basket() -> HashMap<String, u32> { fn fruit_basket() -> HashMap<String, u32> {
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); basket.insert(String::from("banana"), 2);
// TODO: Put more fruits in your basket here. // TODO:在这往篮子里添加更多的水果
basket basket
} }

@ -1,16 +1,11 @@
// hashmap2.rs // hashmap2.rs
// 给你一个用散列表表示的水果篮,它的键表示水果的名称,值表示篮子里对应水果的个数。
// A basket of fruits in the form of a hash map is given. The key // 现在需要往篮子添加至少 11 种水果。篮子里已有 - 苹果 (4),
// represents the name of the fruit and the value represents how many // 芒果 (2) 和荔枝 (5) 三种水果,你不能再添加这些水果。
// 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!
// //
// Make me pass the tests! // 让我通过测试!
// //
// Execute the command `rustlings hint hashmap2` if you need // 如果需要提示,可以执行命令 `rustlings hint hashmap2`。
// hints.
// I AM NOT DONE // I AM NOT DONE
@ -35,9 +30,8 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
]; ];
for fruit in fruit_kinds { for fruit in fruit_kinds {
// TODO: Put new fruits if not already present. Note that you // TODO如果篮子里没有某种水果就把它放入篮子。
// are not allowed to put any type of fruit that's already // 注意,你不能放入篮子中任何已有的水果。
// present!
} }
} }

@ -1,14 +1,13 @@
// vec1.rs // vec1.rs
// Your task is to create a `Vec` which holds the exact same elements // 你的任务是创建一个与数组 `a` 中的元素完全相同的 `Vec`。
// as in the array `a`. // 让我通过编译和测试!
// Make me compile and pass the test! // 如果需要提示,可以执行命令 `rustlings hint vec1`。
// Execute the command `rustlings hint vec1` if you need hints.
// I AM NOT DONE // I AM NOT DONE
fn array_and_vec() -> ([i32; 4], Vec<i32>) { fn array_and_vec() -> ([i32; 4], Vec<i32>) {
let a = [10, 20, 30, 40]; // a plain array let a = [10, 20, 30, 40]; // 一个普通的数组
let v = // TODO: declare your vector here with the macro for vectors let v = // TODO:在这里用 vectors 的宏来声明你的 vector
(a, v) (a, v)
} }

@ -1,21 +1,18 @@
// vec2.rs // vec2.rs
// A Vec of even numbers is given. Your task is to complete the loop // 给定一个全是偶数的 Vec 。你的任务是完成一个循环,做到将 Vec 中的每个数字都乘以 2 。
// so that each number in the Vec is multiplied by 2.
// //
// Make me pass the test! // 让我通过编译和测试!
// //
// Execute the command `rustlings hint vec2` if you need // 如果需要提示,可以执行命令 `rustlings hint vec2`。
// hints.
// I AM NOT DONE // I AM NOT DONE
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> { fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for i in v.iter_mut() { for i in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is // TODO将 Vec `v` 中的每个元素都乘以 2 。
// multiplied by 2.
} }
// At this point, `v` should be equal to [4, 8, 12, 16, 20]. // 此时 `v' 应该等于 [4, 8, 12, 16, 20] 。
v v
} }

@ -362,13 +362,12 @@ name = "vec1"
path = "exercises/collections/vec1.rs" path = "exercises/collections/vec1.rs"
mode = "test" mode = "test"
hint = """ hint = """
In Rust, there are two ways to define a Vector. Rust vector
1. One way is to use the `Vec::new()` function to create a new vector 1. 使 `Vec::new()` vector
and fill it with the `push()` method. 使 `push()`
2. The second way, which is simpler is to use the `vec![]` macro and 2. 使 `vec![]`
define your elements inside the square brackets. https://doc.rust-lang.org/stable/book/ch08-01-vectors.html
Check this chapter: https://doc.rust-lang.org/stable/book/ch08-01-vectors.html
of the Rust book to learn more.
""" """
[[exercises]] [[exercises]]
@ -376,9 +375,8 @@ name = "vec2"
path = "exercises/collections/vec2.rs" path = "exercises/collections/vec2.rs"
mode = "test" mode = "test"
hint = """ hint = """
Hint 1: `i` is each element from the Vec as they are being iterated. 1 `i` Vec
Can you try multiplying this? 2 : ;)
Hint 2: Check the suggestion from the compiler error ;)
""" """
[[exercises]] [[exercises]]
@ -386,10 +384,8 @@ name = "hashmap1"
path = "exercises/collections/hashmap1.rs" path = "exercises/collections/hashmap1.rs"
mode = "test" mode = "test"
hint = """ hint = """
Hint 1: Take a look at the return type of the function to figure out 1 `basket`
the type for the `basket`. 2 5
Hint 2: Number of fruits should be at least 5. And you have to put
at least three different types of fruits.
""" """
[[exercises]] [[exercises]]
@ -397,8 +393,8 @@ name = "hashmap2"
path = "exercises/collections/hashmap2.rs" path = "exercises/collections/hashmap2.rs"
mode = "test" mode = "test"
hint = """ hint = """
Use the `entry()` and `or_insert()` methods of `HashMap` to achieve this. 使 `HashMap` `entry()` `or_insert()`
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 https://doc.rust-lang.org/stable/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value
""" """
# STRINGS # STRINGS

Loading…
Cancel
Save