You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: branches/try2/doc/complement-cheatsheet.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -62,8 +62,8 @@ let reader : File = File::open(&path).unwrap_or_else(on_error);
62
62
Use the [`lines`](http://static.rust-lang.org/doc/master/std/io/trait.Buffer.html#method.lines) method on a [`BufferedReader`](http://static.rust-lang.org/doc/master/std/io/buffered/struct.BufferedReader.html).
(`expr[expr]`), and [field references](#field-expressions) (`expr.f`).
2210
2212
All other expressions are rvalues.
2211
2213
2212
-
The left operand of an [assignment](#assignment-expressions),
2213
-
[binary move](#binary-move-expressions) or
2214
+
The left operand of an [assignment](#assignment-expressions) or
2214
2215
[compound-assignment](#compound-assignment-expressions) expression is an lvalue context,
2215
-
as is the single operand of a unary [borrow](#unary-operator-expressions),
2216
-
or [move](#unary-move-expressions) expression,
2217
-
and _both_ operands of a [swap](#swap-expressions) expression.
2216
+
as is the single operand of a unary [borrow](#unary-operator-expressions).
2218
2217
All other expression contexts are rvalue contexts.
2219
2218
2220
2219
When an lvalue is evaluated in an _lvalue context_, it denotes a memory location;
@@ -2227,9 +2226,8 @@ A temporary's lifetime equals the largest lifetime of any reference that points
2227
2226
2228
2227
When a [local variable](#memory-slots) is used
2229
2228
as an [rvalue](#lvalues-rvalues-and-temporaries)
2230
-
the variable will either be [moved](#move-expressions) or copied,
2231
-
depending on its type.
2232
-
For types that contain [owning pointers](#owning-pointers)
2229
+
the variable will either be moved or copied, depending on its type.
2230
+
For types that contain [owning pointers](#pointer-types)
2233
2231
or values that implement the special trait `Drop`,
2234
2232
the variable is moved.
2235
2233
All other types are copied.
@@ -2318,14 +2316,24 @@ let base = Point3d {x: 1, y: 2, z: 3};
2318
2316
Point3d {y: 0, z: 10, .. base};
2319
2317
~~~~
2320
2318
2321
-
### Record expressions
2319
+
### Block expressions
2322
2320
2323
2321
~~~~{.ebnf .gram}
2324
-
rec_expr : '{' ident ':' expr
2325
-
[ ',' ident ':' expr ] *
2326
-
[ ".." expr ] '}'
2322
+
block_expr : '{' [ view_item ] *
2323
+
[ stmt ';' | item ] *
2324
+
[ expr ] '}'
2327
2325
~~~~
2328
2326
2327
+
A _block expression_ is similar to a module in terms of the declarations that
2328
+
are possible. Each block conceptually introduces a new namespace scope. View
2329
+
items can bring new names into scopes and declared items are in scope for only
2330
+
the block itself.
2331
+
2332
+
A block will execute each statement sequentially, and then execute the
2333
+
expression (if given). If the final expression is omitted, the type and return
2334
+
value of the block are `()`, but if it is provided, the type and return value
2335
+
of the block are that of the expression itself.
2336
+
2329
2337
### Method-call expressions
2330
2338
2331
2339
~~~~{.ebnf .gram}
@@ -2878,16 +2886,26 @@ match x {
2878
2886
2879
2887
The first pattern matches lists constructed by applying `Cons` to any head value, and a
2880
2888
tail value of `~Nil`. The second pattern matches _any_ list constructed with `Cons`,
2881
-
ignoring the values of its arguments. The difference between `_` and `*` is that the pattern `C(_)` is only type-correct if
2882
-
`C` has exactly one argument, while the pattern `C(..)` is type-correct for any enum variant `C`, regardless of how many arguments `C` has.
2883
-
2884
-
To execute an `match` expression, first the head expression is evaluated, then
2885
-
its value is sequentially compared to the patterns in the arms until a match
2889
+
ignoring the values of its arguments. The difference between `_` and `*` is that the pattern
2890
+
`C(_)` is only type-correct if `C` has exactly one argument, while the pattern `C(..)` is
2891
+
type-correct for any enum variant `C`, regardless of how many arguments `C` has.
2892
+
2893
+
A `match` behaves differently depending on whether or not the head expression
2894
+
is an [lvalue or an rvalue](#lvalues-rvalues-and-temporaries).
2895
+
If the head expression is an rvalue, it is
2896
+
first evaluated into a temporary location, and the resulting value
2897
+
is sequentially compared to the patterns in the arms until a match
2886
2898
is found. The first arm with a matching pattern is chosen as the branch target
2887
2899
of the `match`, any variables bound by the pattern are assigned to local
2888
2900
variables in the arm's block, and control enters the block.
2889
2901
2890
-
An example of an `match` expression:
2902
+
When the head expression is an lvalue, the match does not allocate a
2903
+
temporary location (however, a by-value binding may copy or move from
2904
+
the lvalue). When possible, it is preferable to match on lvalues, as the
2905
+
lifetime of these matches inherits the lifetime of the lvalue, rather
2906
+
than being restricted to the inside of the match.
2907
+
2908
+
An example of a `match` expression:
2891
2909
2892
2910
~~~~
2893
2911
# fn process_pair(a: int, b: int) { }
@@ -2917,19 +2935,31 @@ Patterns that bind variables
2917
2935
default to binding to a copy or move of the matched value
2918
2936
(depending on the matched value's type).
2919
2937
This can be changed to bind to a reference by
2920
-
using the ```ref``` keyword,
2921
-
or to a mutable reference using ```ref mut```.
2922
-
2923
-
A pattern that's just an identifier,
2924
-
like `Nil` in the previous answer,
2925
-
could either refer to an enum variant that's in scope,
2926
-
or bind a new variable.
2927
-
The compiler resolves this ambiguity by forbidding variable bindings that occur in ```match``` patterns from shadowing names of variants that are in scope.
2928
-
For example, wherever ```List``` is in scope,
2929
-
a ```match``` pattern would not be able to bind ```Nil``` as a new name.
2930
-
The compiler interprets a variable pattern `x` as a binding _only_ if there is no variant named `x` in scope.
2931
-
A convention you can use to avoid conflicts is simply to name variants with upper-case letters,
2932
-
and local variables with lower-case letters.
2938
+
using the `ref` keyword,
2939
+
or to a mutable reference using `ref mut`.
2940
+
2941
+
Patterns can also dereference pointers by using the `&`,
2942
+
`~` or `@` symbols, as appropriate. For example, these two matches
2943
+
on `x: &int` are equivalent:
2944
+
2945
+
~~~~
2946
+
# let x = &3;
2947
+
let y = match *x { 0 => "zero", _ => "some" };
2948
+
let z = match x { &0 => "zero", _ => "some" };
2949
+
2950
+
assert_eq!(y, z);
2951
+
~~~~
2952
+
2953
+
A pattern that's just an identifier, like `Nil` in the previous answer,
2954
+
could either refer to an enum variant that's in scope, or bind a new variable.
2955
+
The compiler resolves this ambiguity by forbidding variable bindings that occur
2956
+
in `match` patterns from shadowing names of variants that are in scope.
2957
+
For example, wherever `List` is in scope,
2958
+
a `match` pattern would not be able to bind `Nil` as a new name.
2959
+
The compiler interprets a variable pattern `x` as a binding _only_ if there is
2960
+
no variant named `x` in scope.
2961
+
A convention you can use to avoid conflicts is simply to name variants with
2962
+
upper-case letters, and local variables with lower-case letters.
2933
2963
2934
2964
Multiple match patterns may be joined with the `|` operator.
2935
2965
A range of values may be specified with `..`.
@@ -3110,19 +3140,20 @@ A `struct` *type* is a heterogeneous product of other types, called the *fields*
3110
3140
the *record* types of the ML family,
3111
3141
or the *structure* types of the Lisp family.]
3112
3142
3113
-
New instances of a `struct` can be constructed with a [struct expression](#struct-expressions).
3143
+
New instances of a `struct` can be constructed with a [struct expression](#structure-expressions).
3114
3144
3115
3145
The memory order of fields in a `struct` is given by the item defining it.
3116
3146
Fields may be given in any order in a corresponding struct *expression*;
3117
3147
the resulting `struct` value will always be laid out in memory in the order specified by the corresponding *item*.
3118
3148
3119
-
The fields of a `struct` may be qualified by [visibility modifiers](#visibility-modifiers),
3149
+
The fields of a `struct` may be qualified by [visibility modifiers](#re-exporting-and-visibility),
3120
3150
to restrict access to implementation-private data in a structure.
3121
3151
3122
3152
A _tuple struct_ type is just like a structure type, except that the fields are anonymous.
3123
3153
3124
3154
A _unit-like struct_ type is like a structure type, except that it has no fields.
3125
-
The one value constructed by the associated [structure expression](#structure-expression) is the only value that inhabits such a type.
3155
+
The one value constructed by the associated [structure expression](#structure-expressions)
3156
+
is the only value that inhabits such a type.
3126
3157
3127
3158
### Enumerated types
3128
3159
@@ -3793,7 +3824,7 @@ over the output format of a Rust crate.
3793
3824
### Logging system
3794
3825
3795
3826
The runtime contains a system for directing [logging
3796
-
expressions](#log-expressions) to a logging console and/or internal logging
3827
+
expressions](#logging-expressions) to a logging console and/or internal logging
3797
3828
buffers. Logging can be enabled per module.
3798
3829
3799
3830
Logging output is enabled by setting the `RUST_LOG` environment
0 commit comments