From 34b81df7e427366b6057ee25cc3deaffc7cca221 Mon Sep 17 00:00:00 2001 From: WangMengabc <98640292+WangMengabc@users.noreply.github.com> Date: Wed, 8 May 2024 06:07:38 +0000 Subject: [PATCH] update trait.md and destructure_slice.md (#191) --- .../match/destructuring/destructure_slice.md | 48 +++++++++++++++++++ english/src/trait.md | 6 +-- src/SUMMARY.md | 1 + .../match/destructuring/destructure_slice.md | 45 +++++++++++++++++ src/trait.md | 4 +- 5 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 english/src/flow_control/match/destructuring/destructure_slice.md create mode 100644 src/flow_control/match/destructuring/destructure_slice.md diff --git a/english/src/flow_control/match/destructuring/destructure_slice.md b/english/src/flow_control/match/destructuring/destructure_slice.md new file mode 100644 index 00000000..93b7e420 --- /dev/null +++ b/english/src/flow_control/match/destructuring/destructure_slice.md @@ -0,0 +1,48 @@ +# arrays/slices + +Like tuples, arrays and slices can be destructured this way: + +```rust,editable +fn main() { + // Try changing the values in the array, or make it a slice! + let array = [1, -2, 6]; + + match array { + // Binds the second and the third elements to the respective variables + [0, second, third] => + println!("array[0] = 0, array[1] = {}, array[2] = {}", second, third), + + // Single values can be ignored with _ + [1, _, third] => println!( + "array[0] = 1, array[2] = {} and array[1] was ignored", + third + ), + + // You can also bind some and ignore the rest + [-1, second, ..] => println!( + "array[0] = -1, array[1] = {} and all the other ones were ignored", + second + ), + // The code below would not compile + // [-1, second] => ... + + // Or store them in another array/slice (the type depends on + // that of the value that is being matched against) + [3, second, tail @ ..] => println!( + "array[0] = 3, array[1] = {} and the other elements were {:?}", + second, tail + ), + + // Combining these patterns, we can, for example, bind the first and + // last values, and store the rest of them in a single array + [first, middle @ .., last] => println!( + "array[0] = {}, middle = {:?}, array[2] = {}", + first, middle, last + ), + } +} +``` + +### See also: + +[Arrays and Slices](../../../primitives/array.md) and [Binding](../binding.md) for `@` sigil diff --git a/english/src/trait.md b/english/src/trait.md index 7878d474..d4d40946 100644 --- a/english/src/trait.md +++ b/english/src/trait.md @@ -12,10 +12,10 @@ methods from `Animal` with a `Sheep`. struct Sheep { naked: bool, name: &'static str } trait Animal { - // Static method signature; `Self` refers to the implementor type. + // Associated function signature; `Self` refers to the implementor type. fn new(name: &'static str) -> Self; - // Instance method signatures; these will return a string. + // Method signatures; these will return a string. fn name(&self) -> &'static str; fn noise(&self) -> &'static str; @@ -77,4 +77,4 @@ fn main() { dolly.shear(); dolly.talk(); } -``` \ No newline at end of file +``` diff --git a/src/SUMMARY.md b/src/SUMMARY.md index bfbb450d..ff9a05a4 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -52,6 +52,7 @@ - [match 匹配](flow_control/match.md) - [解构](flow_control/match/destructuring.md) - [元组](flow_control/match/destructuring/destructure_tuple.md) + - [数组/切片](flow_control/match/destructuring/destructure_slice.md) - [枚举](flow_control/match/destructuring/destructure_enum.md) - [指针和引用](flow_control/match/destructuring/destructure_pointers.md) - [结构体](flow_control/match/destructuring/destructure_structures.md) diff --git a/src/flow_control/match/destructuring/destructure_slice.md b/src/flow_control/match/destructuring/destructure_slice.md new file mode 100644 index 00000000..ce8431f2 --- /dev/null +++ b/src/flow_control/match/destructuring/destructure_slice.md @@ -0,0 +1,45 @@ +# 数组/切片 + +像元组一样,数组和切片也可以这样解构: + +```rust,editable +fn main() { + // 尝试改变数组中的值,或者将其做成切片! + let array = [1, -2, 6]; + + match array { + // 将第二个和第三个元素绑定到各自的变量 + [0, second, third] => + println!("array[0] = 0, array[1] = {}, array[2] = {}", second, third), + + // 单个值可以用 `_` 忽略 + [1, _, third] => println!( + "array[0] = 1, array[2] = {} and array[1] was ignored", + third + ), + + // 你也可以绑定一些而忽略其余的 + [-1, second, ..] => println!( + "array[0] = -1, array[1] = {} and all the other ones were ignored", + second + ), + // 下面的代码无法编译 + // [-1, second] => ... + + // 或者将它们存储在另一个数组/切片中(类型取决于所匹配的值的类型) + [3, second, tail @ ..] => println!( + "array[0] = 3, array[1] = {} and the other elements were {:?}", + second, tail + ), + + // 结合这些模式,我们可以绑定第一个和最后一个值,并将其余的值存储在一个数组中 + [first, middle @ .., last] => println!( + "array[0] = {}, middle = {:?}, array[2] = {}", + first, middle, last + ), + } +} +``` + +### 参见: +[数组和切片](../../../primitives/array.md) 与 `@` 符号用法[绑定](../binding.md) \ No newline at end of file diff --git a/src/trait.md b/src/trait.md index 76f63d5d..db3f3802 100644 --- a/src/trait.md +++ b/src/trait.md @@ -8,10 +8,10 @@ struct Sheep { naked: bool, name: &'static str } trait Animal { - // 静态方法签名;`Self` 表示实现者类型(implementor type)。 + // 关联函数签名;`Self` 表示实现者类型(implementor type)。 fn new(name: &'static str) -> Self; - // 实例方法签名;这些方法将返回一个字符串。 + // 方法签名;这些方法将返回一个字符串。 fn name(&self) -> &'static str; fn noise(&self) -> &'static str;