Skip to content

Commit 3b89dcb

Browse files
committed
tutorial: Clean up literal section
1 parent 4081b40 commit 3b89dcb

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

doc/tutorial.md

+16-20
Original file line numberDiff line numberDiff line change
@@ -408,34 +408,30 @@ error. Read about [single-variant enums](#single_variant_enum)
408408
further on if you need to create a type name that's not just a
409409
synonym.
410410

411-
## Numeric literals
411+
## Literals
412412

413413
Integers can be written in decimal (`144`), hexadecimal (`0x90`), and
414-
binary (`0b10010000`) base.
414+
binary (`0b10010000`) base. Each integral type has a corresponding literal
415+
suffix that can be used to indicate the type of a literal: `i` for `int`,
416+
`u` for `uint`, and `i8` for the `i8` type, etc.
415417

416-
If you write an integer literal without a suffix (`3`, `-500`, etc.),
417-
the Rust compiler will try to infer its type based on type annotations
418-
and function signatures in the surrounding program. In the absence of any type
419-
annotations at all, Rust will assume that an unsuffixed integer literal has
420-
type `int`. It's also possible to avoid any type ambiguity by writing integer
421-
literals with a suffix. For example:
418+
In the absense of an integer literal suffix, Rust will infer the
419+
integer type based on type annotations and function signatures in the
420+
surrounding program. In the absence of any type information at all,
421+
Rust will assume that an unsuffixed integer literal has type
422+
`int`.
422423

423424
~~~~
424-
let x = 50;
425-
log(error, x); // x is an int
426-
let y = 100u;
427-
log(error, y); // y is an uint
425+
let a = 1; // a is an int
426+
let b = 10i; // b is an int, due to the 'i' suffix
427+
let c = 100u; // c as a uint
428+
let d = 1000i32; // d is an i32
428429
~~~~
429430

430-
Note that, in Rust, no implicit conversion between integer types
431-
happens. If you are adding one to a variable of type `uint`, saying
432-
`+= 1u8` will give you a type error.
433-
434431
Floating point numbers are written `0.0`, `1e6`, or `2.1e-4`. Without
435-
a suffix, the literal is assumed to be of type `float`. Suffixes `f` (32-bit)
436-
and `l` (64-bit) can be used to create literals of a specific type.
437-
438-
## Other literals
432+
a suffix, the literal is assumed to be of type `float`. Suffixes `f32`
433+
(32-bit) and `f64` (64-bit) can be used to create literals of a
434+
specific type.
439435

440436
The nil literal is written just like the type: `()`. The keywords
441437
`true` and `false` produce the boolean literals.

0 commit comments

Comments
 (0)