1
1
# Operator expressions
2
2
3
+ > ** <sup >Syntax</sup >**
4
+ > _ OperatorExpression_ :
5
+ >   ;  ;   ;  ; [ _ BorrowExpression_ ]
6
+ >   ;  ; | [ _ DereferenceExpression_ ]
7
+ >   ;  ; | [ _ ErrorPropagationExpression_ ]
8
+ >   ;  ; | [ _ NegationExpression_ ]
9
+ >   ;  ; | [ _ ArithmeticOrLogicalExpression_ ]
10
+ >   ;  ; | [ _ ComparisonExpression_ ]
11
+ >   ;  ; | [ _ LazyBooleanExpression_ ]
12
+ >   ;  ; | [ _ TypeCastExpression_ ]
13
+ >   ;  ; | [ _ AssignmentExpression_ ]
14
+ >   ;  ; | [ _ CompoundAssignmentExpression_ ]
15
+
3
16
Operators are defined for built in types by the Rust language. Many of the
4
17
following operators can also be overloaded using traits in ` std::ops ` or
5
18
` std::cmp ` .
@@ -21,6 +34,10 @@ overflow:
21
34
22
35
## Grouped expressions
23
36
37
+ > ** <sup >Syntax</sup >**
38
+ > _ GroupedExpression_ :
39
+ >   ;  ; ` ( ` [ _ Expression_ ] ` ) `
40
+
24
41
An expression enclosed in parentheses evaluates to the result of the enclosed
25
42
expression. Parentheses can be used to explicitly specify evaluation order
26
43
within an expression.
@@ -38,6 +55,11 @@ assert_eq!(y, 20);
38
55
39
56
## Borrow operators
40
57
58
+ > ** <sup >Syntax</sup >**
59
+ > _ BorrowExpression_ :
60
+ >   ;  ;   ;  ; (` & ` |` && ` ) [ _ Expression_ ]
61
+ >   ;  ; | (` & ` |` && ` ) ` mut ` [ _ Expression_ ]
62
+
41
63
The ` & ` (shared borrow) and ` &mut ` (mutable borrow) operators are unary prefix
42
64
operators. When applied to a [ place expression] , this expressions produces a
43
65
reference (pointer) to the location that the value refers to. The memory
@@ -63,8 +85,26 @@ let mut array = [-2, 3, 9];
63
85
}
64
86
```
65
87
88
+ Even though ` && ` is a single token ([ the lazy 'and' operator] ( #lazy-boolean-operators ) ),
89
+ when used in the context of borrow expressions it works as two borrows:
90
+
91
+ ``` rust
92
+ // same meanings:
93
+ let a = && 10 ;
94
+ let a = & & 10 ;
95
+
96
+ // same meanings:
97
+ let a = &&&& mut 10 ;
98
+ let a = && && mut 10 ;
99
+ let a = & & & & mut 10 ;
100
+ ```
101
+
66
102
## The dereference operator
67
103
104
+ > ** <sup >Syntax</sup >**
105
+ > _ DereferenceExpression_ :
106
+ >   ;  ; ` * ` [ _ Expression_ ]
107
+
68
108
The ` * ` (dereference) operator is also a unary prefix operator. When applied to
69
109
a [ pointer] ( types.html#pointer-types ) it denotes the pointed-to location. If
70
110
the expression is of type ` &mut T ` and ` *mut T ` , and is either a local
@@ -86,6 +126,10 @@ assert_eq!(*y, 11);
86
126
87
127
## The question mark operator
88
128
129
+ > ** <sup >Syntax</sup >**
130
+ > _ ErrorPropagationExpression_ :
131
+ >   ;  ; [ _ Expression_ ] ` ? `
132
+
89
133
The question mark operator (` ? ` ) unwraps valid values or returns errornous
90
134
values, propagating them to the calling function. It is a unary postfix
91
135
operator that can only be applied to the types ` Result<T, E> ` and ` Option<T> ` .
@@ -130,6 +174,11 @@ assert_eq!(try_option_none(), None);
130
174
131
175
## Negation operators
132
176
177
+ > ** <sup >Syntax</sup >**
178
+ > _ NegationExpression_ :
179
+ >   ;  ;   ;  ; ` - ` [ _ Expression_ ]
180
+ >   ;  ; | ` ! ` [ _ Expression_ ]
181
+
133
182
These are the last two unary operators. This table summarizes the behavior of
134
183
them on primitive types and which traits are used to overload these operators
135
184
for other types. Remember that signed integers are always represented using
@@ -154,6 +203,19 @@ assert_eq!(true, !false);
154
203
155
204
## Arithmetic and Logical Binary Operators
156
205
206
+ > ** <sup >Syntax</sup >**
207
+ > _ ArithmeticOrLogicalExpression_ :
208
+ >   ;  ;   ;  ; [ _ Expression_ ] ` + ` [ _ Expression_ ]
209
+ >   ;  ; | [ _ Expression_ ] ` - ` [ _ Expression_ ]
210
+ >   ;  ; | [ _ Expression_ ] ` * ` [ _ Expression_ ]
211
+ >   ;  ; | [ _ Expression_ ] ` / ` [ _ Expression_ ]
212
+ >   ;  ; | [ _ Expression_ ] ` % ` [ _ Expression_ ]
213
+ >   ;  ; | [ _ Expression_ ] ` & ` [ _ Expression_ ]
214
+ >   ;  ; | [ _ Expression_ ] ` | ` [ _ Expression_ ]
215
+ >   ;  ; | [ _ Expression_ ] ` ^ ` [ _ Expression_ ]
216
+ >   ;  ; | [ _ Expression_ ] ` << ` [ _ Expression_ ]
217
+ >   ;  ; | [ _ Expression_ ] ` >> ` [ _ Expression_ ]
218
+
157
219
Binary operators expressions are all written with infix notation. This table
158
220
summarizes the behavior of arithmetic and logical binary operators on
159
221
primitive types and which traits are used to overload these operators for other
@@ -194,6 +256,15 @@ assert_eq!(-10 >> 2, -3);
194
256
195
257
## Comparison Operators
196
258
259
+ > ** <sup >Syntax</sup >**
260
+ > _ ComparisonExpression_ :
261
+ >   ;  ;   ;  ; [ _ Expression_ ] ` == ` [ _ Expression_ ]
262
+ >   ;  ; | [ _ Expression_ ] ` != ` [ _ Expression_ ]
263
+ >   ;  ; | [ _ Expression_ ] ` > ` [ _ Expression_ ]
264
+ >   ;  ; | [ _ Expression_ ] ` < ` [ _ Expression_ ]
265
+ >   ;  ; | [ _ Expression_ ] ` >= ` [ _ Expression_ ]
266
+ >   ;  ; | [ _ Expression_ ] ` <= ` [ _ Expression_ ]
267
+
197
268
Comparison operators are also defined both for primitive types and many type in
198
269
the standard library. Parentheses are required when chaining comparison
199
270
operators. For example, the expression ` a == b == c ` is invalid and may be
@@ -238,6 +309,11 @@ assert!("World" >= "Hello");
238
309
239
310
## Lazy boolean operators
240
311
312
+ > ** <sup >Syntax</sup >**
313
+ > _ LazyBooleanExpression_ :
314
+ >   ;  ;   ;  ; [ _ Expression_ ] ` || ` [ _ Expression_ ]
315
+ >   ;  ; | [ _ Expression_ ] ` && ` [ _ Expression_ ]
316
+
241
317
The operators ` || ` and ` && ` may be applied to operands of boolean type. The
242
318
` || ` operator denotes logical 'or', and the ` && ` operator denotes logical
243
319
'and'. They differ from ` | ` and ` & ` in that the right-hand operand is only
@@ -253,6 +329,10 @@ let y = false && panic!(); // false, doesn't evaluate `panic!()`
253
329
254
330
## Type cast expressions
255
331
332
+ > ** <sup >Syntax</sup >**
333
+ > _ TypeCastExpression_ :
334
+ >   ;  ; [ _ Expression_ ] ` as ` [ _ PathInExpression_ ]
335
+
256
336
A type cast expression is denoted with the binary operator ` as ` .
257
337
258
338
Executing an ` as ` expression casts the value on the left-hand side to the type
@@ -321,8 +401,15 @@ same trait object.
321
401
* ` u8 ` to ` char ` cast
322
402
* Casts to the ` char ` with the corresponding code point.
323
403
404
+ [ float-int ] : https://github.com/rust-lang/rust/issues/10184
405
+ [ float-float ] : https://github.com/rust-lang/rust/issues/15536
406
+
324
407
## Assignment expressions
325
408
409
+ > ** <sup >Syntax</sup >**
410
+ > _ AssignmentExpression_ :
411
+ >   ;  ; | [ _ Expression_ ] ` = ` [ _ Expression_ ]
412
+
326
413
An _ assignment expression_ consists of a [ place expression] followed by an
327
414
equals sign (` = ` ) and a [ value expression] .
328
415
@@ -341,6 +428,19 @@ x = y;
341
428
342
429
## Compound assignment expressions
343
430
431
+ > ** <sup >Syntax</sup >**
432
+ > _ CompoundAssignmentExpression_ :
433
+ >   ;  ;   ;  ; [ _ Expression_ ] ` += ` [ _ Expression_ ]
434
+ >   ;  ; | [ _ Expression_ ] ` -= ` [ _ Expression_ ]
435
+ >   ;  ; | [ _ Expression_ ] ` *= ` [ _ Expression_ ]
436
+ >   ;  ; | [ _ Expression_ ] ` /= ` [ _ Expression_ ]
437
+ >   ;  ; | [ _ Expression_ ] ` %= ` [ _ Expression_ ]
438
+ >   ;  ; | [ _ Expression_ ] ` &= ` [ _ Expression_ ]
439
+ >   ;  ; | [ _ Expression_ ] ` |= ` [ _ Expression_ ]
440
+ >   ;  ; | [ _ Expression_ ] ` ^= ` [ _ Expression_ ]
441
+ >   ;  ; | [ _ Expression_ ] ` <<= ` [ _ Expression_ ]
442
+ >   ;  ; | [ _ Expression_ ] ` >>= ` [ _ Expression_ ]
443
+
344
444
The ` + ` , ` - ` , ` * ` , ` / ` , ` % ` , ` & ` , ` | ` , ` ^ ` , ` << ` , and ` >> ` operators may be
345
445
composed with the ` = ` operator. The expression ` place_exp OP= value ` is
346
446
equivalent to ` place_expr = place_expr OP val ` . For example, ` x = x + 1 ` may be
@@ -361,4 +461,18 @@ assert_eq!(x, 14);
361
461
[ temporary value ] : expressions.html#temporary-lifetimes
362
462
[ float-int ] : https://github.com/rust-lang/rust/issues/10184
363
463
[ float-float ] : https://github.com/rust-lang/rust/issues/15536
364
- [ `unit` type ] : types.html#tuple-types
464
+ [ `unit` type ] : types.html#tuple-types
465
+
466
+ [ _BorrowExpression_ ] : #borrow-operators
467
+ [ _DereferenceExpression_ ] : #the-dereference-operator
468
+ [ _ErrorPropagationExpression_ ] : #the--operator
469
+ [ _NegationExpression_ ] : #negation-operators
470
+ [ _ArithmeticOrLogicalExpression_ ] : #arithmetic-and-logical-binary-operators
471
+ [ _ComparisonExpression_ ] : #comparison-operators
472
+ [ _LazyBooleanExpression_ ] : #lazy-boolean-operators
473
+ [ _TypeCastExpression_ ] : #type-cast-expressions
474
+ [ _AssignmentExpression_ ] : #assignment-expressions
475
+ [ _CompoundAssignmentExpression_ ] : #compound-assignment-expressions
476
+
477
+ [ _Expression_ ] : expressions.html
478
+ [ _PathInExpression_ ] : paths.html
0 commit comments