@@ -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
@@ -401,8 +411,8 @@ following order:
401
411
1 . If ` A ` is now an [ array] ( types.html#array-and-slice-types ) type, then
402
412
repeat steps 1-4 with the corresponding slice type.
403
413
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
414
+ Note: that in steps 1-4 the receiver is used, not the type of ` Self ` nor the
415
+ type of ` A ` . For example
406
416
407
417
``` rust,ignore
408
418
// `Self` is `&A`, receiver is `&A`.
@@ -442,8 +452,8 @@ mystruct.method(); // Method expression
442
452
```
443
453
444
454
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 .
455
+ referring to the value of that field. When the subexpression is
456
+ [ mutable ] ( # mutability) , the field expression is also mutable .
447
457
448
458
Also, if the type of the expression to the left of the dot is a pointer, it is
449
459
automatically dereferenced as many times as necessary to make the field access
@@ -453,7 +463,7 @@ Finally the fields of a struct, a reference to a struct are treated as separate
453
463
entities when borrowing. If the struct does not implement
454
464
[ ` Drop ` ] ( #the-drop-trait ) this also applies to moving out of each of its fields
455
465
where possible. This also does not apply if automatic dereferencing is done
456
- though other types.
466
+ though user defined types.
457
467
458
468
``` rust
459
469
# struct A { f1 : String , f2 : String , f3 : String }
@@ -464,18 +474,18 @@ though other types.
464
474
# };
465
475
let a : & mut String = & mut x . f1; // x.f1 borrowed mutably
466
476
let b : & String = & x . f2; // x.f2 borrowed immutably
467
- let c : & String = & x . f2;
477
+ let c : & String = & x . f2; // Can borrow again
468
478
let d : String = x . f3; // Move out of x.f3
469
479
```
470
480
471
481
### Tuple indexing expressions
472
482
473
483
[ Tuples] ( types.html#tuple-types ) and [ struct tuples] ( items.html#structs ) can be
474
484
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.
485
+ must be written as a [ decimal literal] ( tokens.html#integer-literals ) with no
486
+ underscores or suffix. Tuple indexing expressions also differ from field
487
+ expressions in that they can unambiguously be called as a function. In all
488
+ other aspects they have the same behavior.
479
489
480
490
``` rust
481
491
# struct Point (f32 , f32 );
@@ -577,14 +587,13 @@ Refer to [RFC 132] for further details and motivations.
577
587
578
588
## Closure expressions
579
589
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
- ` | ` .
590
+ A _ closure expression_ defines a closure and denotes it as a value, in a single
591
+ expression. A closure expression is a pipe-symbol-delimited (` | ` ) list of
592
+ patterns followed by an expression. Type annotations may optionally be added
593
+ for the type of the parameters or for the return type. If there is a return
594
+ type, the expression used for the body of the closure must be a normal
595
+ [ block] ( #block-expressions ) . A closure expression also may begin with the
596
+ ` move ` keyword before the initial ` | ` .
588
597
589
598
A closure expression denotes a function that maps a list of parameters
590
599
(` ident_list ` ) onto the expression that follows the ` ident_list ` . The patterns
@@ -608,10 +617,10 @@ closure's type is `'static`.
608
617
609
618
The compiler will determine which of the [ closure
610
619
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.
620
+ acts on its captured variables . The closure will also implement
621
+ [ ` Send ` ] ( the-send-trait.html ) and/or [ ` Sync ` ] ( the-sync-trait.html ) if all of
622
+ its captured types do. These traits allow functions to accept closures using
623
+ generics, even though the exact types can't be named.
615
624
616
625
In this example, we define a function ` ten_times ` that takes a higher-order
617
626
function argument, and we then call it with a closure expression as an argument,
@@ -625,24 +634,26 @@ fn ten_times<F>(f: F) where F: Fn(i32) {
625
634
}
626
635
627
636
ten_times (| j | println! (" hello, {}" , j ));
637
+ // With type annotations
638
+ ten_times (| j : i32 | -> () { println! (" hello, {}" , j ) });
628
639
629
640
let word = " konnichiwa" . to_owned ();
630
641
ten_times (move | j | println! (" {}, {}" , word , j ));
631
642
```
632
643
633
644
## Array expressions
634
645
635
- An [ array] ( types.html#array-and-slice-types ) _ expression _ can be written by
646
+ An _ [ array] ( types.html#array-and-slice-types ) expression _ can be written by
636
647
enclosing zero or more comma-separated expressions of uniform type in square
637
648
brackets. This produces and array containing each of these values in the
638
649
order they are written.
639
650
640
651
Alternatively there can be exactly two expressions inside the brackets,
641
652
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
653
+ ` usize ` and be a [ constant expression] ( #constant-expressions ) , such as a
654
+ [ literal] ( tokens.html#literals ) or a [ constant
655
+ item] ( items.html#constant-items ) . ` [a; b] ` creates an array containing ` b `
656
+ copies of the value of ` a ` . If the expression after the semi-colon has a value
646
657
greater than 1 then this requires that the type of ` a ` is
647
658
[ ` Copy ` ] ( the-copy-trait.html ) .
648
659
@@ -715,6 +726,7 @@ Integer operators will panic when they overflow when compiled in debug mode.
715
726
The ` -C debug-assertions ` and ` -C overflow-checks ` compiler flags can be used
716
727
to control this more directly. The following things are considered to be
717
728
overflow:
729
+
718
730
* When ` + ` , ` * ` or ` - ` create a value greater than the maximum value, or less
719
731
than the minimum value that can be stored. This includes unary ` - ` on the
720
732
smallest value of any signed integer type.
@@ -759,7 +771,7 @@ resulting [lvalue](expressions.html#lvalues-rvalues-and-temporaries) can be
759
771
assigned to. Dereferencing a raw pointer requires ` unsafe ` .
760
772
761
773
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
774
+ [ immutable lvalue context] ( #mutability ) and `* std::ops::Deref::deref_mut(&mut
763
775
x)` in a mutable lvalue context.
764
776
765
777
``` rust
@@ -793,11 +805,11 @@ println!("{:?}", res);
793
805
794
806
### Negation operators
795
807
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.
808
+ These are the last two unary operators. This table summarizes the behavior of
809
+ them on primitive types and which traits are used to overload these operators
810
+ for other types. Remember that signed integers are always represented using
811
+ two's complement. The operands of all of these operators are evaluated in
812
+ rvalue context so are moved or copied.
801
813
802
814
| Symbol | Integer | ` bool ` | Floating Point | Overloading Trait |
803
815
| --------| -------------| -------------| ----------------| --------------------|
@@ -822,7 +834,7 @@ summarizes the behavior of arithmetic and logical binary operators on
822
834
primitive types and which traits are used to overload these operators for other
823
835
types. Remember that signed integers are always represented using two's
824
836
complement. The operands of all of these operators are evaluated in rvalue
825
- context and are moved or copied.
837
+ context so are moved or copied.
826
838
827
839
| Symbol | Integer | ` bool ` | Floating Point | Overloading Trait |
828
840
| --------| -------------------------| -------------| ----------------| --------------------|
@@ -934,8 +946,8 @@ fn average(values: &[f64]) -> f64 {
934
946
```
935
947
936
948
` 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` .
949
+ well as the following additional casts. Here ` *T ` means either ` *const T ` or
950
+ ` *mut T ` .
939
951
940
952
| Type of ` e ` | ` U ` | Cast performed by ` e as U ` |
941
953
| -----------------------| -----------------------| ----------------------------------|
@@ -950,7 +962,10 @@ well as the following additional casts. `*T` is short for either
950
962
| [ Function pointer] ( type.html#function-types ) | ` *V ` where ` V: Sized ` | Function pointer to pointer cast |
951
963
| Function pointer | Integer | Function pointer to address cast |
952
964
953
- \* or ` T ` and ` V ` are compatible unsized types, e.g., both slices.
965
+ \* or ` T ` and ` V ` are compatible unsized types, e.g., both slices, both the
966
+ same trait object.
967
+
968
+ #### Semantics
954
969
955
970
* Numeric cast
956
971
* Casting between two integers of the same size (e.g. i32 -> u32) is a no-op
@@ -1041,7 +1056,7 @@ given by their associativity.
1041
1056
| <code >| ;| ; </code > | left to right |
1042
1057
| ` .. ` ` ... ` | Require parentheses |
1043
1058
| ` <- ` | right to left |
1044
- | ` = ` ` += ` ` -= ` ` *= ` ` /= ` ` %= ` ` &= ` <code >| ; =</code > ` ^= ` ` <<= ` ` >>= ` | right to left |
1059
+ | ` = ` ` += ` ` -= ` ` *= ` ` /= ` ` %= ` < br > ` &= ` <code >| ; =</code > ` ^= ` ` <<= ` ` >>= ` | right to left |
1045
1060
1046
1061
## Grouped expressions
1047
1062
0 commit comments