diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 4ac71edc2..eb2c45276 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 @@ -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 +> **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 +126,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 +174,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 +203,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 +256,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 +309,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 +329,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 +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 +> **Syntax** +> _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 +> **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 +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 \ 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