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.
@@ -36,6 +53,11 @@ assert_eq!(y, 20);
36
53
37
54
## Borrow operators
38
55
56
+ > ** <sup >Syntax</sup >**
57
+ > _ BorrowExpression_ :
58
+ >   ;  ;   ;  ; (` & ` |` && ` ) [ _ Expression_ ]
59
+ >   ;  ; | (` & ` |` && ` ) ` mut ` [ _ Expression_ ]
60
+
39
61
The ` & ` (shared borrow) and ` &mut ` (mutable borrow) operators are unary prefix
40
62
operators. When applied to an lvalue produce a reference (pointer) to the
41
63
location that the value refers to. The lvalue is also placed into a borrowed
@@ -62,6 +84,10 @@ let mut array = [-2, 3, 9];
62
84
63
85
## The dereference operator
64
86
87
+ > ** <sup >Syntax</sup >**
88
+ > _ DereferenceExpression_ :
89
+ >   ;  ; ` * ` [ _ Expression_ ]
90
+
65
91
The ` * ` (dereference) operator is also a unary prefix operator. When applied to
66
92
a [ pointer] ( types.html#pointer-types ) it denotes the pointed-to location. If
67
93
the expression is of type ` &mut T ` and ` *mut T ` , and is either a local
@@ -81,7 +107,11 @@ let y = &mut 9;
81
107
assert_eq! (* y , 11 );
82
108
```
83
109
84
- ## The ` ? ` operator.
110
+ ## The ` ? ` operator
111
+
112
+ > ** <sup >Syntax</sup >**
113
+ > _ ErrorPropagationExpression_ :
114
+ >   ;  ; [ _ Expression_ ] ` ? `
85
115
86
116
The ` ? ` ("question mark") operator can be applied to values of the `Result<T,
87
117
E>` type to propagate errors. If applied to ` Err(e)` it will return
@@ -104,6 +134,11 @@ println!("{:?}", res);
104
134
105
135
## Negation operators
106
136
137
+ > ** <sup >Syntax</sup >**
138
+ > _ NegationExpression_ :
139
+ >   ;  ;   ;  ; ` - ` [ _ Expression_ ]
140
+ >   ;  ; | ` ! ` [ _ Expression_ ]
141
+
107
142
These are the last two unary operators. This table summarizes the behavior of
108
143
them on primitive types and which traits are used to overload these operators
109
144
for other types. Remember that signed integers are always represented using
@@ -128,6 +163,19 @@ assert_eq!(true, !false);
128
163
129
164
## Arithmetic and Logical Binary Operators
130
165
166
+ > ** <sup >Syntax</sup >**
167
+ > _ ArithmeticOrLogicalExpression_ :
168
+ >   ;  ;   ;  ; [ _ Expression_ ] ` + ` [ _ Expression_ ]
169
+ >   ;  ; | [ _ Expression_ ] ` - ` [ _ Expression_ ]
170
+ >   ;  ; | [ _ Expression_ ] ` * ` [ _ Expression_ ]
171
+ >   ;  ; | [ _ Expression_ ] ` / ` [ _ Expression_ ]
172
+ >   ;  ; | [ _ Expression_ ] ` % ` [ _ Expression_ ]
173
+ >   ;  ; | [ _ Expression_ ] ` & ` [ _ Expression_ ]
174
+ >   ;  ; | [ _ Expression_ ] ` | ` [ _ Expression_ ]
175
+ >   ;  ; | [ _ Expression_ ] ` ^ ` [ _ Expression_ ]
176
+ >   ;  ; | [ _ Expression_ ] ` << ` [ _ Expression_ ]
177
+ >   ;  ; | [ _ Expression_ ] ` >> ` [ _ Expression_ ]
178
+
131
179
Binary operators expressions are all written with infix notation. This table
132
180
summarizes the behavior of arithmetic and logical binary operators on
133
181
primitive types and which traits are used to overload these operators for other
@@ -168,6 +216,15 @@ assert_eq!(-10 >> 2, -3);
168
216
169
217
## Comparison Operators
170
218
219
+ > ** <sup >Syntax</sup >**
220
+ > _ ComparisonExpression_ :
221
+ >   ;  ;   ;  ; [ _ Expression_ ] ` == ` [ _ Expression_ ]
222
+ >   ;  ; | [ _ Expression_ ] ` != ` [ _ Expression_ ]
223
+ >   ;  ; | [ _ Expression_ ] ` > ` [ _ Expression_ ]
224
+ >   ;  ; | [ _ Expression_ ] ` < ` [ _ Expression_ ]
225
+ >   ;  ; | [ _ Expression_ ] ` >= ` [ _ Expression_ ]
226
+ >   ;  ; | [ _ Expression_ ] ` <= ` [ _ Expression_ ]
227
+
171
228
Comparison operators are also defined both for primitive types and many type in
172
229
the standard library. Parentheses are required when chaining comparison
173
230
operators. For example, the expression ` a == b == c ` is invalid and may be
@@ -212,6 +269,11 @@ assert!("World" >= "Hello");
212
269
213
270
## Lazy boolean operators
214
271
272
+ > ** <sup >Syntax</sup >**
273
+ > _ LazyBooleanExpression_ :
274
+ >   ;  ;   ;  ; [ _ Expression_ ] ` || ` [ _ Expression_ ]
275
+ >   ;  ; | [ _ Expression_ ] ` && ` [ _ Expression_ ]
276
+
215
277
The operators ` || ` and ` && ` may be applied to operands of boolean type. The
216
278
` || ` operator denotes logical 'or', and the ` && ` operator denotes logical
217
279
'and'. They differ from ` | ` and ` & ` in that the right-hand operand is only
@@ -227,6 +289,10 @@ let y = false && panic!(); // false, doesn't evaluate `panic!()`
227
289
228
290
## Type cast expressions
229
291
292
+ > ** <sup >Syntax</sup >**
293
+ > _ TypeCastExpression_ :
294
+ >   ;  ; [ _ Expression_ ] ` as ` [ _ PathInExpression_ ]
295
+
230
296
A type cast expression is denoted with the binary operator ` as ` .
231
297
232
298
Executing an ` as ` expression casts the value on the left-hand side to the type
@@ -300,6 +366,10 @@ same trait object.
300
366
301
367
## Assignment expressions
302
368
369
+ > ** <sup >Syntax</sup >**
370
+ > _ AssignmentExpression_ :
371
+ >   ;  ; | [ _ Expression_ ] ` = ` [ _ Expression_ ]
372
+
303
373
An _ assignment expression_ consists of an
304
374
[ lvalue] ( expressions.html#lvalues-and-rvalues ) expression followed
305
375
by an equals sign (` = ` ) and an
@@ -318,6 +388,19 @@ x = y;
318
388
319
389
## Compound assignment expressions
320
390
391
+ > ** <sup >Syntax</sup >**
392
+ > _ CompoundAssignmentExpression_ :
393
+ >   ;  ;   ;  ; [ _ Expression_ ] ` += ` [ _ Expression_ ]
394
+ >   ;  ; | [ _ Expression_ ] ` -= ` [ _ Expression_ ]
395
+ >   ;  ; | [ _ Expression_ ] ` *= ` [ _ Expression_ ]
396
+ >   ;  ; | [ _ Expression_ ] ` /= ` [ _ Expression_ ]
397
+ >   ;  ; | [ _ Expression_ ] ` %= ` [ _ Expression_ ]
398
+ >   ;  ; | [ _ Expression_ ] ` &= ` [ _ Expression_ ]
399
+ >   ;  ; | [ _ Expression_ ] ` |= ` [ _ Expression_ ]
400
+ >   ;  ; | [ _ Expression_ ] ` ^= ` [ _ Expression_ ]
401
+ >   ;  ; | [ _ Expression_ ] ` <<= ` [ _ Expression_ ]
402
+ >   ;  ; | [ _ Expression_ ] ` >>= ` [ _ Expression_ ]
403
+
321
404
The ` + ` , ` - ` , ` * ` , ` / ` , ` % ` , ` & ` , ` | ` , ` ^ ` , ` << ` , and ` >> ` operators may be
322
405
composed with the ` = ` operator. The expression ` lval OP= val ` is equivalent to
323
406
` lval = lval OP val ` . For example, ` x = x + 1 ` may be written as ` x += 1 ` .
@@ -331,3 +414,17 @@ let mut x = 10;
331
414
x += 4 ;
332
415
assert_eq! (x , 14 );
333
416
```
417
+
418
+ [ _BorrowExpression_ ] : #borrow-operators
419
+ [ _DereferenceExpression_ ] : #the-dereference-operator
420
+ [ _ErrorPropagationExpression_ ] : #the--operator
421
+ [ _NegationExpression_ ] : #negation-operators
422
+ [ _ArithmeticOrLogicalExpression_ ] : #arithmetic-and-logical-binary-operators
423
+ [ _ComparisonExpression_ ] : #comparison-operators
424
+ [ _LazyBooleanExpression_ ] : #lazy-boolean-operators
425
+ [ _TypeCastExpression_ ] : #type-cast-expressions
426
+ [ _AssignmentExpression_ ] : #assignment-expressions
427
+ [ _CompoundAssignmentExpression_ ] : #compound-assignment-expressions
428
+
429
+ [ _Expression_ ] : expressions.html
430
+ [ _PathInExpression_ ] : paths.html
0 commit comments