diff options
| author | Brian Anderson <banderson@mozilla.com> | 2012-08-05 22:07:22 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-08-05 22:07:33 -0700 |
| commit | c9d27693796fe4ced8568e11aa465750f743097b (patch) | |
| tree | b2e96c30a41499631d94a23d0516c3fa34082aae | |
| parent | 3fe1c7071d9585f53748d8d0ccb5a06c7b850287 (diff) | |
doc: Update for alt arrows
| -rw-r--r-- | doc/rust.md | 42 | ||||
| -rw-r--r-- | doc/tutorial.md | 57 |
2 files changed, 58 insertions, 41 deletions
diff --git a/doc/rust.md b/doc/rust.md index 8478afc7ea2..fb3d27e67e5 100644 --- a/doc/rust.md +++ b/doc/rust.md @@ -957,8 +957,8 @@ An example of a predicate that uses an unchecked block: fn pure_foldl<T, U: copy>(ls: list<T>, u: U, f: fn(&&T, &&U) -> U) -> U { alt ls { - nil { u } - cons(hd, tl) { f(hd, pure_foldl(*tl, f(hd, u), f)) } + nil => u, + cons(hd, tl) => f(hd, pure_foldl(*tl, f(hd, u), f)) } } @@ -1157,8 +1157,8 @@ class file_descriptor { } fn get_name() -> ~str { alt self.name { - none { fail ~"File has no name!"; } - some(n) { n } + none => fail ~"File has no name!", + some(n) => n } } } @@ -2176,7 +2176,7 @@ then any `else` block is executed. ~~~~~~~~{.ebnf .gram} alt_expr : "alt" expr '{' alt_arm [ '|' alt_arm ] * '}' ; -alt_arm : alt_pat '{' block '}' ; +alt_arm : alt_pat '=>' expr_or_blockish ; alt_pat : pat [ "to" pat ] ? [ "if" expr ] ; ~~~~~~~~ @@ -2199,9 +2199,9 @@ enum list<X> { nil, cons(X, @list<X>) } let x: list<int> = cons(10, @cons(11, @nil)); alt x { - cons(_, @nil) { fail ~"singleton list"; } - cons(*) { return; } - nil { fail ~"empty list"; } + cons(_, @nil) => fail ~"singleton list", + cons(*) => return, + nil => fail ~"empty list" } ~~~~ @@ -2228,16 +2228,16 @@ enum list<X> { nil, cons(X, @list<X>) } let x: list<int> = cons(10, @cons(11, @nil)); alt x { - cons(a, @cons(b, _)) { + cons(a, @cons(b, _)) => { process_pair(a,b); } - cons(10, _) { + cons(10, _) => { process_ten(); } - nil { + nil => { return; } - _ { + _ => { fail; } } @@ -2265,13 +2265,13 @@ fn main() { }; alt r { - {options: {choose: true, _}, _} { + {options: {choose: true, _}, _} => { choose_player(r) } - {player: p, options: {size: ~"small", _}, _} { + {player: p, options: {size: ~"small", _}, _} => { log(info, p + ~" is small"); } - _ { + _ => { next_player(); } } @@ -2285,9 +2285,9 @@ range of values may be specified with `to`. For example: # let x = 2; let message = alt x { - 0 | 1 { ~"not many" } - 2 to 9 { ~"a few" } - _ { ~"lots" } + 0 | 1 => ~"not many", + 2 to 9 => ~"a few", + _ => ~"lots" }; ~~~~ @@ -2302,9 +2302,9 @@ guard may refer to the variables bound within the pattern they follow. # fn process_other(i: int) { } let message = alt maybe_digit { - some(x) if x < 10 { process_digit(x) } - some(x) { process_other(x) } - none { fail } + some(x) if x < 10 => process_digit(x), + some(x) => process_other(x), + none => fail }; ~~~~ diff --git a/doc/tutorial.md b/doc/tutorial.md index 481e184e31b..8ee70482ea9 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -117,9 +117,9 @@ fn main() { // Pick two gestures and decide the result alt (pick(), pick()) { - (rock, scissors) | (paper, rock) | (scissors, paper) { copy player1 } - (scissors, rock) | (rock, paper) | (paper, scissors) { copy player2 } - _ { ~"tie" } + (rock, scissors) | (paper, rock) | (scissors, paper) => copy player1, + (scissors, rock) | (rock, paper) | (paper, scissors) => copy player2, + _ => ~"tie" } } } @@ -715,10 +715,10 @@ the value. ~~~~ # let my_number = 1; alt my_number { - 0 { io::println(~"zero"); } - 1 | 2 { io::println(~"one or two"); } - 3 to 10 { io::println(~"three to ten"); } - _ { io::println(~"something else"); } + 0 => io::println(~"zero"), + 1 | 2 => io::println(~"one or two"), + 3 to 10 => io::println(~"three to ten"), + _ => io::println(~"something else") } ~~~~ @@ -732,6 +732,23 @@ valid patterns, and will match only their own value. The pipe operator of numeric literal patterns can be expressed with `to`. The underscore (`_`) is a wildcard pattern that matches everything. +The patterns in an alt arm are followed by a fat arrow, `=>`, then an +expression to evaluate. Each case is separated by commas. It's often +convenient to use a block expression for a case, in which case the +commas are optional. + +~~~ +# let my_number = 1; +alt my_number { + 0 => { + io::println(~"zero") + } + _ => { + io::println(~"something else") + } +} +~~~ + If the arm with the wildcard pattern was left off in the above example, the typechecker would reject it at compile time. `alt` constructs must be exhaustive: they must have an arm covering every @@ -747,9 +764,9 @@ that `(float, float)` is a tuple of two floats: ~~~~ fn angle(vec: (float, float)) -> float { alt vec { - (0f, y) if y < 0f { 1.5 * float::consts::pi } - (0f, y) { 0.5 * float::consts::pi } - (x, y) { float::atan(y / x) } + (0f, y) if y < 0f => 1.5 * float::consts::pi, + (0f, y) => 0.5 * float::consts::pi, + (x, y) => float::atan(y / x) } } ~~~~ @@ -1035,8 +1052,8 @@ name as the field. ~~~~ # let mypoint = {x: 0f, y: 0f}; alt mypoint { - {x: 0f, y: y_name} { /* Provide sub-patterns for fields */ } - {x, y} { /* Simply bind the fields */ } + {x: 0f, y: y_name} => { /* Provide sub-patterns for fields */ } + {x, y} => { /* Simply bind the fields */ } } ~~~~ @@ -1141,8 +1158,8 @@ patterns, as in this definition of `area`: # enum shape { circle(point, float), rectangle(point, point) } fn area(sh: shape) -> float { alt sh { - circle(_, size) { float::consts::pi * size * size } - rectangle({x, y}, {x: x2, y: y2}) { (x2 - x) * (y2 - y) } + circle(_, size) => float::consts::pi * size * size, + rectangle({x, y}, {x: x2, y: y2}) => (x2 - x) * (y2 - y) } } ~~~~ @@ -1154,10 +1171,10 @@ Another example, matching nullary enum variants: # enum direction { north, east, south, west } fn point_from_direction(dir: direction) -> point { alt dir { - north { {x: 0f, y: 1f} } - east { {x: 1f, y: 0f} } - south { {x: 0f, y: -1f} } - west { {x: -1f, y: 0f} } + north => {x: 0f, y: 1f}, + east => {x: 1f, y: 0f}, + south => {x: 0f, y: -1f}, + west => {x: -1f, y: 0f} } } ~~~~ @@ -1172,7 +1189,7 @@ nil, `()`, as the empty tuple if you like). ~~~~ let mytup: (int, int, float) = (10, 20, 30.0); alt mytup { - (a, b, c) { log(info, a + b + (c as int)); } + (a, b, c) => log(info, a + b + (c as int)) } ~~~~ @@ -1914,7 +1931,7 @@ safety. ~~~~ let mut my_rec = {a: 4, b: ~[1, 2, 3]}; alt my_rec { - {a, b} { + {a, b} => { log(info, b); // This is okay my_rec = {a: a + 1, b: b + ~[a]}; log(info, b); // Here reference b has become invalid |
