Skip to content

Commit 15204c0

Browse files
Describe the effect of integer literal suffixes in literal-expr.md rather than tokens.md
1 parent a3b14b0 commit 15204c0

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

src/expressions/literal-expr.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,45 @@ Each of the lexical [literal][literal tokens] forms described earlier can make u
2424
5; // integer type
2525
```
2626

27+
## Integer literal expressions
28+
29+
An integer literal expression consists of a single [INTEGER_LITERAL] token.
30+
31+
If the token has a [suffix], the suffix will be the name of one of the [primitive integer types][numeric types]: `u8`, `i8`, `u16`, `i16`, `u32`, `i32`, `u64`, `i64`, `u128`, `i128`, `usize`, or `isize`, and the expression has that type.
32+
33+
If the token has no suffix, the expression's type is determined by type inference:
34+
35+
* If an integer type can be _uniquely_ determined from the surrounding program context, the expression has that type.
36+
37+
* If the program context under-constrains the type, it defaults to the signed 32-bit integer `i32`.
38+
39+
* If the program context over-constrains the type, it is considered a static type error.
40+
41+
Examples of integer literal expressions:
42+
43+
```rust
44+
123; // type i32
45+
123i32; // type i32
46+
123u32; // type u32
47+
123_u32; // type u32
48+
let a: u64 = 123; // type u64
49+
50+
0xff; // type i32
51+
0xff_u8; // type u8
52+
53+
0o70; // type i32
54+
0o70_i16; // type i16
55+
56+
0b1111_1111_1001_0000; // type i32
57+
0b1111_1111_1001_0000i64; // type i64
58+
59+
0usize; // type usize
60+
```
61+
2762
[constant expression]: ../const_eval.md#constant-expressions
2863
[literal tokens]: ../tokens.md#literals
64+
[numeric types]: ../types/numeric.md
65+
[suffix]: ../tokens.md#suffixes
2966
[CHAR_LITERAL]: ../tokens.md#character-literals
3067
[STRING_LITERAL]: ../tokens.md#string-literals
3168
[RAW_STRING_LITERAL]: ../tokens.md#raw-string-literals

src/tokens.md

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -359,43 +359,29 @@ An _integer literal_ has one of four forms:
359359
(`0b`) and continues as any mixture (with at least one digit) of binary digits
360360
and underscores.
361361

362-
Like any literal, an integer literal may be followed (immediately,
363-
without any spaces) by an _integer suffix_, which forcibly sets the
364-
type of the literal. The integer suffix must be the name of one of the
365-
integral types: `u8`, `i8`, `u16`, `i16`, `u32`, `i32`, `u64`, `i64`,
366-
`u128`, `i128`, `usize`, or `isize`.
367-
368-
The type of an _unsuffixed_ integer literal is determined by type inference:
369-
370-
* If an integer type can be _uniquely_ determined from the surrounding
371-
program context, the unsuffixed integer literal has that type.
372-
373-
* If the program context under-constrains the type, it defaults to the
374-
signed 32-bit integer `i32`.
375-
376-
* If the program context over-constrains the type, it is considered a
377-
static type error.
362+
Like any literal, an integer literal may be followed (immediately, without any spaces) by an _integer suffix_, which must be the name of one of the [primitive integer types][numeric types]:
363+
`u8`, `i8`, `u16`, `i16`, `u32`, `i32`, `u64`, `i64`, `u128`, `i128`, `usize`, or `isize`.
364+
See [literal expressions] for the effect of these suffixes.
378365

379366
Examples of integer literals of various forms:
380367

381368
```rust
382-
123; // type i32
383-
123i32; // type i32
384-
123u32; // type u32
385-
123_u32; // type u32
386-
let a: u64 = 123; // type u64
369+
123;
370+
123i32;
371+
123u32;
372+
123_u32;
387373

388-
0xff; // type i32
389-
0xff_u8; // type u8
374+
0xff;
375+
0xff_u8;
390376

391-
0o70; // type i32
392-
0o70_i16; // type i16
377+
0o70;
378+
0o70_i16;
393379

394-
0b1111_1111_1001_0000; // type i32
395-
0b1111_1111_1001_0000i64; // type i64
396-
0b________1; // type i32
380+
0b1111_1111_1001_0000;
381+
0b1111_1111_1001_0000i64;
382+
0b________1;
397383

398-
0usize; // type usize
384+
0usize;
399385
```
400386

401387
Examples of invalid integer literals:
@@ -671,6 +657,7 @@ Similarly the `r`, `b`, and `br` prefixes used in raw string literals, byte lite
671657
[negation]: expressions/operator-expr.md#negation-operators
672658
[negative impls]: items/implementations.md
673659
[never type]: types/never.md
660+
[numeric types]: types/numeric.md
674661
[paths]: paths.md
675662
[patterns]: patterns.md
676663
[question]: expressions/operator-expr.md#the-question-mark-operator

0 commit comments

Comments
 (0)