@@ -42,11 +42,11 @@ rvalue context. All other expression contexts are rvalue contexts.
42
42
43
43
When an lvalue is evaluated in an _ rvalue context_ , it denotes the value held
44
44
_ in_ that memory location. If value is of a type that implements ` Copy ` , then
45
- the value will be copied. In other situations if the type of the value is
46
- [ ` Sized ` ] ( the-sized-trait.html ) it may be possible to move the value. Only the
47
- following lvalues may be moved out of:
45
+ the value will be copied. In the remaining situations if the type of the value
46
+ is [ ` Sized ` ] ( the-sized-trait.html ) it may be possible to move the value. Only
47
+ the following lvalues may be moved out of:
48
48
49
- * [ Variables] ( # variables.html) which are not currently borrowed.
49
+ * [ Variables] ( variables.html ) which are not currently borrowed.
50
50
* [ Temporary values] ( #temporary-lifetimes ) .
51
51
* [ Fields] ( #field-expressions ) of an lvalue which can be moved out of and
52
52
doesn't implement [ ` Drop ` ] ( #the-drop-trait ) .
@@ -67,14 +67,14 @@ _immutable_.
67
67
68
68
The following expressions can create mutable lvalues:
69
69
70
- * Mutable [ variables] ( # variables.html) , which are not currently borrowed.
70
+ * Mutable [ variables] ( variables.html ) , which are not currently borrowed.
71
71
* [ Mutable ` static ` items] ( items.html#mutable-statics ) .
72
72
* [ Temporary values] ( #temporary-lifetimes ) .
73
- * [ Fields] ( #field-expressions ) , this evaluates the expression in a mutable
73
+ * [ Fields] ( #field-expressions ) , this evaluates the subexpression in a mutable
74
74
lvalue context.
75
75
* [ Dereferenes] ( #the-dereference-operator ) of a ` *mut T ` pointer.
76
76
* Dereference of a variable, or field of a variable, with type ` &mut T ` . Note:
77
- this is an exception to the next rule.
77
+ this is an exception to the requirement for the next rule.
78
78
* Dereferences of a type that implements ` DerefMut ` , this then requires that
79
79
the value being dereferenced is evaluated is a mutable lvalue context.
80
80
* [ Indexing] ( #index-expressions ) of a type that implements ` DerefMut ` , this
@@ -127,8 +127,13 @@ borrowing it. For example, it is possible to compare two unsized
127
127
operator implicitly borrows it's operands:
128
128
129
129
``` rust
130
- let a : & [i32 ] = & [1 , 2 , 3 ];
131
- let b : & [i32 ] = & vec! [1 , 2 , 3 ];
130
+ # let c = [1 , 2 , 3 ];
131
+ # let d = vec! [1 , 2 , 3 ];
132
+ let a : & [i32 ];
133
+ let b : & [i32 ];
134
+ # a = & c ;
135
+ # b = & d ;
136
+ // ...
132
137
* a == * b ;
133
138
// Equivalent form:
134
139
:: std :: cmp :: PartialEq :: eq (& * a , & * b );
@@ -144,12 +149,6 @@ Implicit borrows may be taken in the following expressions:
144
149
* Operands of [ comparison operators] ( #comparison-operators ) .
145
150
* Left operands of the [ compound assignment] ( #compound-assignment-expressions ) .
146
151
147
- ## Traits
148
-
149
- Many of the following operators and expressions can also be overloaded for
150
- other types using traits in ` std::ops ` or ` std::cmp ` , these traits here also
151
- exist in ` core::ops ` and ` core::cmp ` with the same names.
152
-
153
152
## Constant expressions
154
153
155
154
Certain types of expressions can be evaluated at compile time. These are called
@@ -166,10 +165,11 @@ The following expressions are constant expressions, so long as any operands are
166
165
also constant expressions:
167
166
168
167
* [ Literals] ( #literal-expressions ) .
169
- * [ Paths] ( #paths ) to [ functions] ( items.html#functions ) and constants. Recursively
170
- defining constants is not allowed.
171
- * Statics, so long as only their address, not their value, is used: even
172
- indirectly through a compilicated constant expression. \*
168
+ * [ Paths] ( #paths ) to [ functions] ( items.html#functions ) and constants.
169
+ Recursively defining constants is not allowed.
170
+ * Paths to statics, so long as only their address, not their value, is used.
171
+ This includes using their value indirectly through a compilicated expression.
172
+ \*
173
173
* [ Tuple expressions] ( #tuple-expressions ) .
174
174
* [ Array expressions] ( #array-expressions ) .
175
175
* [ Struct expressions] ( #struct-expressions ) , where the type does not implement
@@ -182,8 +182,8 @@ also constant expressions:
182
182
* [ Index expressions] ( #index-expressions ) , indexing a [ array or
183
183
slice] ( types.html#array-and-slice-types ) with a ` usize ` .
184
184
* [ Range expressions] ( #range-expressions ) .
185
- * [ Closure expressions] ( #closure-expressions ) which don't capture variables from
186
- the environment.
185
+ * [ Closure expressions] ( #closure-expressions ) which don't capture variables
186
+ from the environment.
187
187
* Built in [ negation] ( #negation-operators ) , [ arithmetic,
188
188
logical] ( #arithmetic-and-logical-binary-operators ) ,
189
189
[ comparison] ( #comparison-operators ) or [ lazy
@@ -198,6 +198,12 @@ also constant expressions:
198
198
199
199
\* Only in static items.
200
200
201
+ ## Overloading Traits
202
+
203
+ Many of the following operators and expressions can also be overloaded for
204
+ other types using traits in ` std::ops ` or ` std::cmp ` , these traits here also
205
+ exist in ` core::ops ` and ` core::cmp ` with the same names.
206
+
201
207
## Literal expressions
202
208
203
209
A _ literal expression_ consists of one of the [ literal] ( tokens.html#literals )
@@ -215,20 +221,20 @@ boolean value, or the unit value.
215
221
216
222
A [ path] ( paths.html ) used as an expression context denotes either a local
217
223
variable or an item. Path expressions that resolve to local or static variables
218
- are [ lvalues] ( expressions.html#lvalues-rvalues-and-temporaries ) . Using a
219
- ` static mut ` variable requires an [ ` unsafe ` block ] ( #unsafe-block ) Other
220
- paths are rvalues .
224
+ are [ lvalues] ( expressions.html#lvalues-rvalues-and-temporaries ) , other paths
225
+ are rvalues. Using a ` static mut ` variable requires an [ ` unsafe `
226
+ block ] ( #unsafe-block ) .
221
227
222
228
``` rust
223
- mod globals {
224
- pub static STATIC_VAR : i32 = 5 ;
225
- pub static mut STATIC_MUT_VAR : i32 = 7 ;
226
- }
227
- let local_var = 3 ;
229
+ # mod globals {
230
+ # pub static STATIC_VAR : i32 = 5 ;
231
+ # pub static mut STATIC_MUT_VAR : i32 = 7 ;
232
+ # }
233
+ # let local_var = 3 ;
228
234
local_var ;
229
235
globals :: STATIC_VAR ;
230
236
unsafe { globals :: STATIC_MUT_VAR };
231
- let some_constructor = Option :: Some :: <i32 >;
237
+ let some_constructor = Some :: <i32 >;
232
238
let push_integer = Vec :: <i32 >:: push ;
233
239
let slice_reverse = <[i32 ]>:: reverse ;
234
240
```
@@ -301,7 +307,8 @@ entire expression denotes the result of constructing a new struct (with the
301
307
same type as the base expression) with the given values for the fields that
302
308
were explicitly specified and the values in the base expression for all other
303
309
fields. Just as with all struct expressions, all of the fields of the struct
304
- must be [ visible] ( visibility-and-privacy.html ) .
310
+ must be [ visible] ( visibility-and-privacy.html ) , even those not explicitly
311
+ named.
305
312
306
313
``` rust
307
314
# struct Point3d { x : i32 , y : i32 , z : i32 }
@@ -329,7 +336,7 @@ Point3d { x, y: y_value, z };
329
336
### Enumeration Variant expressions
330
337
331
338
Enumeration variants can be constructed similarly to structs, using a path to
332
- an enum variant instead of a struct:
339
+ an enum variant instead of to a struct:
333
340
334
341
``` rust
335
342
# enum Message {
@@ -345,12 +352,14 @@ let m = Message::Move { x: 50, y: 200 };
345
352
## Block expressions
346
353
347
354
A _ block expression_ is similar to a module in terms of the declarations that
348
- are possible. Each block conceptually introduces a new namespace scope. Use
355
+ are possible, but can also contain [ statements] ( statements.html ) and end with
356
+ an expression. Each block conceptually introduces a new namespace scope. Use
349
357
items can bring new names into scopes and declared items are in scope for only
350
358
the block itself.
351
359
352
360
A block will execute each statement sequentially, and then execute the
353
- expression (if given). If the block ends in a statement, its value is ` () ` :
361
+ expression (if given). If the block doesn't end in an expression, its value is
362
+ ` () ` :
354
363
355
364
``` rust
356
365
let x : () = { println! (" Hello." ); };
@@ -365,7 +374,8 @@ assert_eq!(5, x);
365
374
```
366
375
367
376
Blocks are always [ rvalues] ( #lvalues-and-rvalues ) and evaluate the last
368
- expression in rvalue context.
377
+ expression in rvalue context. This can be used to force moving a value
378
+ if really needed.
369
379
370
380
### ` unsafe ` blocks
371
381
@@ -392,6 +402,7 @@ let log_pi = pi.unwrap_or(1.0).log(2.72);
392
402
393
403
When resolving method calls on an expression of type ` A ` , Rust will use the
394
404
following order:
405
+
395
406
1 . Inherent methods, with receiver of type ` A ` , ` &A ` , ` &mut A ` .
396
407
1 . Trait methods with receiver of type ` A ` .
397
408
1 . Trait methods with receiver of type ` &A ` .
@@ -401,8 +412,8 @@ following order:
401
412
1 . If ` A ` is now an [ array] ( types.html#array-and-slice-types ) type, then
402
413
repeat steps 1-4 with the corresponding slice type.
403
414
404
- Note: that in steps 1-4 the receiver is used, not the type of ` Self ` , which may
405
- not be the same as ` A ` . For example
415
+ Note: that in steps 1-4 the receiver is used, not the type of ` Self ` nor the
416
+ type of ` A ` . For example
406
417
407
418
``` rust,ignore
408
419
// `Self` is `&A`, receiver is `&A`.
@@ -442,8 +453,8 @@ mystruct.method(); // Method expression
442
453
```
443
454
444
455
A field access is an [ lvalue] ( expressions.html#lvalues-rvalues-and-temporaries )
445
- referring to the value of that field. When the type providing the field
446
- inherits mutability, it can be [ assigned ] ( #assignment-expressions ) to .
456
+ referring to the value of that field. When the subexpression is
457
+ [ mutable ] ( # mutability) , the field expression is also mutable .
447
458
448
459
Also, if the type of the expression to the left of the dot is a pointer, it is
449
460
automatically dereferenced as many times as necessary to make the field access
@@ -453,7 +464,7 @@ Finally the fields of a struct, a reference to a struct are treated as separate
453
464
entities when borrowing. If the struct does not implement
454
465
[ ` Drop ` ] ( #the-drop-trait ) this also applies to moving out of each of its fields
455
466
where possible. This also does not apply if automatic dereferencing is done
456
- though other types.
467
+ though user defined types.
457
468
458
469
``` rust
459
470
# struct A { f1 : String , f2 : String , f3 : String }
@@ -464,18 +475,18 @@ though other types.
464
475
# };
465
476
let a : & mut String = & mut x . f1; // x.f1 borrowed mutably
466
477
let b : & String = & x . f2; // x.f2 borrowed immutably
467
- let c : & String = & x . f2;
478
+ let c : & String = & x . f2; // Can borrow again
468
479
let d : String = x . f3; // Move out of x.f3
469
480
```
470
481
471
482
### Tuple indexing expressions
472
483
473
484
[ Tuples] ( types.html#tuple-types ) and [ struct tuples] ( items.html#structs ) can be
474
485
indexed using the number corresponding to the possition of the field. The index
475
- must be a [ decimal literal] ( tokens.html#integer-literals ) with no underscores
476
- or suffix. Tuple indexing expressions also differ from field expressions in
477
- that they can unambiguously be called as a function. In all other aspects they
478
- have the same behavior.
486
+ must be written as a [ decimal literal] ( tokens.html#integer-literals ) with no
487
+ underscores or suffix. Tuple indexing expressions also differ from field
488
+ expressions in that they can unambiguously be called as a function. In all
489
+ other aspects they have the same behavior.
479
490
480
491
``` rust
481
492
# struct Point (f32 , f32 );
@@ -577,14 +588,13 @@ Refer to [RFC 132] for further details and motivations.
577
588
578
589
## Closure expressions
579
590
580
- A _ closure expression_ (sometimes called an "anonymous function expression")
581
- defines a closure and denotes it as a value, in a single expression. A closure
582
- expression is a pipe-symbol-delimited (` | ` ) list of patterns followed by an
583
- expression. Type annotations may optionally be added for the type of the
584
- parameters or for the return type. If there is a return type, the expression
585
- used for the body of the closure must be a normal [ block] ( #block-expressions ) .
586
- A closure expression also may begin with the ` move ` keyword before the initial
587
- ` | ` .
591
+ A _ closure expression_ defines a closure and denotes it as a value, in a single
592
+ expression. A closure expression is a pipe-symbol-delimited (` | ` ) list of
593
+ patterns followed by an expression. Type annotations may optionally be added
594
+ for the type of the parameters or for the return type. If there is a return
595
+ type, the expression used for the body of the closure must be a normal
596
+ [ block] ( #block-expressions ) . A closure expression also may begin with the
597
+ ` move ` keyword before the initial ` | ` .
588
598
589
599
A closure expression denotes a function that maps a list of parameters
590
600
(` ident_list ` ) onto the expression that follows the ` ident_list ` . The patterns
@@ -608,10 +618,10 @@ closure's type is `'static`.
608
618
609
619
The compiler will determine which of the [ closure
610
620
traits] ( types.html#closure-types ) the closure's type will implement by how it
611
- acts on them . The closure will also implement [ ` Send ` ] ( the-send-trait.html )
612
- and/or [ ` Sync ` ] ( the-sync-trait.html ) if all of its captured types do. These
613
- traits allow functions to accept closures using generics, even though the exact
614
- types can't be named.
621
+ acts on its captured variables . The closure will also implement
622
+ [ ` Send ` ] ( the-send-trait.html ) and/or [ ` Sync ` ] ( the-sync-trait.html ) if all of
623
+ its captured types do. These traits allow functions to accept closures using
624
+ generics, even though the exact types can't be named.
615
625
616
626
In this example, we define a function ` ten_times ` that takes a higher-order
617
627
function argument, and we then call it with a closure expression as an argument,
@@ -625,24 +635,26 @@ fn ten_times<F>(f: F) where F: Fn(i32) {
625
635
}
626
636
627
637
ten_times (| j | println! (" hello, {}" , j ));
638
+ // With type annotations
639
+ ten_times (| j : i32 | -> () { println! (" hello, {}" , j ) });
628
640
629
641
let word = " konnichiwa" . to_owned ();
630
642
ten_times (move | j | println! (" {}, {}" , word , j ));
631
643
```
632
644
633
645
## Array expressions
634
646
635
- An [ array] ( types.html#array-and-slice-types ) _ expression _ can be written by
647
+ An _ [ array] ( types.html#array-and-slice-types ) expression _ can be written by
636
648
enclosing zero or more comma-separated expressions of uniform type in square
637
649
brackets. This produces and array containing each of these values in the
638
650
order they are written.
639
651
640
652
Alternatively there can be exactly two expressions inside the brackets,
641
653
separated by a semi-colon. The expression after the ` ; ` must be a have type
642
- ` usize ` and be a constant expression that can be evaluated at compile time,
643
- such as a [ literal] ( tokens.html#literals ) or a [ constant item
644
- item] ( items.html#constant-items ) . ` [a; b] ` creates an array containing ` b ` copies
645
- of the value of ` a ` . If the expression after the semi-colon has a value
654
+ ` usize ` and be a [ constant expression] ( #constant-expressions ) , such as a
655
+ [ literal] ( tokens.html#literals ) or a [ constant
656
+ item] ( items.html#constant-items ) . ` [a; b] ` creates an array containing ` b `
657
+ copies of the value of ` a ` . If the expression after the semi-colon has a value
646
658
greater than 1 then this requires that the type of ` a ` is
647
659
[ ` Copy ` ] ( the-copy-trait.html ) .
648
660
@@ -715,6 +727,7 @@ Integer operators will panic when they overflow when compiled in debug mode.
715
727
The ` -C debug-assertions ` and ` -C overflow-checks ` compiler flags can be used
716
728
to control this more directly. The following things are considered to be
717
729
overflow:
730
+
718
731
* When ` + ` , ` * ` or ` - ` create a value greater than the maximum value, or less
719
732
than the minimum value that can be stored. This includes unary ` - ` on the
720
733
smallest value of any signed integer type.
@@ -759,7 +772,7 @@ resulting [lvalue](expressions.html#lvalues-rvalues-and-temporaries) can be
759
772
assigned to. Dereferencing a raw pointer requires ` unsafe ` .
760
773
761
774
On non-pointer types ` *x ` is equivalent to ` *std::ops::Deref::deref(&x) ` in an
762
- [ immutable lvalue context] ( #mutability ) and`* std::ops::Deref::deref_mut(&mut
775
+ [ immutable lvalue context] ( #mutability ) and `* std::ops::Deref::deref_mut(&mut
763
776
x)` in a mutable lvalue context.
764
777
765
778
``` rust
@@ -793,11 +806,11 @@ println!("{:?}", res);
793
806
794
807
### Negation operators
795
808
796
- This table summarizes the behavior of the last two unary operators on
797
- primitive types and which traits are used to overload these operators for other
798
- types. Remember that signed integers are always represented using two's
799
- complement. The operands of all of these operators are evaluated in rvalue
800
- context and are moved or copied.
809
+ These are the last two unary operators. This table summarizes the behavior of
810
+ them on primitive types and which traits are used to overload these operators
811
+ for other types. Remember that signed integers are always represented using
812
+ two's complement. The operands of all of these operators are evaluated in
813
+ rvalue context so are moved or copied.
801
814
802
815
| Symbol | Integer | ` bool ` | Floating Point | Overloading Trait |
803
816
| --------| -------------| -------------| ----------------| --------------------|
@@ -822,7 +835,7 @@ summarizes the behavior of arithmetic and logical binary operators on
822
835
primitive types and which traits are used to overload these operators for other
823
836
types. Remember that signed integers are always represented using two's
824
837
complement. The operands of all of these operators are evaluated in rvalue
825
- context and are moved or copied.
838
+ context so are moved or copied.
826
839
827
840
| Symbol | Integer | ` bool ` | Floating Point | Overloading Trait |
828
841
| --------| -------------------------| -------------| ----------------| --------------------|
@@ -934,8 +947,8 @@ fn average(values: &[f64]) -> f64 {
934
947
```
935
948
936
949
` as ` can be used to explicitly perform [ coercions] ( type-coercions.html ) , as
937
- well as the following additional casts. ` *T ` is short for either
938
- ` *const T ` or ` * mut T` .
950
+ well as the following additional casts. Here ` *T ` means either ` *const T ` or
951
+ ` *mut T ` .
939
952
940
953
| Type of ` e ` | ` U ` | Cast performed by ` e as U ` |
941
954
| -----------------------| -----------------------| ----------------------------------|
@@ -950,7 +963,10 @@ well as the following additional casts. `*T` is short for either
950
963
| [ Function pointer] ( type.html#function-types ) | ` *V ` where ` V: Sized ` | Function pointer to pointer cast |
951
964
| Function pointer | Integer | Function pointer to address cast |
952
965
953
- \* or ` T ` and ` V ` are compatible unsized types, e.g., both slices.
966
+ \* or ` T ` and ` V ` are compatible unsized types, e.g., both slices, both the
967
+ same trait object.
968
+
969
+ #### Semantics
954
970
955
971
* Numeric cast
956
972
* Casting between two integers of the same size (e.g. i32 -> u32) is a no-op
@@ -1041,7 +1057,7 @@ given by their associativity.
1041
1057
| <code >| ;| ; </code > | left to right |
1042
1058
| ` .. ` ` ... ` | Require parentheses |
1043
1059
| ` <- ` | right to left |
1044
- | ` = ` ` += ` ` -= ` ` *= ` ` /= ` ` %= ` ` &= ` <code >| ; =</code > ` ^= ` ` <<= ` ` >>= ` | right to left |
1060
+ | ` = ` ` += ` ` -= ` ` *= ` ` /= ` ` %= ` < br > ` &= ` <code >| ; =</code > ` ^= ` ` <<= ` ` >>= ` | right to left |
1045
1061
1046
1062
## Grouped expressions
1047
1063
@@ -1402,7 +1418,7 @@ condition expression it expects the keyword `let` followed by a refutable
1402
1418
pattern, an ` = ` and an expression. If the value of the expression on the right
1403
1419
hand side of the ` = ` matches the pattern, the loop body block executes then
1404
1420
control returns to the pattern matching statement. Otherwise, the while
1405
- expression completes. Like ` while ` loops, ` while let ` loops evaluate to ` () ` .
1421
+ expression completes.
1406
1422
1407
1423
``` rust
1408
1424
let mut x = vec! [1 , 2 , 3 ];
0 commit comments