-
Notifications
You must be signed in to change notification settings - Fork 493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Operator expressions grammar #141
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
# Operator expressions | ||
|
||
> **<sup>Syntax</sup>** | ||
> _OperatorExpression_ : | ||
> [_BorrowExpression_] | ||
> | [_DereferenceExpression_] | ||
> | [_ErrorPropagationExpression_] | ||
> | [_NegationExpression_] | ||
> | [_ArithmeticOrLogicalExpression_] | ||
> | [_ComparisonExpression_] | ||
> | [_LazyBooleanExpression_] | ||
> | [_TypeCastExpression_] | ||
> | [_AssignmentExpression_] | ||
> | [_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`. | ||
|
@@ -21,6 +34,10 @@ overflow: | |
|
||
## Grouped expressions | ||
|
||
> **<sup>Syntax</sup>** | ||
> _GroupedExpression_ : | ||
> `(` [_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. | ||
|
@@ -38,6 +55,11 @@ assert_eq!(y, 20); | |
|
||
## Borrow operators | ||
|
||
> **<sup>Syntax</sup>** | ||
> _BorrowExpression_ : | ||
> (`&`|`&&`) [_Expression_] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the lexer will give us let & & mut a = & & mut 10;
let && mut a = && mut 10; I'm currently very busy, but I'll put some explanation in the next days, since it isn't really obvious why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
> | (`&`|`&&`) `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 | ||
|
@@ -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_ : | ||
> `*` [_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 | ||
|
@@ -86,6 +126,10 @@ assert_eq!(*y, 11); | |
|
||
## The question mark operator | ||
|
||
> **<sup>Syntax</sup>** | ||
> _ErrorPropagationExpression_ : | ||
> [_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>`. | ||
|
@@ -130,6 +174,11 @@ assert_eq!(try_option_none(), None); | |
|
||
## Negation operators | ||
|
||
> **<sup>Syntax</sup>** | ||
> _NegationExpression_ : | ||
> `-` [_Expression_] | ||
> | `!` [_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 | ||
|
@@ -154,6 +203,19 @@ assert_eq!(true, !false); | |
|
||
## Arithmetic and Logical Binary Operators | ||
|
||
> **<sup>Syntax</sup>** | ||
> _ArithmeticOrLogicalExpression_ : | ||
> [_Expression_] `+` [_Expression_] | ||
> | [_Expression_] `-` [_Expression_] | ||
> | [_Expression_] `*` [_Expression_] | ||
> | [_Expression_] `/` [_Expression_] | ||
> | [_Expression_] `%` [_Expression_] | ||
> | [_Expression_] `&` [_Expression_] | ||
> | [_Expression_] `|` [_Expression_] | ||
> | [_Expression_] `^` [_Expression_] | ||
> | [_Expression_] `<<` [_Expression_] | ||
> | [_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 | ||
|
@@ -194,6 +256,15 @@ assert_eq!(-10 >> 2, -3); | |
|
||
## Comparison Operators | ||
|
||
> **<sup>Syntax</sup>** | ||
> _ComparisonExpression_ : | ||
> [_Expression_] `==` [_Expression_] | ||
> | [_Expression_] `!=` [_Expression_] | ||
> | [_Expression_] `>` [_Expression_] | ||
> | [_Expression_] `<` [_Expression_] | ||
> | [_Expression_] `>=` [_Expression_] | ||
> | [_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 | ||
|
@@ -238,6 +309,11 @@ assert!("World" >= "Hello"); | |
|
||
## Lazy boolean operators | ||
|
||
> **<sup>Syntax</sup>** | ||
> _LazyBooleanExpression_ : | ||
> [_Expression_] `||` [_Expression_] | ||
> | [_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 | ||
|
@@ -253,6 +329,10 @@ let y = false && panic!(); // false, doesn't evaluate `panic!()` | |
|
||
## Type cast expressions | ||
|
||
> **<sup>Syntax</sup>** | ||
> _TypeCastExpression_ : | ||
> [_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 | ||
|
@@ -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_ : | ||
> | [_Expression_] `=` [_Expression_] | ||
An _assignment expression_ consists of a [place expression] followed by an | ||
equals sign (`=`) and a [value expression]. | ||
|
||
|
@@ -341,6 +428,19 @@ x = y; | |
|
||
## Compound assignment expressions | ||
|
||
> **<sup>Syntax</sup>** | ||
> _CompoundAssignmentExpression_ : | ||
> [_Expression_] `+=` [_Expression_] | ||
> | [_Expression_] `-=` [_Expression_] | ||
> | [_Expression_] `*=` [_Expression_] | ||
> | [_Expression_] `/=` [_Expression_] | ||
> | [_Expression_] `%=` [_Expression_] | ||
> | [_Expression_] `&=` [_Expression_] | ||
> | [_Expression_] `|=` [_Expression_] | ||
> | [_Expression_] `^=` [_Expression_] | ||
> | [_Expression_] `<<=` [_Expression_] | ||
> | [_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 | ||
|
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing GroupedExpression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grouped expression will be located directly on the Expression production. I think it would be out of place if put together with the OperatorExpressions -- even though it is in the same section. I put it in the operator expression section mainly because I think it is too small to be on a separate section by itself.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think grammar should be associated with its prose. GroupedExpression is on this page, so it should show up here. If it feels out of place in the grammar, it should feel out of place on the prose and moved to its own page, albeit the shortness is concerning. Although some examples could help with that.