Skip to content

Commit 34b81df

Browse files
authored
update trait.md and destructure_slice.md (#191)
1 parent c95f29e commit 34b81df

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# arrays/slices
2+
3+
Like tuples, arrays and slices can be destructured this way:
4+
5+
```rust,editable
6+
fn main() {
7+
// Try changing the values in the array, or make it a slice!
8+
let array = [1, -2, 6];
9+
10+
match array {
11+
// Binds the second and the third elements to the respective variables
12+
[0, second, third] =>
13+
println!("array[0] = 0, array[1] = {}, array[2] = {}", second, third),
14+
15+
// Single values can be ignored with _
16+
[1, _, third] => println!(
17+
"array[0] = 1, array[2] = {} and array[1] was ignored",
18+
third
19+
),
20+
21+
// You can also bind some and ignore the rest
22+
[-1, second, ..] => println!(
23+
"array[0] = -1, array[1] = {} and all the other ones were ignored",
24+
second
25+
),
26+
// The code below would not compile
27+
// [-1, second] => ...
28+
29+
// Or store them in another array/slice (the type depends on
30+
// that of the value that is being matched against)
31+
[3, second, tail @ ..] => println!(
32+
"array[0] = 3, array[1] = {} and the other elements were {:?}",
33+
second, tail
34+
),
35+
36+
// Combining these patterns, we can, for example, bind the first and
37+
// last values, and store the rest of them in a single array
38+
[first, middle @ .., last] => println!(
39+
"array[0] = {}, middle = {:?}, array[2] = {}",
40+
first, middle, last
41+
),
42+
}
43+
}
44+
```
45+
46+
### See also:
47+
48+
[Arrays and Slices](../../../primitives/array.md) and [Binding](../binding.md) for `@` sigil

Diff for: english/src/trait.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ methods from `Animal` with a `Sheep`.
1212
struct Sheep { naked: bool, name: &'static str }
1313
1414
trait Animal {
15-
// Static method signature; `Self` refers to the implementor type.
15+
// Associated function signature; `Self` refers to the implementor type.
1616
fn new(name: &'static str) -> Self;
1717
18-
// Instance method signatures; these will return a string.
18+
// Method signatures; these will return a string.
1919
fn name(&self) -> &'static str;
2020
fn noise(&self) -> &'static str;
2121
@@ -77,4 +77,4 @@ fn main() {
7777
dolly.shear();
7878
dolly.talk();
7979
}
80-
```
80+
```

Diff for: src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
- [match 匹配](flow_control/match.md)
5353
- [解构](flow_control/match/destructuring.md)
5454
- [元组](flow_control/match/destructuring/destructure_tuple.md)
55+
- [数组/切片](flow_control/match/destructuring/destructure_slice.md)
5556
- [枚举](flow_control/match/destructuring/destructure_enum.md)
5657
- [指针和引用](flow_control/match/destructuring/destructure_pointers.md)
5758
- [结构体](flow_control/match/destructuring/destructure_structures.md)
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 数组/切片
2+
3+
像元组一样,数组和切片也可以这样解构:
4+
5+
```rust,editable
6+
fn main() {
7+
// 尝试改变数组中的值,或者将其做成切片!
8+
let array = [1, -2, 6];
9+
10+
match array {
11+
// 将第二个和第三个元素绑定到各自的变量
12+
[0, second, third] =>
13+
println!("array[0] = 0, array[1] = {}, array[2] = {}", second, third),
14+
15+
// 单个值可以用 `_` 忽略
16+
[1, _, third] => println!(
17+
"array[0] = 1, array[2] = {} and array[1] was ignored",
18+
third
19+
),
20+
21+
// 你也可以绑定一些而忽略其余的
22+
[-1, second, ..] => println!(
23+
"array[0] = -1, array[1] = {} and all the other ones were ignored",
24+
second
25+
),
26+
// 下面的代码无法编译
27+
// [-1, second] => ...
28+
29+
// 或者将它们存储在另一个数组/切片中(类型取决于所匹配的值的类型)
30+
[3, second, tail @ ..] => println!(
31+
"array[0] = 3, array[1] = {} and the other elements were {:?}",
32+
second, tail
33+
),
34+
35+
// 结合这些模式,我们可以绑定第一个和最后一个值,并将其余的值存储在一个数组中
36+
[first, middle @ .., last] => println!(
37+
"array[0] = {}, middle = {:?}, array[2] = {}",
38+
first, middle, last
39+
),
40+
}
41+
}
42+
```
43+
44+
### 参见:
45+
[数组和切片](../../../primitives/array.md)`@` 符号用法[绑定](../binding.md)

Diff for: src/trait.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
struct Sheep { naked: bool, name: &'static str }
99
1010
trait Animal {
11-
// 静态方法签名;`Self` 表示实现者类型(implementor type)。
11+
// 关联函数签名;`Self` 表示实现者类型(implementor type)。
1212
fn new(name: &'static str) -> Self;
1313
14-
// 实例方法签名;这些方法将返回一个字符串。
14+
// 方法签名;这些方法将返回一个字符串。
1515
fn name(&self) -> &'static str;
1616
fn noise(&self) -> &'static str;
1717

0 commit comments

Comments
 (0)