@@ -396,22 +396,67 @@ synonym.
396
396
397
397
## Literals
398
398
399
+ ### Numeric literals
400
+
399
401
Integers can be written in decimal (` 144 ` ), hexadecimal (` 0x90 ` ), and
400
- binary (` 0b10010000 ` ) base. Without a suffix, an integer literal is
401
- considered to be of type ` int ` . Add a ` u ` (` 144u ` ) to make it a ` uint `
402
- instead. Literals of the fixed-size integer types can be created by
403
- the literal with the type name (` 255u8 ` , ` 50i64 ` , etc).
402
+ binary (` 0b10010000 ` ) base.
403
+
404
+ If you write an integer literal without a suffix (` 3 ` , ` -500 ` , etc.),
405
+ the Rust compiler will try to infer its type based on type annotations
406
+ and function signatures in the surrounding program. For example, here
407
+ the type of ` x ` is inferred to be ` u16 ` because it is passed to a
408
+ function that takes a ` u16 ` argument:
409
+
410
+ ~~~~~
411
+ let x = 3;
412
+
413
+ fn identity_u16(n: u16) -> u16 { n }
414
+
415
+ identity_u16(x);
416
+ ~~~~
417
+
418
+ On the other hand, if the program gives conflicting information about
419
+ what the type of the unsuffixed literal should be, you'll get an error
420
+ message.
421
+
422
+ ~~~~~{.xfail-test}
423
+ let x = 3;
424
+ let y: i32 = 3;
425
+
426
+ fn identity_u8(n: u8) -> u8 { n }
427
+ fn identity_u16(n: u16) -> u16 { n }
428
+
429
+ identity_u8(x); // after this, `x` is assumed to have type `u8`
430
+ identity_u16(x); // raises a type error (expected `u16` but found `u8`)
431
+ identity_u16(y); // raises a type error (expected `u16` but found `i32`)
432
+ ~~~~
433
+
434
+ In the absence of any type annotations at all, Rust will assume that
435
+ an unsuffixed integer literal has type `int`.
436
+
437
+ ~~~~
438
+ let n = 50;
439
+ log(error, n); // n is an int
440
+ ~~~~
441
+
442
+ It's also possible to avoid any type ambiguity by writing integer
443
+ literals with a suffix. The suffixes `i` and `u` are for the types
444
+ `int` and `uint`, respectively: the literal `-3i` has type `int`,
445
+ while `127u` has type `uint`. For the fixed-size integer types, just
446
+ suffix the literal with the type name: `255u8`, `50i64`, etc.
404
447
405
448
Note that, in Rust, no implicit conversion between integer types
406
- happens. If you are adding one to a variable of type ` uint ` , you must
407
- type ` v += 1u ` —saying ` += 1 ` will give you a type error.
449
+ happens. If you are adding one to a variable of type `uint`, saying
450
+ ` += 1u8 ` will give you a type error.
408
451
409
452
Floating point numbers are written `0.0`, `1e6`, or `2.1e-4`. Without
410
453
a suffix, the literal is assumed to be of type `float`. Suffixes `f32`
411
454
and `f64` can be used to create literals of a specific type. The
412
455
suffix `f` can be used to write `float` literals without a dot or
413
456
exponent: `3f`.
414
457
458
+ ### Other literals
459
+
415
460
The nil literal is written just like the type: `()`. The keywords
416
461
`true` and `false` produce the boolean literals.
417
462
0 commit comments