Skip to content

Commit e5568bb

Browse files
committed
Operator expressions grammar
1 parent 7bfc70e commit e5568bb

File tree

1 file changed

+98
-1
lines changed

1 file changed

+98
-1
lines changed

src/expressions/operator-expr.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Operator expressions
22

3+
> **<sup>Syntax</sup>**
4+
> _OperatorExpression_ :
5+
> &nbsp;&nbsp; &nbsp;&nbsp; [_BorrowExpression_]
6+
> &nbsp;&nbsp; | [_DereferenceExpression_]
7+
> &nbsp;&nbsp; | [_ErrorPropagationExpression_]
8+
> &nbsp;&nbsp; | [_NegationExpression_]
9+
> &nbsp;&nbsp; | [_ArithmeticOrLogicalExpression_]
10+
> &nbsp;&nbsp; | [_ComparisonExpression_]
11+
> &nbsp;&nbsp; | [_LazyBooleanExpression_]
12+
> &nbsp;&nbsp; | [_TypeCastExpression_]
13+
> &nbsp;&nbsp; | [_AssignmentExpression_]
14+
> &nbsp;&nbsp; | [_CompoundAssignmentExpression_]
15+
316
Operators are defined for built in types by the Rust language. Many of the
417
following operators can also be overloaded using traits in `std::ops` or
518
`std::cmp`.
@@ -21,6 +34,10 @@ overflow:
2134

2235
## Grouped expressions
2336

37+
> **<sup>Syntax</sup>**
38+
> _GroupedExpression_ :
39+
> &nbsp;&nbsp; `(` [_Expression_] `)`
40+
2441
An expression enclosed in parentheses evaluates to the result of the enclosed
2542
expression. Parentheses can be used to explicitly specify evaluation order
2643
within an expression.
@@ -36,6 +53,11 @@ assert_eq!(y, 20);
3653

3754
## Borrow operators
3855

56+
> **<sup>Syntax</sup>**
57+
> _BorrowExpression_ :
58+
> &nbsp;&nbsp; &nbsp;&nbsp; (`&`|`&&`) [_Expression_]
59+
> &nbsp;&nbsp; | (`&`|`&&`) `mut` [_Expression_]
60+
3961
The `&` (shared borrow) and `&mut` (mutable borrow) operators are unary prefix
4062
operators. When applied to an lvalue produce a reference (pointer) to the
4163
location that the value refers to. The lvalue is also placed into a borrowed
@@ -62,6 +84,10 @@ let mut array = [-2, 3, 9];
6284

6385
## The dereference operator
6486

87+
> **<sup>Syntax</sup>**
88+
> _DereferenceExpression_ :
89+
> &nbsp;&nbsp; `*` [_Expression_]
90+
6591
The `*` (dereference) operator is also a unary prefix operator. When applied to
6692
a [pointer](types.html#pointer-types) it denotes the pointed-to location. If
6793
the expression is of type `&mut T` and `*mut T`, and is either a local
@@ -81,7 +107,11 @@ let y = &mut 9;
81107
assert_eq!(*y, 11);
82108
```
83109

84-
## The `?` operator.
110+
## The `?` operator
111+
112+
> **<sup>Syntax</sup>**
113+
> _ErrorPropagationExpression_ :
114+
> &nbsp;&nbsp; [_Expression_] `?`
85115
86116
The `?` ("question mark") operator can be applied to values of the `Result<T,
87117
E>` type to propagate errors. If applied to `Err(e)` it will return
@@ -104,6 +134,11 @@ println!("{:?}", res);
104134

105135
## Negation operators
106136

137+
> **<sup>Syntax</sup>**
138+
> _NegationExpression_ :
139+
> &nbsp;&nbsp; &nbsp;&nbsp; `-` [_Expression_]
140+
> &nbsp;&nbsp; | `!` [_Expression_]
141+
107142
These are the last two unary operators. This table summarizes the behavior of
108143
them on primitive types and which traits are used to overload these operators
109144
for other types. Remember that signed integers are always represented using
@@ -128,6 +163,19 @@ assert_eq!(true, !false);
128163

129164
## Arithmetic and Logical Binary Operators
130165

166+
> **<sup>Syntax</sup>**
167+
> _ArithmeticOrLogicalExpression_ :
168+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `+` [_Expression_]
169+
> &nbsp;&nbsp; | [_Expression_] `-` [_Expression_]
170+
> &nbsp;&nbsp; | [_Expression_] `*` [_Expression_]
171+
> &nbsp;&nbsp; | [_Expression_] `/` [_Expression_]
172+
> &nbsp;&nbsp; | [_Expression_] `%` [_Expression_]
173+
> &nbsp;&nbsp; | [_Expression_] `&` [_Expression_]
174+
> &nbsp;&nbsp; | [_Expression_] `|` [_Expression_]
175+
> &nbsp;&nbsp; | [_Expression_] `^` [_Expression_]
176+
> &nbsp;&nbsp; | [_Expression_] `<<` [_Expression_]
177+
> &nbsp;&nbsp; | [_Expression_] `>>` [_Expression_]
178+
131179
Binary operators expressions are all written with infix notation. This table
132180
summarizes the behavior of arithmetic and logical binary operators on
133181
primitive types and which traits are used to overload these operators for other
@@ -168,6 +216,15 @@ assert_eq!(-10 >> 2, -3);
168216

169217
## Comparison Operators
170218

219+
> **<sup>Syntax</sup>**
220+
> _ComparisonExpression_ :
221+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `==` [_Expression_]
222+
> &nbsp;&nbsp; | [_Expression_] `!=` [_Expression_]
223+
> &nbsp;&nbsp; | [_Expression_] `>` [_Expression_]
224+
> &nbsp;&nbsp; | [_Expression_] `<` [_Expression_]
225+
> &nbsp;&nbsp; | [_Expression_] `>=` [_Expression_]
226+
> &nbsp;&nbsp; | [_Expression_] `<=` [_Expression_]
227+
171228
Comparison operators are also defined both for primitive types and many type in
172229
the standard library. Parentheses are required when chaining comparison
173230
operators. For example, the expression `a == b == c` is invalid and may be
@@ -212,6 +269,11 @@ assert!("World" >= "Hello");
212269

213270
## Lazy boolean operators
214271

272+
> **<sup>Syntax</sup>**
273+
> _LazyBooleanExpression_ :
274+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `||` [_Expression_]
275+
> &nbsp;&nbsp; | [_Expression_] `&&` [_Expression_]
276+
215277
The operators `||` and `&&` may be applied to operands of boolean type. The
216278
`||` operator denotes logical 'or', and the `&&` operator denotes logical
217279
'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!()`
227289

228290
## Type cast expressions
229291

292+
> **<sup>Syntax</sup>**
293+
> _TypeCastExpression_ :
294+
> &nbsp;&nbsp; [_Expression_] `as` [_PathInExpression_]
295+
230296
A type cast expression is denoted with the binary operator `as`.
231297

232298
Executing an `as` expression casts the value on the left-hand side to the type
@@ -300,6 +366,10 @@ same trait object.
300366

301367
## Assignment expressions
302368

369+
> **<sup>Syntax</sup>**
370+
> _AssignmentExpression_ :
371+
> &nbsp;&nbsp; | [_Expression_] `=` [_Expression_]
372+
303373
An _assignment expression_ consists of an
304374
[lvalue](expressions.html#lvalues-and-rvalues) expression followed
305375
by an equals sign (`=`) and an
@@ -318,6 +388,19 @@ x = y;
318388

319389
## Compound assignment expressions
320390

391+
> **<sup>Syntax</sup>**
392+
> _CompoundAssignmentExpression_ :
393+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `+=` [_Expression_]
394+
> &nbsp;&nbsp; | [_Expression_] `-=` [_Expression_]
395+
> &nbsp;&nbsp; | [_Expression_] `*=` [_Expression_]
396+
> &nbsp;&nbsp; | [_Expression_] `/=` [_Expression_]
397+
> &nbsp;&nbsp; | [_Expression_] `%=` [_Expression_]
398+
> &nbsp;&nbsp; | [_Expression_] `&=` [_Expression_]
399+
> &nbsp;&nbsp; | [_Expression_] `|=` [_Expression_]
400+
> &nbsp;&nbsp; | [_Expression_] `^=` [_Expression_]
401+
> &nbsp;&nbsp; | [_Expression_] `<<=` [_Expression_]
402+
> &nbsp;&nbsp; | [_Expression_] `>>=` [_Expression_]
403+
321404
The `+`, `-`, `*`, `/`, `%`, `&`, `|`, `^`, `<<`, and `>>` operators may be
322405
composed with the `=` operator. The expression `lval OP= val` is equivalent to
323406
`lval = lval OP val`. For example, `x = x + 1` may be written as `x += 1`.
@@ -331,3 +414,17 @@ let mut x = 10;
331414
x += 4;
332415
assert_eq!(x, 14);
333416
```
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

Comments
 (0)