Skip to content

Commit 1913a4f

Browse files
committed
Notes about negated literals
1 parent c8b9a20 commit 1913a4f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/expressions/literal-expr.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ If the value does not fit in `u128`, the expression is rejected by the parser.
7777
> **Note**: The final cast will truncate the value of the literal if it does not fit in the expression's type.
7878
> There is a [lint check] named `overflowing_literals`, defaulting to `deny`, which rejects expressions where this occurs.
7979
80+
> **Note**: `-1i8`, for example, is an application of the [negation operator] to the literal expression `1i8`, not a single integer literal expression.
81+
8082
## Floating-point literal expressions
8183

8284
A floating-point literal expression consists of a single [FLOAT_LITERAL] token.
@@ -108,6 +110,8 @@ The value of the expression is determined from the string representation of the
108110

109111
* The string is converted to the expression's type as if by [`f32::from_str`] or [`f64::from_str`].
110112

113+
> **Note**: `-1.0`, for example, is an application of the [negation operator] to the literal expression `1.0`, not a single floating-point literal expression.
114+
111115
> **Note**: `inf` and `NaN` are not literal tokens.
112116
> The [`f32::INFINITY`], [`f64::INFINITY`], [`f32::NAN`], and [`f64::NAN`] constants can be used instead of literal expressions.
113117
> A literal large enough to be evaluated as infinite will trigger the `overflowing_literals` lint check.
@@ -119,6 +123,7 @@ The value of the expression is determined from the string representation of the
119123
[numeric cast]: operator-expr.md#numeric-cast
120124
[numeric types]: ../types/numeric.md
121125
[suffix]: ../tokens.md#suffixes
126+
[negation operator]: operator-expr.md#negation-operators
122127
[`f32::from_str`]: ../../core/primitive.f32.md#method.from_str
123128
[`f32::INFINITY`]: ../../core/primitive.f32.md#associatedconstant.INFINITY
124129
[`f32::NAN`]: ../../core/primitive.f32.md#associatedconstant.NAN

src/tokens.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ Examples of integer literals of various forms:
384384
0usize;
385385
```
386386

387+
Note that `-1i8`, for example, is analyzed as two tokens: `-` followed by `1i8`.
388+
387389
Examples of invalid integer literals:
388390

389391
```rust,compile_fail
@@ -408,10 +410,6 @@ Examples of invalid integer literals:
408410
0b____;
409411
```
410412

411-
Note that the Rust syntax considers `-1i8` as an application of the [unary minus
412-
operator] to an integer literal `1i8`, rather than
413-
a single integer literal.
414-
415413
#### Tuple index
416414

417415
> **<sup>Lexer</sup>**\
@@ -483,6 +481,8 @@ This last example is different because it is not possible to use the suffix
483481
syntax with a floating point literal ending in a period. `2.f64` would attempt
484482
to call a method named `f64` on `2`.
485483

484+
Note that `-1.0`, for example, is analyzed as two tokens: `-` followed by `1.0`.
485+
486486
### Boolean literals
487487

488488
> **<sup>Lexer</sup>**\

0 commit comments

Comments
 (0)