Skip to content

Commit 9f3e435

Browse files
committed
Operator expressions grammar
1 parent 588eb6d commit 9f3e435

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

src/expressions/operator-expr.md

Lines changed: 101 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.
@@ -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
@@ -65,6 +87,10 @@ let mut array = [-2, 3, 9];
6587

6688
## The dereference operator
6789

90+
> **<sup>Syntax</sup>**
91+
> _DereferenceExpression_ :
92+
> &nbsp;&nbsp; `*` [_Expression_]
93+
6894
The `*` (dereference) operator is also a unary prefix operator. When applied to
6995
a [pointer](types.html#pointer-types) it denotes the pointed-to location. If
7096
the expression is of type `&mut T` and `*mut T`, and is either a local
@@ -86,6 +112,10 @@ assert_eq!(*y, 11);
86112

87113
## The question mark operator
88114

115+
> **<sup>Syntax</sup>**
116+
> _ErrorPropagationExpression_ :
117+
> &nbsp;&nbsp; [_Expression_] `?`
118+
89119
The question mark operator (`?`) unwraps valid values or returns errornous
90120
values, propagating them to the calling function. It is a unary postfix
91121
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);
130160

131161
## Negation operators
132162

163+
> **<sup>Syntax</sup>**
164+
> _NegationExpression_ :
165+
> &nbsp;&nbsp; &nbsp;&nbsp; `-` [_Expression_]
166+
> &nbsp;&nbsp; | `!` [_Expression_]
167+
133168
These are the last two unary operators. This table summarizes the behavior of
134169
them on primitive types and which traits are used to overload these operators
135170
for other types. Remember that signed integers are always represented using
@@ -154,6 +189,19 @@ assert_eq!(true, !false);
154189

155190
## Arithmetic and Logical Binary Operators
156191

192+
> **<sup>Syntax</sup>**
193+
> _ArithmeticOrLogicalExpression_ :
194+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `+` [_Expression_]
195+
> &nbsp;&nbsp; | [_Expression_] `-` [_Expression_]
196+
> &nbsp;&nbsp; | [_Expression_] `*` [_Expression_]
197+
> &nbsp;&nbsp; | [_Expression_] `/` [_Expression_]
198+
> &nbsp;&nbsp; | [_Expression_] `%` [_Expression_]
199+
> &nbsp;&nbsp; | [_Expression_] `&` [_Expression_]
200+
> &nbsp;&nbsp; | [_Expression_] `|` [_Expression_]
201+
> &nbsp;&nbsp; | [_Expression_] `^` [_Expression_]
202+
> &nbsp;&nbsp; | [_Expression_] `<<` [_Expression_]
203+
> &nbsp;&nbsp; | [_Expression_] `>>` [_Expression_]
204+
157205
Binary operators expressions are all written with infix notation. This table
158206
summarizes the behavior of arithmetic and logical binary operators on
159207
primitive types and which traits are used to overload these operators for other
@@ -194,6 +242,15 @@ assert_eq!(-10 >> 2, -3);
194242

195243
## Comparison Operators
196244

245+
> **<sup>Syntax</sup>**
246+
> _ComparisonExpression_ :
247+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `==` [_Expression_]
248+
> &nbsp;&nbsp; | [_Expression_] `!=` [_Expression_]
249+
> &nbsp;&nbsp; | [_Expression_] `>` [_Expression_]
250+
> &nbsp;&nbsp; | [_Expression_] `<` [_Expression_]
251+
> &nbsp;&nbsp; | [_Expression_] `>=` [_Expression_]
252+
> &nbsp;&nbsp; | [_Expression_] `<=` [_Expression_]
253+
197254
Comparison operators are also defined both for primitive types and many type in
198255
the standard library. Parentheses are required when chaining comparison
199256
operators. For example, the expression `a == b == c` is invalid and may be
@@ -238,6 +295,11 @@ assert!("World" >= "Hello");
238295

239296
## Lazy boolean operators
240297

298+
> **<sup>Syntax</sup>**
299+
> _LazyBooleanExpression_ :
300+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `||` [_Expression_]
301+
> &nbsp;&nbsp; | [_Expression_] `&&` [_Expression_]
302+
241303
The operators `||` and `&&` may be applied to operands of boolean type. The
242304
`||` operator denotes logical 'or', and the `&&` operator denotes logical
243305
'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!()`
253315

254316
## Type cast expressions
255317

318+
> **<sup>Syntax</sup>**
319+
> _TypeCastExpression_ :
320+
> &nbsp;&nbsp; [_Expression_] `as` [_PathInExpression_]
321+
256322
A type cast expression is denoted with the binary operator `as`.
257323

258324
Executing an `as` expression casts the value on the left-hand side to the type
@@ -321,8 +387,15 @@ same trait object.
321387
* `u8` to `char` cast
322388
* Casts to the `char` with the corresponding code point.
323389

390+
[float-int]: https://github.com/rust-lang/rust/issues/10184
391+
[float-float]: https://github.com/rust-lang/rust/issues/15536
392+
324393
## Assignment expressions
325394

395+
> **<sup>Syntax</sup>**
396+
> _AssignmentExpression_ :
397+
> &nbsp;&nbsp; | [_Expression_] `=` [_Expression_]
398+
326399
An _assignment expression_ consists of a [place expression] followed by an
327400
equals sign (`=`) and a [value expression].
328401

@@ -341,6 +414,19 @@ x = y;
341414

342415
## Compound assignment expressions
343416

417+
> **<sup>Syntax</sup>**
418+
> _CompoundAssignmentExpression_ :
419+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `+=` [_Expression_]
420+
> &nbsp;&nbsp; | [_Expression_] `-=` [_Expression_]
421+
> &nbsp;&nbsp; | [_Expression_] `*=` [_Expression_]
422+
> &nbsp;&nbsp; | [_Expression_] `/=` [_Expression_]
423+
> &nbsp;&nbsp; | [_Expression_] `%=` [_Expression_]
424+
> &nbsp;&nbsp; | [_Expression_] `&=` [_Expression_]
425+
> &nbsp;&nbsp; | [_Expression_] `|=` [_Expression_]
426+
> &nbsp;&nbsp; | [_Expression_] `^=` [_Expression_]
427+
> &nbsp;&nbsp; | [_Expression_] `<<=` [_Expression_]
428+
> &nbsp;&nbsp; | [_Expression_] `>>=` [_Expression_]
429+
344430
The `+`, `-`, `*`, `/`, `%`, `&`, `|`, `^`, `<<`, and `>>` operators may be
345431
composed with the `=` operator. The expression `place_exp OP= value` is
346432
equivalent to `place_expr = place_expr OP val`. For example, `x = x + 1` may be
@@ -361,4 +447,18 @@ assert_eq!(x, 14);
361447
[temporary value]: expressions.html#temporary-lifetimes
362448
[float-int]: https://github.com/rust-lang/rust/issues/10184
363449
[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

Comments
 (0)