Skip to content

Commit e490727

Browse files
authored
Merge pull request #141 from brauliobz/grammar_operator_expr
Operator expressions grammar
2 parents 588eb6d + b39801d commit e490727

File tree

1 file changed

+115
-1
lines changed

1 file changed

+115
-1
lines changed

src/expressions/operator-expr.md

+115-1
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.
@@ -38,6 +55,11 @@ assert_eq!(y, 20);
3855

3956
## Borrow operators
4057

58+
> **<sup>Syntax</sup>**
59+
> _BorrowExpression_ :
60+
> &nbsp;&nbsp; &nbsp;&nbsp; (`&`|`&&`) [_Expression_]
61+
> &nbsp;&nbsp; | (`&`|`&&`) `mut` [_Expression_]
62+
4163
The `&` (shared borrow) and `&mut` (mutable borrow) operators are unary prefix
4264
operators. When applied to a [place expression], this expressions produces a
4365
reference (pointer) to the location that the value refers to. The memory
@@ -63,8 +85,26 @@ let mut array = [-2, 3, 9];
6385
}
6486
```
6587

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+
66102
## The dereference operator
67103

104+
> **<sup>Syntax</sup>**
105+
> _DereferenceExpression_ :
106+
> &nbsp;&nbsp; `*` [_Expression_]
107+
68108
The `*` (dereference) operator is also a unary prefix operator. When applied to
69109
a [pointer](types.html#pointer-types) it denotes the pointed-to location. If
70110
the expression is of type `&mut T` and `*mut T`, and is either a local
@@ -86,6 +126,10 @@ assert_eq!(*y, 11);
86126

87127
## The question mark operator
88128

129+
> **<sup>Syntax</sup>**
130+
> _ErrorPropagationExpression_ :
131+
> &nbsp;&nbsp; [_Expression_] `?`
132+
89133
The question mark operator (`?`) unwraps valid values or returns errornous
90134
values, propagating them to the calling function. It is a unary postfix
91135
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);
130174

131175
## Negation operators
132176

177+
> **<sup>Syntax</sup>**
178+
> _NegationExpression_ :
179+
> &nbsp;&nbsp; &nbsp;&nbsp; `-` [_Expression_]
180+
> &nbsp;&nbsp; | `!` [_Expression_]
181+
133182
These are the last two unary operators. This table summarizes the behavior of
134183
them on primitive types and which traits are used to overload these operators
135184
for other types. Remember that signed integers are always represented using
@@ -154,6 +203,19 @@ assert_eq!(true, !false);
154203

155204
## Arithmetic and Logical Binary Operators
156205

206+
> **<sup>Syntax</sup>**
207+
> _ArithmeticOrLogicalExpression_ :
208+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `+` [_Expression_]
209+
> &nbsp;&nbsp; | [_Expression_] `-` [_Expression_]
210+
> &nbsp;&nbsp; | [_Expression_] `*` [_Expression_]
211+
> &nbsp;&nbsp; | [_Expression_] `/` [_Expression_]
212+
> &nbsp;&nbsp; | [_Expression_] `%` [_Expression_]
213+
> &nbsp;&nbsp; | [_Expression_] `&` [_Expression_]
214+
> &nbsp;&nbsp; | [_Expression_] `|` [_Expression_]
215+
> &nbsp;&nbsp; | [_Expression_] `^` [_Expression_]
216+
> &nbsp;&nbsp; | [_Expression_] `<<` [_Expression_]
217+
> &nbsp;&nbsp; | [_Expression_] `>>` [_Expression_]
218+
157219
Binary operators expressions are all written with infix notation. This table
158220
summarizes the behavior of arithmetic and logical binary operators on
159221
primitive types and which traits are used to overload these operators for other
@@ -194,6 +256,15 @@ assert_eq!(-10 >> 2, -3);
194256

195257
## Comparison Operators
196258

259+
> **<sup>Syntax</sup>**
260+
> _ComparisonExpression_ :
261+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `==` [_Expression_]
262+
> &nbsp;&nbsp; | [_Expression_] `!=` [_Expression_]
263+
> &nbsp;&nbsp; | [_Expression_] `>` [_Expression_]
264+
> &nbsp;&nbsp; | [_Expression_] `<` [_Expression_]
265+
> &nbsp;&nbsp; | [_Expression_] `>=` [_Expression_]
266+
> &nbsp;&nbsp; | [_Expression_] `<=` [_Expression_]
267+
197268
Comparison operators are also defined both for primitive types and many type in
198269
the standard library. Parentheses are required when chaining comparison
199270
operators. For example, the expression `a == b == c` is invalid and may be
@@ -238,6 +309,11 @@ assert!("World" >= "Hello");
238309

239310
## Lazy boolean operators
240311

312+
> **<sup>Syntax</sup>**
313+
> _LazyBooleanExpression_ :
314+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `||` [_Expression_]
315+
> &nbsp;&nbsp; | [_Expression_] `&&` [_Expression_]
316+
241317
The operators `||` and `&&` may be applied to operands of boolean type. The
242318
`||` operator denotes logical 'or', and the `&&` operator denotes logical
243319
'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!()`
253329

254330
## Type cast expressions
255331

332+
> **<sup>Syntax</sup>**
333+
> _TypeCastExpression_ :
334+
> &nbsp;&nbsp; [_Expression_] `as` [_PathInExpression_]
335+
256336
A type cast expression is denoted with the binary operator `as`.
257337

258338
Executing an `as` expression casts the value on the left-hand side to the type
@@ -321,8 +401,15 @@ same trait object.
321401
* `u8` to `char` cast
322402
* Casts to the `char` with the corresponding code point.
323403

404+
[float-int]: https://github.com/rust-lang/rust/issues/10184
405+
[float-float]: https://github.com/rust-lang/rust/issues/15536
406+
324407
## Assignment expressions
325408

409+
> **<sup>Syntax</sup>**
410+
> _AssignmentExpression_ :
411+
> &nbsp;&nbsp; | [_Expression_] `=` [_Expression_]
412+
326413
An _assignment expression_ consists of a [place expression] followed by an
327414
equals sign (`=`) and a [value expression].
328415

@@ -341,6 +428,19 @@ x = y;
341428

342429
## Compound assignment expressions
343430

431+
> **<sup>Syntax</sup>**
432+
> _CompoundAssignmentExpression_ :
433+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `+=` [_Expression_]
434+
> &nbsp;&nbsp; | [_Expression_] `-=` [_Expression_]
435+
> &nbsp;&nbsp; | [_Expression_] `*=` [_Expression_]
436+
> &nbsp;&nbsp; | [_Expression_] `/=` [_Expression_]
437+
> &nbsp;&nbsp; | [_Expression_] `%=` [_Expression_]
438+
> &nbsp;&nbsp; | [_Expression_] `&=` [_Expression_]
439+
> &nbsp;&nbsp; | [_Expression_] `|=` [_Expression_]
440+
> &nbsp;&nbsp; | [_Expression_] `^=` [_Expression_]
441+
> &nbsp;&nbsp; | [_Expression_] `<<=` [_Expression_]
442+
> &nbsp;&nbsp; | [_Expression_] `>>=` [_Expression_]
443+
344444
The `+`, `-`, `*`, `/`, `%`, `&`, `|`, `^`, `<<`, and `>>` operators may be
345445
composed with the `=` operator. The expression `place_exp OP= value` is
346446
equivalent to `place_expr = place_expr OP val`. For example, `x = x + 1` may be
@@ -361,4 +461,18 @@ assert_eq!(x, 14);
361461
[temporary value]: expressions.html#temporary-lifetimes
362462
[float-int]: https://github.com/rust-lang/rust/issues/10184
363463
[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

Comments
 (0)