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
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)

@ -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<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);
// TODO: Put more fruits in your basket here.
// TODO:在这往篮子里添加更多的水果
basket
}

@ -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<Fruit, u32>) {
];
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如果篮子里没有某种水果就把它放入篮子。
// 注意,你不能放入篮子中任何已有的水果。
}
}

@ -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<i32>) {
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)
}

@ -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<i32>) -> Vec<i32> {
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
}

@ -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

Loading…
Cancel
Save