Skip to content

Commit 1d1b811

Browse files
committed
Merge pull request #4589 from sanxiyn/doc-vec-match
doc: Document pattern matching on vectors
2 parents 10e8ae8 + 92e52bc commit 1d1b811

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

doc/tutorial.md

+12
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,18 @@ match crayons[0] {
12261226
}
12271227
~~~~
12281228
1229+
A vector can be destructured using pattern matching:
1230+
1231+
~~~~
1232+
let numbers: [int * 3] = [1, 2, 3];
1233+
let score = match numbers {
1234+
[] => 0,
1235+
[a] => a * 10,
1236+
[a, b] => a * 6 + b * 4,
1237+
[a, b, c, ..rest] => a * 5 + b * 3 + c * 2 + rest.len() as int
1238+
};
1239+
~~~~
1240+
12291241
The elements of a vector _inherit the mutability of the vector_,
12301242
and as such, individual elements may not be reassigned when the
12311243
vector lives in an immutable slot.

0 commit comments

Comments
 (0)