Update docs to new let syntax.

This commit is contained in:
Graydon Hoare 2011-08-10 15:44:33 -07:00
parent 347230b001
commit 5b084e4e36

View File

@ -342,7 +342,7 @@ features of currying, in a smaller and simpler package.
To save some quantity of programmer key-pressing, Rust supports local type To save some quantity of programmer key-pressing, Rust supports local type
inference: signatures of functions, objects and iterators always require type inference: signatures of functions, objects and iterators always require type
annotation, but within the body of a function or iterator many slots can be annotation, but within the body of a function or iterator many slots can be
declared @code{auto} and Rust will infer the slot's type from its uses. declared without a type, and Rust will infer the slot's type from its uses.
@sp 1 @sp 1
@item Structural object system @item Structural object system
@ -662,7 +662,6 @@ The keywords are:
@tab @code{import} @tab @code{import}
@tab @code{export} @tab @code{export}
@tab @code{let} @tab @code{let}
@tab @code{auto}
@item @code{state} @item @code{state}
@tab @code{gc} @tab @code{gc}
@tab @code{const} @tab @code{const}
@ -1222,9 +1221,9 @@ mod foo = "foo.rs";
// In the source file "foo.rs", use the #re syntax extension and // In the source file "foo.rs", use the #re syntax extension and
// the re module at run-time. // the re module at run-time.
let str s = get_string(); let s: str = get_string();
let regex pattern = #re.pat@{ aa+b? @}; let pattern: regex = #re.pat@{ aa+b? @};
let bool matched = re.match(pattern, s); let matched: bool = re.match(pattern, s);
@end example @end example
@page @page
@ -1375,7 +1374,7 @@ Box types and values are constructed by the @emph{at} sigil @code{@@}.
An example of constructing a box type and value: An example of constructing a box type and value:
@example @example
let @@int x = @@10; let x: @@int = @@10;
@end example @end example
Some operations implicitly dereference boxes. Examples of such @dfn{implicit Some operations implicitly dereference boxes. Examples of such @dfn{implicit
@ -1387,8 +1386,8 @@ dereference} operations are:
An example of an implicit-dereference operation performed on box values: An example of an implicit-dereference operation performed on box values:
@example @example
let @@int x = @@10; let x: @@int = @@10;
let @@int y = @@12; let y: @@int = @@12;
assert (x + y == 22); assert (x + y == 22);
@end example @end example
@ -1412,7 +1411,7 @@ fn takes_unboxed(int b) @{
@} @}
fn main() @{ fn main() @{
let @@int x = @@10; let x: @@int = @@10;
takes_boxed(x); takes_boxed(x);
takes_unboxed(*x); takes_unboxed(*x);
@} @}
@ -1896,14 +1895,14 @@ each} loop or as the argument in a @code{put each} expression.
An example of an iterator: An example of an iterator:
@example @example
iter range(int lo, int hi) -> int @{ iter range(int lo, int hi) -> int @{
let int i = lo; let i: int = lo;
while (i < hi) @{ while (i < hi) @{
put i; put i;
i = i + 1; i = i + 1;
@} @}
@} @}
let int sum = 0; let sum: int = 0;
for each (int x in range(0,100)) @{ for each (int x in range(0,100)) @{
sum += x; sum += x;
@} @}
@ -1936,7 +1935,7 @@ obj counter(int state) @{
@} @}
@} @}
let counter c = counter(1); let c: counter = counter(1);
c.incr(); c.incr();
c.incr(); c.incr();
@ -1952,7 +1951,7 @@ obj my_obj() @{
ret 3; ret 3;
@} @}
fn foo() @{ fn foo() @{
auto c = get(); // Fails let c = get(); // Fails
@} @}
@} @}
@end example @end example
@ -1976,7 +1975,7 @@ fn mk_my_obj() -> my_obj @{
ret get_helper(); ret get_helper();
@} @}
fn foo() @{ fn foo() @{
auto c = get_helper(); // Works let c = get_helper(); // Works
@} @}
@} @}
@ -2040,7 +2039,7 @@ tag animal @{
cat; cat;
@} @}
let animal a = dog; let a: animal = dog;
a = cat; a = cat;
@end example @end example
@ -2051,7 +2050,7 @@ tag list[T] @{
cons(T, @@list[T]); cons(T, @@list[T]);
@} @}
let list[int] a = cons(7, cons(13, nil)); let a: list[int] = cons(7, cons(13, nil));
@end example @end example
@ -2266,8 +2265,8 @@ the record type.
An example of a record type and its use: An example of a record type and its use:
@example @example
type point = @{x: int, y: int@}; type point = @{x: int, y: int@};
let point p = @{x: 10, y: 11@}; let p: point = @{x: 10, y: 11@};
let int px = p.x; let px: int = p.x;
@end example @end example
@node Ref.Type.Tup @node Ref.Type.Tup
@ -2284,7 +2283,7 @@ like a record, in order specified by the tuple type.
An example of a tuple type and its use: An example of a tuple type and its use:
@example @example
type pair = tup(int,str); type pair = tup(int,str);
let pair p = tup(10,"hello"); let p: pair = tup(10,"hello");
assert (p._0 == 10); assert (p._0 == 10);
p._1 = "world"; p._1 = "world";
assert (p._1 == "world"); assert (p._1 == "world");
@ -2307,9 +2306,9 @@ interval -- out of the sliced vector.
An example of a vector type and its use: An example of a vector type and its use:
@example @example
let [int] v = [7, 5, 3]; let v: [int] = [7, 5, 3];
let int i = v.(2); let i: int = v.(2);
let [int] v2 = v.(0,1); // Form a slice. let v2: [int] = v.(0,1); // Form a slice.
@end example @end example
Vectors always @emph{allocate} a storage region sufficient to store the first Vectors always @emph{allocate} a storage region sufficient to store the first
@ -2318,7 +2317,7 @@ vector. This behaviour supports idiomatic in-place ``growth'' of a mutable
slot holding a vector: slot holding a vector:
@example @example
let mutable vec[int] v = [1, 2, 3]; let v: mutable vec[int] = [1, 2, 3];
v += [4, 5, 6]; v += [4, 5, 6];
@end example @end example
@ -2363,7 +2362,7 @@ fn add(int x, int y) -> int @{
let int x = add(5,7); let int x = add(5,7);
type binop = fn(int,int) -> int; type binop = fn(int,int) -> int;
let binop bo = add; let bo: binop = add;
x = bo(5,7); x = bo(5,7);
@end example @end example
@ -2411,8 +2410,8 @@ mutable values can pass over a port or channel.
An example of a @code{port} type: An example of a @code{port} type:
@example @example
type port[vec[str]] svp; type port[vec[str]] svp;
let svp p = get_port(); let p: svp = get_port();
let vec[str] v; let v: vec[str];
v <- p; v <- p;
@end example @end example
@ -2450,8 +2449,8 @@ message is dropped.
An example of a @code{chan} type: An example of a @code{chan} type:
@example @example
type chan[vec[str]] svc; type chan[vec[str]] svc;
let svc c = get_chan(); let c: svc = get_chan();
let vec[str] v = ["hello", "world"]; let v: vec[str] = ["hello", "world"];
c <| v; c <| v;
@end example @end example
@ -2523,10 +2522,10 @@ fn give_ints(taker t) @{
t.take(3); t.take(3);
@} @}
let port[int] p = port(); let p: port[int] = port();
let taker t1 = adder(0); let t1: taker = adder(0);
let taker t2 = sender(chan(p)); let t2: taker = sender(chan(p));
give_ints(t1); give_ints(t1);
give_ints(t2); give_ints(t2);
@ -2554,10 +2553,10 @@ An example of a constrained type with two separate instantiations:
@example @example
type ordered_range = @{low: int, high: int@} : less_than(*.low, *.high); type ordered_range = @{low: int, high: int@} : less_than(*.low, *.high);
let ordered_range rng1 = @{low: 5, high: 7@}; let rng1: ordered_range = @{low: 5, high: 7@};
// implicit: 'check less_than(rng1.low, rng1.high);' // implicit: 'check less_than(rng1.low, rng1.high);'
let ordered_range rng2 = @{low: 15, high: 17@}; let rng2: ordered_range = @{low: 15, high: 17@};
// implicit: 'check less_than(rng2.low, rng2.high);' // implicit: 'check less_than(rng2.low, rng2.high);'
@end example @end example
@ -2696,8 +2695,8 @@ pred is_less_than(int a, int b) -> bool @{
@} @}
fn test() @{ fn test() @{
let int x = 10; let x: int = 10;
let int y = 20; let y: int = 20;
check is_less_than(x,y); check is_less_than(x,y);
@} @}
@end example @end example
@ -2890,27 +2889,27 @@ declaring a function-local item.
@cindex Local slot @cindex Local slot
@cindex Variable, see @i{Local slot} @cindex Variable, see @i{Local slot}
@cindex Type inference @cindex Type inference
@cindex Automatic slot
A @code{slot declaration statement} has one one of two forms: A @code{slot declaration statement} has one one of two forms:
@itemize @itemize
@item @code{let} @var{type} @var{slot} @var{optional-init}; @item @code{let} @var{pattern} @var{optional-init};
@item @code{auto} @var{slot} @var{optional-init}; @item @code{let} @var{pattern} : @var{type} @var{optional-init};
@end itemize @end itemize
Where @var{type} is a type expression, @var{slot} is the name of the slot Where @var{type} is a type expression, @var{pattern} is an irrefutable pattern
being declared, and @var{optional-init} is either the empty string or an (often just the name of a single slot), and @var{optional-init} is an optional
equals sign (@code{=}) followed by a primitive expression. initializer. If present, the initializer consists of either an equals sign
(@code{=}) or move operator (@code{<-}), followed by an expression.
Both forms introduce a new slot into the containing block scope. The new slot Both forms introduce a new slot into the containing block scope. The new slot
is visible across the entire scope, but is initialized only at the point is visible across the entire scope, but is initialized only at the point
following the declaration statement. following the declaration statement.
The latter (@code{auto}) form of slot declaration causes the compiler to infer The former form, with no type annotation, causes the compiler to infer the
the static type of the slot through unification with the types of values static type of the slot through unification with the types of values assigned
assigned to the slot in the remaining code in the block scope. Inference only to the slot in the remaining code in the block scope. Inference only occurs on
occurs on frame-local slots, not argument slots. Function, iterator and object frame-local slots, not argument slots. Function, iterator and object
signatures must always declared types for all argument slots. signatures must always declared types for all argument slots.
@xref{Ref.Mem.Slot}. @xref{Ref.Mem.Slot}.
@ -2953,8 +2952,7 @@ effects of the expression's evaluation.
* Ref.Expr.Prove:: Expression for static assertion of typestate. * Ref.Expr.Prove:: Expression for static assertion of typestate.
* Ref.Expr.Check:: Expression for dynamic assertion of typestate. * Ref.Expr.Check:: Expression for dynamic assertion of typestate.
* Ref.Expr.Claim:: Expression for static (unsafe) or dynamic assertion of typestate. * Ref.Expr.Claim:: Expression for static (unsafe) or dynamic assertion of typestate.
* Ref.Expr.Assert:: Expression for halting the program if a * Ref.Expr.Assert:: Expression for halting the program if a boolean condition fails to hold.
boolean condition fails to hold.
* Ref.Expr.IfCheck:: Expression for dynamic testing of typestate. * Ref.Expr.IfCheck:: Expression for dynamic testing of typestate.
@end menu @end menu
@ -3012,11 +3010,11 @@ fn helper(chan[u8] out) @{
out <| result; out <| result;
@} @}
let port[u8] out; let out: port[u8];
let task p = spawn helper(chan(out)); let p: task = spawn helper(chan(out));
let task p2 = spawn "my_helper" helper(chan(out)); let p2: task = spawn "my_helper" helper(chan(out));
// let task run, do other things. // let task run, do other things.
auto result <- out; let result <- out;
@end example @end example
@ -3070,7 +3068,7 @@ un-blocks the receiving task. @xref{Ref.Run.Comm}.
An example of a @emph{receive}: An example of a @emph{receive}:
@example @example
port[str] p = @dots{}; port[str] p = @dots{};
let str s <- p; let s: str <- p;
@end example @end example
@node Ref.Expr.Call @node Ref.Expr.Call
@ -3090,7 +3088,7 @@ typestates propagate through function boundaries. @xref{Ref.Typestate}.
An example of a call expression: An example of a call expression:
@example @example
let int x = add(1, 2); let x: int = add(1, 2);
@end example @end example
@node Ref.Expr.Bind @node Ref.Expr.Bind
@ -3121,9 +3119,9 @@ fn add(int x, int y) -> int @{
@} @}
type single_param_fn = fn(int) -> int; type single_param_fn = fn(int) -> int;
let single_param_fn add4 = bind add(4, _); let add4: single_param_fn = bind add(4, _);
let single_param_fn add5 = bind add(_, 5); let add5: single_param_fn = bind add(_, 5);
assert (add(4,5) == add4(5)); assert (add(4,5) == add4(5));
assert (add(4,5) == add5(4)); assert (add(4,5) == add5(4));
@ -3368,7 +3366,7 @@ run the loop over the slice.
Example of 4 for loops, all identical: Example of 4 for loops, all identical:
@example @example
let vec[foo] v = [a, b, c]; let v: vec[foo] = [a, b, c];
for (foo e in v.(0, _vec.len(v))) @{ for (foo e in v.(0, _vec.len(v))) @{
bar(e); bar(e);
@ -3400,8 +3398,8 @@ the iterator. When the iterator returns or fails, the loop terminates.
Example of a foreach loop: Example of a foreach loop:
@example @example
let str txt; let txt: str;
let vec[str] lines; let lines: vec[str];
for each (str s in _str.split(txt, "\n")) @{ for each (str s in _str.split(txt, "\n")) @{
vec.push(lines, s); vec.push(lines, s);
@} @}
@ -3472,10 +3470,10 @@ branches to the block of that arm.
An example of a communication @code{alt} expression: An example of a communication @code{alt} expression:
@example @example
let chan c[int] = foo(); let c: chan[int] = foo();
let port p[str] = bar(); let p: port[str] = bar();
let int x = 0; let x: int = 0;
let vec[str] strs; let strs: vec[str];
alt @{ alt @{
case (str s <- p) @{ case (str s <- p) @{
@ -3504,15 +3502,15 @@ To execute a pattern @code{alt} expression, first the head expression is
evaluated, then its value is sequentially compared to the patterns in the arms evaluated, then its value is sequentially compared to the patterns in the arms
until a match is found. The first arm with a matching @code{case} pattern is until a match is found. The first arm with a matching @code{case} pattern is
chosen as the branch target of the @code{alt}, any variables bound by the chosen as the branch target of the @code{alt}, any variables bound by the
pattern are assigned to local @emph{auto} slots in the arm's block, and pattern are assigned to local slots in the arm's block, and control enters the
control enters the block. block.
An example of a pattern @code{alt} expression: An example of a pattern @code{alt} expression:
@example @example
type list[X] = tag(nil, cons(X, @@list[X])); type list[X] = tag(nil, cons(X, @@list[X]));
let list[int] x = cons(10, cons(11, nil)); let x: list[int] = cons(10, cons(11, nil));
alt (x) @{ alt (x) @{
case (cons(a, cons(b, _))) @{ case (cons(a, cons(b, _))) @{
@ -3544,7 +3542,7 @@ the value in the @code{any}.
An example of an @code{alt type} expression: An example of an @code{alt type} expression:
@example @example
let any x = foo(); let x: any = foo();
alt type (x) @{ alt type (x) @{
case (int i) @{ case (int i) @{
@ -3606,7 +3604,7 @@ fn print_even(int x) : even(x) @{
@} @}
fn test() @{ fn test() @{
let int y = 8; let y: int = 8;
// Cannot call print_even(y) here. // Cannot call print_even(y) here.