1
+ <!--
1
2
# arrays/slices
3
+ -->
4
+ # 配列とスライス
2
5
6
+ <!--
3
7
Like tuples, arrays and slices can be destructured this way:
8
+ -->
9
+ タプル同様、配列とスライスも以下のようにデストラクトできます:
4
10
5
11
``` rust,editable
6
12
fn main() {
7
13
// Try changing the values in the array, or make it a slice!
14
+ // 配列中の値を変更してみましょう。または、スライスにしてみましょう。
8
15
let array = [1, -2, 6];
9
16
10
17
match array {
11
18
// Binds the second and the third elements to the respective variables
19
+ // 2番目と3番目の要素を変数にバインドする。
12
20
[0, second, third] =>
13
21
println!("array[0] = 0, array[1] = {}, array[2] = {}", second, third),
14
22
15
23
// Single values can be ignored with _
24
+ // _で値を無視できる。
16
25
[1, _, third] => println!(
17
26
"array[0] = 1, array[2] = {} and array[1] was ignored",
18
27
third
19
28
),
20
29
21
30
// You can also bind some and ignore the rest
31
+ // いくつかの値をバインドして残りを無視できる。
22
32
[-1, second, ..] => println!(
23
33
"array[0] = -1, array[1] = {} and all the other ones were ignored",
24
34
second
25
35
),
26
36
// The code below would not compile
37
+ // 以下のコードはコンパイルできない。
27
38
// [-1, second] => ...
28
39
29
40
// Or store them in another array/slice (the type depends on
30
41
// that of the value that is being matched against)
42
+ // 別の配列やスライスに値を持たせることもできます。
43
+ // (配列かスライスかは、マッチする値の型により異なります)
31
44
[3, second, tail @ ..] => println!(
32
45
"array[0] = 3, array[1] = {} and the other elements were {:?}",
33
46
second, tail
34
47
),
35
48
36
49
// Combining these patterns, we can, for example, bind the first and
37
50
// last values, and store the rest of them in a single array
51
+ // 例えば、これらのパターンを組み合わせて、
52
+ // 最初と最後の値をバインドし、残りの値を配列に持たせることもできます。
38
53
[first, middle @ .., last] => println!(
39
54
"array[0] = {}, middle = {:?}, array[2] = {}",
40
55
first, middle, last
@@ -43,6 +58,12 @@ fn main() {
43
58
}
44
59
```
45
60
61
+ <!--
46
62
### See also:
63
+ -->
64
+ ### 参照
47
65
66
+ <!--
48
67
[Arrays and Slices](../../../primitives/array.md) and [Binding](../binding.md) for `@` sigil
68
+ -->
69
+ [ 配列とスライス] ( ../../../primitives/array.md ) 、@マークについては[ バインディング] ( ../binding.md )
0 commit comments