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
@@ -65,6 +87,10 @@ let mut array = [-2, 3, 9];
65
87
66
88
## The dereference operator
67
89
90
+ > ** <sup >Syntax</sup >**
91
+ > _ DereferenceExpression_ :
92
+ >   ;  ; ` * ` [ _ Expression_ ]
93
+
68
94
The ` * ` (dereference) operator is also a unary prefix operator. When applied to
69
95
a [ pointer] ( types.html#pointer-types ) it denotes the pointed-to location. If
70
96
the expression is of type ` &mut T ` and ` *mut T ` , and is either a local
@@ -86,6 +112,10 @@ assert_eq!(*y, 11);
86
112
87
113
## The question mark operator
88
114
115
+ > ** <sup >Syntax</sup >**
116
+ > _ ErrorPropagationExpression_ :
117
+ >   ;  ; [ _ Expression_ ] ` ? `
118
+
89
119
The question mark operator (` ? ` ) unwraps valid values or returns errornous
90
120
values, propagating them to the calling function. It is a unary postfix
91
121
operator that can only be applied to the types ` Result<T, E> ` and ` Option<T> ` .
@@ -130,6 +160,11 @@ assert_eq!(try_option_none(), None);
130
160
131
161
## Negation operators
132
162
163
+ > ** <sup >Syntax</sup >**
164
+ > _ NegationExpression_ :
165
+ >   ;  ;   ;  ; ` - ` [ _ Expression_ ]
166
+ >   ;  ; | ` ! ` [ _ Expression_ ]
167
+
133
168
These are the last two unary operators. This table summarizes the behavior of
134
169
them on primitive types and which traits are used to overload these operators
135
170
for other types. Remember that signed integers are always represented using
@@ -154,6 +189,19 @@ assert_eq!(true, !false);
154
189
155
190
## Arithmetic and Logical Binary Operators
156
191
192
+ > ** <sup >Syntax</sup >**
193
+ > _ ArithmeticOrLogicalExpression_ :
194
+ >   ;  ;   ;  ; [ _ Expression_ ] ` + ` [ _ Expression_ ]
195
+ >   ;  ; | [ _ Expression_ ] ` - ` [ _ Expression_ ]
196
+ >   ;  ; | [ _ Expression_ ] ` * ` [ _ Expression_ ]
197
+ >   ;  ; | [ _ Expression_ ] ` / ` [ _ Expression_ ]
198
+ >   ;  ; | [ _ Expression_ ] ` % ` [ _ Expression_ ]
199
+ >   ;  ; | [ _ Expression_ ] ` & ` [ _ Expression_ ]
200
+ >   ;  ; | [ _ Expression_ ] ` | ` [ _ Expression_ ]
201
+ >   ;  ; | [ _ Expression_ ] ` ^ ` [ _ Expression_ ]
202
+ >   ;  ; | [ _ Expression_ ] ` << ` [ _ Expression_ ]
203
+ >   ;  ; | [ _ Expression_ ] ` >> ` [ _ Expression_ ]
204
+
157
205
Binary operators expressions are all written with infix notation. This table
158
206
summarizes the behavior of arithmetic and logical binary operators on
159
207
primitive types and which traits are used to overload these operators for other
@@ -194,6 +242,15 @@ assert_eq!(-10 >> 2, -3);
194
242
195
243
## Comparison Operators
196
244
245
+ > ** <sup >Syntax</sup >**
246
+ > _ ComparisonExpression_ :
247
+ >   ;  ;   ;  ; [ _ Expression_ ] ` == ` [ _ Expression_ ]
248
+ >   ;  ; | [ _ Expression_ ] ` != ` [ _ Expression_ ]
249
+ >   ;  ; | [ _ Expression_ ] ` > ` [ _ Expression_ ]
250
+ >   ;  ; | [ _ Expression_ ] ` < ` [ _ Expression_ ]
251
+ >   ;  ; | [ _ Expression_ ] ` >= ` [ _ Expression_ ]
252
+ >   ;  ; | [ _ Expression_ ] ` <= ` [ _ Expression_ ]
253
+
197
254
Comparison operators are also defined both for primitive types and many type in
198
255
the standard library. Parentheses are required when chaining comparison
199
256
operators. For example, the expression ` a == b == c ` is invalid and may be
@@ -238,6 +295,11 @@ assert!("World" >= "Hello");
238
295
239
296
## Lazy boolean operators
240
297
298
+ > ** <sup >Syntax</sup >**
299
+ > _ LazyBooleanExpression_ :
300
+ >   ;  ;   ;  ; [ _ Expression_ ] ` || ` [ _ Expression_ ]
301
+ >   ;  ; | [ _ Expression_ ] ` && ` [ _ Expression_ ]
302
+
241
303
The operators ` || ` and ` && ` may be applied to operands of boolean type. The
242
304
` || ` operator denotes logical 'or', and the ` && ` operator denotes logical
243
305
'and'. They differ from ` | ` and ` & ` in that the right-hand operand is only
@@ -253,6 +315,10 @@ let y = false && panic!(); // false, doesn't evaluate `panic!()`
253
315
254
316
## Type cast expressions
255
317
318
+ > ** <sup >Syntax</sup >**
319
+ > _ TypeCastExpression_ :
320
+ >   ;  ; [ _ Expression_ ] ` as ` [ _ PathInExpression_ ]
321
+
256
322
A type cast expression is denoted with the binary operator ` as ` .
257
323
258
324
Executing an ` as ` expression casts the value on the left-hand side to the type
@@ -321,8 +387,15 @@ same trait object.
321
387
* ` u8 ` to ` char ` cast
322
388
* Casts to the ` char ` with the corresponding code point.
323
389
390
+ [ float-int ] : https://github.com/rust-lang/rust/issues/10184
391
+ [ float-float ] : https://github.com/rust-lang/rust/issues/15536
392
+
324
393
## Assignment expressions
325
394
395
+ > ** <sup >Syntax</sup >**
396
+ > _ AssignmentExpression_ :
397
+ >   ;  ; | [ _ Expression_ ] ` = ` [ _ Expression_ ]
398
+
326
399
An _ assignment expression_ consists of a [ place expression] followed by an
327
400
equals sign (` = ` ) and a [ value expression] .
328
401
@@ -341,6 +414,19 @@ x = y;
341
414
342
415
## Compound assignment expressions
343
416
417
+ > ** <sup >Syntax</sup >**
418
+ > _ CompoundAssignmentExpression_ :
419
+ >   ;  ;   ;  ; [ _ Expression_ ] ` += ` [ _ Expression_ ]
420
+ >   ;  ; | [ _ Expression_ ] ` -= ` [ _ Expression_ ]
421
+ >   ;  ; | [ _ Expression_ ] ` *= ` [ _ Expression_ ]
422
+ >   ;  ; | [ _ Expression_ ] ` /= ` [ _ Expression_ ]
423
+ >   ;  ; | [ _ Expression_ ] ` %= ` [ _ Expression_ ]
424
+ >   ;  ; | [ _ Expression_ ] ` &= ` [ _ Expression_ ]
425
+ >   ;  ; | [ _ Expression_ ] ` |= ` [ _ Expression_ ]
426
+ >   ;  ; | [ _ Expression_ ] ` ^= ` [ _ Expression_ ]
427
+ >   ;  ; | [ _ Expression_ ] ` <<= ` [ _ Expression_ ]
428
+ >   ;  ; | [ _ Expression_ ] ` >>= ` [ _ Expression_ ]
429
+
344
430
The ` + ` , ` - ` , ` * ` , ` / ` , ` % ` , ` & ` , ` | ` , ` ^ ` , ` << ` , and ` >> ` operators may be
345
431
composed with the ` = ` operator. The expression ` place_exp OP= value ` is
346
432
equivalent to ` place_expr = place_expr OP val ` . For example, ` x = x + 1 ` may be
@@ -361,4 +447,18 @@ assert_eq!(x, 14);
361
447
[ temporary value ] : expressions.html#temporary-lifetimes
362
448
[ float-int ] : https://github.com/rust-lang/rust/issues/10184
363
449
[ float-float ] : https://github.com/rust-lang/rust/issues/15536
364
- [ `unit` type ] : types.html#tuple-types
450
+ [ `unit` type ] : types.html#tuple-types
451
+
452
+ [ _BorrowExpression_ ] : #borrow-operators
453
+ [ _DereferenceExpression_ ] : #the-dereference-operator
454
+ [ _ErrorPropagationExpression_ ] : #the--operator
455
+ [ _NegationExpression_ ] : #negation-operators
456
+ [ _ArithmeticOrLogicalExpression_ ] : #arithmetic-and-logical-binary-operators
457
+ [ _ComparisonExpression_ ] : #comparison-operators
458
+ [ _LazyBooleanExpression_ ] : #lazy-boolean-operators
459
+ [ _TypeCastExpression_ ] : #type-cast-expressions
460
+ [ _AssignmentExpression_ ] : #assignment-expressions
461
+ [ _CompoundAssignmentExpression_ ] : #compound-assignment-expressions
462
+
463
+ [ _Expression_ ] : expressions.html
464
+ [ _PathInExpression_ ] : paths.html
0 commit comments