Skip to content

Commit 47996f0

Browse files
Document how the value of an integer literal expression is determined
1 parent af35a77 commit 47996f0

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/expressions/literal-expr.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ let a: u64 = 123; // type u64
5959
0usize; // type usize
6060
```
6161

62+
The value of the expression is determined from the string representation of the token as follows:
63+
64+
* An integer radix is chosen by inspecting the first two characters of the string: `0b` indicates radix 2, `0o` indicates radix 8, `0x` indicates radix 16; otherwise the radix is 10.
65+
66+
* If the radix is not 10, the first two characters are removed from the string.
67+
68+
* Any underscores are removed from the string.
69+
70+
* The string is converted to a `u128` value as if by [`u128::from_str_radix`] with the chosen radix.
71+
72+
* The `u128` value is converted to the expression's type via a [numeric cast].
73+
6274
## Floating-point literal expressions
6375

6476
A floating-point literal expression consists of a single [FLOAT_LITERAL] token.
@@ -87,8 +99,10 @@ let x: f64 = 2.; // type f64
8799
[constant expression]: ../const_eval.md#constant-expressions
88100
[floating-point types]: ../types/numeric.md#floating-point-types
89101
[literal tokens]: ../tokens.md#literals
102+
[numeric cast]: operator-expr.md#numeric-cast
90103
[numeric types]: ../types/numeric.md
91104
[suffix]: ../tokens.md#suffixes
105+
[`u128::from_str_radix`]: ../../core/primitive.u128.md#method.from_str_radix
92106
[CHAR_LITERAL]: ../tokens.md#character-literals
93107
[STRING_LITERAL]: ../tokens.md#string-literals
94108
[RAW_STRING_LITERAL]: ../tokens.md#raw-string-literals

0 commit comments

Comments
 (0)