@@ -408,34 +408,30 @@ error. Read about [single-variant enums](#single_variant_enum)
408
408
further on if you need to create a type name that's not just a
409
409
synonym.
410
410
411
- ## Numeric literals
411
+ ## Literals
412
412
413
413
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.
415
417
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 ` .
422
423
423
424
~~~~
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
428
429
~~~~
429
430
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
-
434
431
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.
439
435
440
436
The nil literal is written just like the type: ` () ` . The keywords
441
437
` true ` and ` false ` produce the boolean literals.
0 commit comments