From 9f3e43580a56e62bfa1da541d014c9d9e96ffcd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A1ulio=20Bezerra?= Date: Sat, 14 Oct 2017 13:39:34 -0300 Subject: [PATCH 1/3] Operator expressions grammar --- src/expressions/operator-expr.md | 102 ++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 4ac71edc2..fc5e15f88 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -1,5 +1,18 @@ # Operator expressions +> **Syntax** +> _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 +> **Syntax** +> _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 +> **Syntax** +> _BorrowExpression_ : +>       (`&`|`&&`) [_Expression_] +>    | (`&`|`&&`) `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 @@ -65,6 +87,10 @@ let mut array = [-2, 3, 9]; ## The dereference operator +> **Syntax** +> _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 +112,10 @@ assert_eq!(*y, 11); ## The question mark operator +> **Syntax** +> _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` and `Option`. @@ -130,6 +160,11 @@ assert_eq!(try_option_none(), None); ## Negation operators +> **Syntax** +> _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 +189,19 @@ assert_eq!(true, !false); ## Arithmetic and Logical Binary Operators +> **Syntax** +> _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 +242,15 @@ assert_eq!(-10 >> 2, -3); ## Comparison Operators +> **Syntax** +> _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 +295,11 @@ assert!("World" >= "Hello"); ## Lazy boolean operators +> **Syntax** +> _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 +315,10 @@ let y = false && panic!(); // false, doesn't evaluate `panic!()` ## Type cast expressions +> **Syntax** +> _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 +387,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 +> **Syntax** +> _AssignmentExpression_ : +>    | [_Expression_] `=` [_Expression_] + An _assignment expression_ consists of a [place expression] followed by an equals sign (`=`) and a [value expression]. @@ -341,6 +414,19 @@ x = y; ## Compound assignment expressions +> **Syntax** +> _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 +447,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 \ No newline at end of file +[`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 From e79d1ef274938dcae1559b8d63bc3e7074ac8ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A1ulio=20Bezerra?= Date: Wed, 29 Nov 2017 23:26:09 -0300 Subject: [PATCH 2/3] Added explanation of the inclusion of "&&" on the borrow operator production --- src/expressions/operator-expr.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index fc5e15f88..671eff6bf 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -85,6 +85,19 @@ let mut array = [-2, 3, 9]; } ``` +For ease of typing, consecutive borrow expressions can be expressed by the +single token `&&` as if they were separated: + +```rust +// same meanings: +let a = && 10; +let a = & & 10; + +// same meanings: +let a = && mut 10; +let a = & & mut 10; +``` + ## The dereference operator > **Syntax** From b39801d7a87e7cc53f7053722cff06d8a1f48fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A1ulio=20Bezerra?= Date: Fri, 9 Mar 2018 23:04:48 -0300 Subject: [PATCH 3/3] Simplified the bit about the && vs & & in borrow expressions --- src/expressions/operator-expr.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 671eff6bf..eb2c45276 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -85,8 +85,8 @@ let mut array = [-2, 3, 9]; } ``` -For ease of typing, consecutive borrow expressions can be expressed by the -single token `&&` as if they were separated: +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: @@ -94,8 +94,9 @@ let a = && 10; let a = & & 10; // same meanings: -let a = && mut 10; -let a = & & mut 10; +let a = &&&& mut 10; +let a = && && mut 10; +let a = & & & & mut 10; ``` ## The dereference operator