Skip to content

Commit 5b10f4e

Browse files
committed
Miscellaneous documentation additions.
Added notes explaining how [expr, ..expr] form is used, targeted at individuals like me who thought it was more general and handled dynamic repeat expressions. (I left a TODO for this section in a comment, but perhaps that is bad form for the manual...) Added example of `do` syntax with a function of arity > 1; yes, one should be able to derive this from the text above it, but it is still a useful detail to compare and contrast against the arity == 1 case. Added example of using for expression over a uint range, since someone who is most used to write `for(int i; i < lim; i++) { ... }` will likely want to know how to translate that form (regardless of whether it happens to be good style or not for their use-case). Added note about the semi-strange meaning of "fixed size" of vectors in the vector type section.
1 parent 125cdf5 commit 5b10f4e

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

doc/rust.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,12 @@ vec_elems : [expr [',' expr]*] | [expr ',' ".." expr]
16711671
A [_vector_](#vector-types) _expression_ is written by enclosing zero or
16721672
more comma-separated expressions of uniform type in square brackets.
16731673

1674+
In the `[expr ',' ".." expr]` form, the expression after the `".."`
1675+
must be an expression form that can be evaluated at compile time, such
1676+
as a [literal](#literals) or a [constant](#constants).
1677+
1678+
<!--- TODO: elaborate the actual subgrammar for constant expressions -->
1679+
16741680
~~~~
16751681
[1, 2, 3, 4];
16761682
["a", "b", "c", "d"];
@@ -2156,6 +2162,19 @@ do f |j| {
21562162
}
21572163
~~~~
21582164

2165+
In this example, both calls to the (binary) function `k` are equivalent:
2166+
2167+
~~~~
2168+
# fn k(x:int, f: &fn(int)) { }
2169+
# fn l(i: int) { }
2170+
2171+
k(3, |j| l(j));
2172+
2173+
do k(3) |j| {
2174+
l(j);
2175+
}
2176+
~~~~
2177+
21592178

21602179
### For expressions
21612180

@@ -2184,7 +2203,7 @@ and early boolean-valued returns from the `block` function,
21842203
such that the meaning of `break` and `loop` is preserved in a primitive loop
21852204
when rewritten as a `for` loop controlled by a higher order function.
21862205

2187-
An example a for loop:
2206+
An example of a for loop over the contents of a vector:
21882207

21892208
~~~~
21902209
# type foo = int;
@@ -2198,6 +2217,14 @@ for v.each |e| {
21982217
}
21992218
~~~~
22002219

2220+
An example of a for loop over a series of integers:
2221+
2222+
~~~~
2223+
# fn bar(b:uint) { }
2224+
for uint::range(0, 256) |i| {
2225+
bar(i);
2226+
}
2227+
~~~~
22012228

22022229
### If expressions
22032230

@@ -2474,6 +2501,7 @@ fail_unless!(b != "world");
24742501

24752502
The vector type constructor represents a homogeneous array of values of a given type.
24762503
A vector has a fixed size.
2504+
(Operations like `vec::push` operate solely on owned vectors.)
24772505
A vector type can be annotated with a _definite_ size,
24782506
written with a trailing asterisk and integer literal, such as `[int * 10]`.
24792507
Such a definite-sized vector type is a first-class type, since its size is known statically.
@@ -2484,6 +2512,10 @@ such as `&[T]`, `@[T]` or `~[T]`.
24842512
The kind of a vector type depends on the kind of its element type,
24852513
as with other simple structural types.
24862514

2515+
Expressions producing vectors of definite size cannot be evaluated in a
2516+
context expecting a vector of indefinite size; one must copy the
2517+
definite-sized vector contents into a distinct vector of indefinite size.
2518+
24872519
An example of a vector type and its use:
24882520

24892521
~~~~

0 commit comments

Comments
 (0)