Skip to content

Commit c9d2769

Browse files
committed
doc: Update for alt arrows
1 parent 3fe1c70 commit c9d2769

File tree

2 files changed

+58
-41
lines changed

2 files changed

+58
-41
lines changed

doc/rust.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,8 @@ An example of a predicate that uses an unchecked block:
957957
958958
fn pure_foldl<T, U: copy>(ls: list<T>, u: U, f: fn(&&T, &&U) -> U) -> U {
959959
alt ls {
960-
nil { u }
961-
cons(hd, tl) { f(hd, pure_foldl(*tl, f(hd, u), f)) }
960+
nil => u,
961+
cons(hd, tl) => f(hd, pure_foldl(*tl, f(hd, u), f))
962962
}
963963
}
964964
@@ -1157,8 +1157,8 @@ class file_descriptor {
11571157
}
11581158
fn get_name() -> ~str {
11591159
alt self.name {
1160-
none { fail ~"File has no name!"; }
1161-
some(n) { n }
1160+
none => fail ~"File has no name!",
1161+
some(n) => n
11621162
}
11631163
}
11641164
}
@@ -2176,7 +2176,7 @@ then any `else` block is executed.
21762176
~~~~~~~~{.ebnf .gram}
21772177
alt_expr : "alt" expr '{' alt_arm [ '|' alt_arm ] * '}' ;
21782178
2179-
alt_arm : alt_pat '{' block '}' ;
2179+
alt_arm : alt_pat '=>' expr_or_blockish ;
21802180
21812181
alt_pat : pat [ "to" pat ] ? [ "if" expr ] ;
21822182
~~~~~~~~
@@ -2199,9 +2199,9 @@ enum list<X> { nil, cons(X, @list<X>) }
21992199
let x: list<int> = cons(10, @cons(11, @nil));
22002200
22012201
alt x {
2202-
cons(_, @nil) { fail ~"singleton list"; }
2203-
cons(*) { return; }
2204-
nil { fail ~"empty list"; }
2202+
cons(_, @nil) => fail ~"singleton list",
2203+
cons(*) => return,
2204+
nil => fail ~"empty list"
22052205
}
22062206
~~~~
22072207

@@ -2228,16 +2228,16 @@ enum list<X> { nil, cons(X, @list<X>) }
22282228
let x: list<int> = cons(10, @cons(11, @nil));
22292229
22302230
alt x {
2231-
cons(a, @cons(b, _)) {
2231+
cons(a, @cons(b, _)) => {
22322232
process_pair(a,b);
22332233
}
2234-
cons(10, _) {
2234+
cons(10, _) => {
22352235
process_ten();
22362236
}
2237-
nil {
2237+
nil => {
22382238
return;
22392239
}
2240-
_ {
2240+
_ => {
22412241
fail;
22422242
}
22432243
}
@@ -2265,13 +2265,13 @@ fn main() {
22652265
};
22662266
22672267
alt r {
2268-
{options: {choose: true, _}, _} {
2268+
{options: {choose: true, _}, _} => {
22692269
choose_player(r)
22702270
}
2271-
{player: p, options: {size: ~"small", _}, _} {
2271+
{player: p, options: {size: ~"small", _}, _} => {
22722272
log(info, p + ~" is small");
22732273
}
2274-
_ {
2274+
_ => {
22752275
next_player();
22762276
}
22772277
}
@@ -2285,9 +2285,9 @@ range of values may be specified with `to`. For example:
22852285
# let x = 2;
22862286
22872287
let message = alt x {
2288-
0 | 1 { ~"not many" }
2289-
2 to 9 { ~"a few" }
2290-
_ { ~"lots" }
2288+
0 | 1 => ~"not many",
2289+
2 to 9 => ~"a few",
2290+
_ => ~"lots"
22912291
};
22922292
~~~~
22932293

@@ -2302,9 +2302,9 @@ guard may refer to the variables bound within the pattern they follow.
23022302
# fn process_other(i: int) { }
23032303
23042304
let message = alt maybe_digit {
2305-
some(x) if x < 10 { process_digit(x) }
2306-
some(x) { process_other(x) }
2307-
none { fail }
2305+
some(x) if x < 10 => process_digit(x),
2306+
some(x) => process_other(x),
2307+
none => fail
23082308
};
23092309
~~~~
23102310

doc/tutorial.md

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ fn main() {
117117
118118
// Pick two gestures and decide the result
119119
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"
123123
}
124124
}
125125
}
@@ -715,10 +715,10 @@ the value.
715715
~~~~
716716
# let my_number = 1;
717717
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")
722722
}
723723
~~~~
724724

@@ -732,6 +732,23 @@ valid patterns, and will match only their own value. The pipe operator
732732
of numeric literal patterns can be expressed with `to`. The underscore
733733
(`_`) is a wildcard pattern that matches everything.
734734

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+
735752
If the arm with the wildcard pattern was left off in the above
736753
example, the typechecker would reject it at compile time. `alt`
737754
constructs must be exhaustive: they must have an arm covering every
@@ -747,9 +764,9 @@ that `(float, float)` is a tuple of two floats:
747764
~~~~
748765
fn angle(vec: (float, float)) -> float {
749766
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)
753770
}
754771
}
755772
~~~~
@@ -1035,8 +1052,8 @@ name as the field.
10351052
~~~~
10361053
# let mypoint = {x: 0f, y: 0f};
10371054
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 */ }
10401057
}
10411058
~~~~
10421059

@@ -1141,8 +1158,8 @@ patterns, as in this definition of `area`:
11411158
# enum shape { circle(point, float), rectangle(point, point) }
11421159
fn area(sh: shape) -> float {
11431160
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)
11461163
}
11471164
}
11481165
~~~~
@@ -1154,10 +1171,10 @@ Another example, matching nullary enum variants:
11541171
# enum direction { north, east, south, west }
11551172
fn point_from_direction(dir: direction) -> point {
11561173
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}
11611178
}
11621179
}
11631180
~~~~
@@ -1172,7 +1189,7 @@ nil, `()`, as the empty tuple if you like).
11721189
~~~~
11731190
let mytup: (int, int, float) = (10, 20, 30.0);
11741191
alt mytup {
1175-
(a, b, c) { log(info, a + b + (c as int)); }
1192+
(a, b, c) => log(info, a + b + (c as int))
11761193
}
11771194
~~~~
11781195

@@ -1914,7 +1931,7 @@ safety.
19141931
~~~~
19151932
let mut my_rec = {a: 4, b: ~[1, 2, 3]};
19161933
alt my_rec {
1917-
{a, b} {
1934+
{a, b} => {
19181935
log(info, b); // This is okay
19191936
my_rec = {a: a + 1, b: b + ~[a]};
19201937
log(info, b); // Here reference b has become invalid

0 commit comments

Comments
 (0)