Skip to content

Commit

Permalink
Merge pull request #141 from brauliobz/grammar_operator_expr
Browse files Browse the repository at this point in the history
Operator expressions grammar
  • Loading branch information
Havvy authored Mar 10, 2018
2 parents 588eb6d + b39801d commit e490727
Showing 1 changed file with 115 additions and 1 deletion.
116 changes: 115 additions & 1 deletion src/expressions/operator-expr.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Operator expressions

> **<sup>Syntax</sup>**
> _OperatorExpression_ :
> &nbsp;&nbsp; &nbsp;&nbsp; [_BorrowExpression_]
> &nbsp;&nbsp; | [_DereferenceExpression_]
> &nbsp;&nbsp; | [_ErrorPropagationExpression_]
> &nbsp;&nbsp; | [_NegationExpression_]
> &nbsp;&nbsp; | [_ArithmeticOrLogicalExpression_]
> &nbsp;&nbsp; | [_ComparisonExpression_]
> &nbsp;&nbsp; | [_LazyBooleanExpression_]
> &nbsp;&nbsp; | [_TypeCastExpression_]
> &nbsp;&nbsp; | [_AssignmentExpression_]
> &nbsp;&nbsp; | [_CompoundAssignmentExpression_]
Operators are defined for built in types by the Rust language. Many of the
following operators can also be overloaded using traits in `std::ops` or
`std::cmp`.
Expand All @@ -21,6 +34,10 @@ overflow:

## Grouped expressions

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

## Borrow operators

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

Even though `&&` is a single token ([the lazy 'and' operator](#lazy-boolean-operators)),
when used in the context of borrow expressions it works as two borrows:

```rust
// same meanings:
let a = && 10;
let a = & & 10;

// same meanings:
let a = &&&& mut 10;
let a = && && mut 10;
let a = & & & & mut 10;
```

## The dereference operator

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

## The question mark operator

> **<sup>Syntax</sup>**
> _ErrorPropagationExpression_ :
> &nbsp;&nbsp; [_Expression_] `?`
The question mark operator (`?`) unwraps valid values or returns errornous
values, propagating them to the calling function. It is a unary postfix
operator that can only be applied to the types `Result<T, E>` and `Option<T>`.
Expand Down Expand Up @@ -130,6 +174,11 @@ assert_eq!(try_option_none(), None);

## Negation operators

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

## Arithmetic and Logical Binary Operators

> **<sup>Syntax</sup>**
> _ArithmeticOrLogicalExpression_ :
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `+` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `-` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `*` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `/` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `%` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `&` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `|` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `^` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `<<` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `>>` [_Expression_]
Binary operators expressions are all written with infix notation. This table
summarizes the behavior of arithmetic and logical binary operators on
primitive types and which traits are used to overload these operators for other
Expand Down Expand Up @@ -194,6 +256,15 @@ assert_eq!(-10 >> 2, -3);

## Comparison Operators

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

## Lazy boolean operators

> **<sup>Syntax</sup>**
> _LazyBooleanExpression_ :
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `||` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `&&` [_Expression_]
The operators `||` and `&&` may be applied to operands of boolean type. The
`||` operator denotes logical 'or', and the `&&` operator denotes logical
'and'. They differ from `|` and `&` in that the right-hand operand is only
Expand All @@ -253,6 +329,10 @@ let y = false && panic!(); // false, doesn't evaluate `panic!()`

## Type cast expressions

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

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

[float-int]: https://github.com/rust-lang/rust/issues/10184
[float-float]: https://github.com/rust-lang/rust/issues/15536

## Assignment expressions

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

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

## Compound assignment expressions

> **<sup>Syntax</sup>**
> _CompoundAssignmentExpression_ :
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `+=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `-=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `*=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `/=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `%=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `&=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `|=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `^=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `<<=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `>>=` [_Expression_]
The `+`, `-`, `*`, `/`, `%`, `&`, `|`, `^`, `<<`, and `>>` operators may be
composed with the `=` operator. The expression `place_exp OP= value` is
equivalent to `place_expr = place_expr OP val`. For example, `x = x + 1` may be
Expand All @@ -361,4 +461,18 @@ assert_eq!(x, 14);
[temporary value]: expressions.html#temporary-lifetimes
[float-int]: https://github.com/rust-lang/rust/issues/10184
[float-float]: https://github.com/rust-lang/rust/issues/15536
[`unit` type]: types.html#tuple-types
[`unit` type]: types.html#tuple-types

[_BorrowExpression_]: #borrow-operators
[_DereferenceExpression_]: #the-dereference-operator
[_ErrorPropagationExpression_]: #the--operator
[_NegationExpression_]: #negation-operators
[_ArithmeticOrLogicalExpression_]: #arithmetic-and-logical-binary-operators
[_ComparisonExpression_]: #comparison-operators
[_LazyBooleanExpression_]: #lazy-boolean-operators
[_TypeCastExpression_]: #type-cast-expressions
[_AssignmentExpression_]: #assignment-expressions
[_CompoundAssignmentExpression_]: #compound-assignment-expressions

[_Expression_]: expressions.html
[_PathInExpression_]: paths.html

0 comments on commit e490727

Please sign in to comment.