-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update trait.md and destructure_slice.md (#191)
- Loading branch information
1 parent
c95f29e
commit 34b81df
Showing
5 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
english/src/flow_control/match/destructuring/destructure_slice.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters