@@ -117,9 +117,9 @@ fn main() {
117
117
118
118
// Pick two gestures and decide the result
119
119
alt (pick(), pick()) {
120
- (rock, scissors) | (paper, rock) | (scissors, paper) { copy player1 }
121
- (scissors, rock) | (rock, paper) | (paper, scissors) { copy player2 }
122
- _ { ~"tie" }
120
+ (rock, scissors) | (paper, rock) | (scissors, paper) => copy player1,
121
+ (scissors, rock) | (rock, paper) | (paper, scissors) => copy player2,
122
+ _ => ~"tie"
123
123
}
124
124
}
125
125
}
@@ -715,10 +715,10 @@ the value.
715
715
~~~~
716
716
# let my_number = 1;
717
717
alt my_number {
718
- 0 { io::println(~"zero"); }
719
- 1 | 2 { io::println(~"one or two"); }
720
- 3 to 10 { io::println(~"three to ten"); }
721
- _ { io::println(~"something else"); }
718
+ 0 => io::println(~"zero"),
719
+ 1 | 2 => io::println(~"one or two"),
720
+ 3 to 10 => io::println(~"three to ten"),
721
+ _ => io::println(~"something else")
722
722
}
723
723
~~~~
724
724
@@ -732,6 +732,23 @@ valid patterns, and will match only their own value. The pipe operator
732
732
of numeric literal patterns can be expressed with ` to ` . The underscore
733
733
(` _ ` ) is a wildcard pattern that matches everything.
734
734
735
+ The patterns in an alt arm are followed by a fat arrow, ` => ` , then an
736
+ expression to evaluate. Each case is separated by commas. It's often
737
+ convenient to use a block expression for a case, in which case the
738
+ commas are optional.
739
+
740
+ ~~~
741
+ # let my_number = 1;
742
+ alt my_number {
743
+ 0 => {
744
+ io::println(~"zero")
745
+ }
746
+ _ => {
747
+ io::println(~"something else")
748
+ }
749
+ }
750
+ ~~~
751
+
735
752
If the arm with the wildcard pattern was left off in the above
736
753
example, the typechecker would reject it at compile time. ` alt `
737
754
constructs must be exhaustive: they must have an arm covering every
@@ -747,9 +764,9 @@ that `(float, float)` is a tuple of two floats:
747
764
~~~~
748
765
fn angle(vec: (float, float)) -> float {
749
766
alt vec {
750
- (0f, y) if y < 0f { 1.5 * float::consts::pi }
751
- (0f, y) { 0.5 * float::consts::pi }
752
- (x, y) { float::atan(y / x) }
767
+ (0f, y) if y < 0f => 1.5 * float::consts::pi,
768
+ (0f, y) => 0.5 * float::consts::pi,
769
+ (x, y) => float::atan(y / x)
753
770
}
754
771
}
755
772
~~~~
@@ -1035,8 +1052,8 @@ name as the field.
1035
1052
~~~~
1036
1053
# let mypoint = {x: 0f, y: 0f};
1037
1054
alt mypoint {
1038
- {x: 0f, y: y_name} { /* Provide sub-patterns for fields */ }
1039
- {x, y} { /* Simply bind the fields */ }
1055
+ {x: 0f, y: y_name} => { /* Provide sub-patterns for fields */ }
1056
+ {x, y} => { /* Simply bind the fields */ }
1040
1057
}
1041
1058
~~~~
1042
1059
@@ -1141,8 +1158,8 @@ patterns, as in this definition of `area`:
1141
1158
# enum shape { circle(point, float), rectangle(point, point) }
1142
1159
fn area(sh: shape) -> float {
1143
1160
alt sh {
1144
- circle(_, size) { float::consts::pi * size * size }
1145
- rectangle({x, y}, {x: x2, y: y2}) { (x2 - x) * (y2 - y) }
1161
+ circle(_, size) => float::consts::pi * size * size,
1162
+ rectangle({x, y}, {x: x2, y: y2}) => (x2 - x) * (y2 - y)
1146
1163
}
1147
1164
}
1148
1165
~~~~
@@ -1154,10 +1171,10 @@ Another example, matching nullary enum variants:
1154
1171
# enum direction { north, east, south, west }
1155
1172
fn point_from_direction(dir: direction) -> point {
1156
1173
alt dir {
1157
- north { {x: 0f, y: 1f} }
1158
- east { {x: 1f, y: 0f} }
1159
- south { {x: 0f, y: -1f} }
1160
- west { {x: -1f, y: 0f} }
1174
+ north => {x: 0f, y: 1f},
1175
+ east => {x: 1f, y: 0f},
1176
+ south => {x: 0f, y: -1f},
1177
+ west => {x: -1f, y: 0f}
1161
1178
}
1162
1179
}
1163
1180
~~~~
@@ -1172,7 +1189,7 @@ nil, `()`, as the empty tuple if you like).
1172
1189
~~~~
1173
1190
let mytup: (int, int, float) = (10, 20, 30.0);
1174
1191
alt mytup {
1175
- (a, b, c) { log(info, a + b + (c as int)); }
1192
+ (a, b, c) => log(info, a + b + (c as int))
1176
1193
}
1177
1194
~~~~
1178
1195
@@ -1914,7 +1931,7 @@ safety.
1914
1931
~~~~
1915
1932
let mut my_rec = {a: 4, b: ~[1, 2, 3]};
1916
1933
alt my_rec {
1917
- {a, b} {
1934
+ {a, b} => {
1918
1935
log(info, b); // This is okay
1919
1936
my_rec = {a: a + 1, b: b + ~[a]};
1920
1937
log(info, b); // Here reference b has become invalid
0 commit comments