]> git.proxmox.com Git - rustc.git/blob - src/doc/rust-by-example/src/primitives.md
New upstream version 1.28.0~beta.14+dfsg1
[rustc.git] / src / doc / rust-by-example / src / primitives.md
1 # Primitives
2
3 Rust provides access to a wide variety of `primitives`. A sample includes:
4
5
6 ### Scalar Types
7
8 * signed integers: `i8`, `i16`, `i32`, `i64`, `i128` and `isize` (pointer size)
9 * unsigned integers: `u8`, `u16`, `u32`, `u64`, `u128` and `usize` (pointer
10 size)
11 * floating point: `f32`, `f64`
12 * `char` Unicode scalar values like `'a'`, `'α'` and `'∞'` (4 bytes each)
13 * `bool` either `true` or `false`
14 * and the unit type `()`, whose only possible value is an empty tuple: `()`
15
16 Despite the value of a unit type being a tuple, it is not considered a
17 compound type because it does not contain multiple values.
18
19 ### Compound Types
20
21 * arrays like `[1, 2, 3]`
22 * tuples like `(1, true)`
23
24 Variables can always be *type annotated*. Numbers may additionally be
25 annotated via a *suffix* or *by default*. Integers default to `i32` and
26 floats to `f64`. Note that Rust can also infer types from context.
27
28 ```rust,editable,ignore,mdbook-runnable
29 fn main() {
30 // Variables can be type annotated.
31 let logical: bool = true;
32
33 let a_float: f64 = 1.0; // Regular annotation
34 let an_integer = 5i32; // Suffix annotation
35
36 // Or a default will be used.
37 let default_float = 3.0; // `f64`
38 let default_integer = 7; // `i32`
39
40 // A type can also be inferred from context
41 let mut inferred_type = 12; // Type i64 is inferred from another line
42 inferred_type = 4294967296i64;
43
44 // A mutable variable's value can be changed.
45 let mut mutable = 12; // Mutable `i32`
46 mutable = 21;
47
48 // Error! The type of a variable can't be changed.
49 mutable = true;
50
51 // Variables can be overwritten with shadowing.
52 let mutable = true;
53 }
54 ```
55
56 ### See also:
57
58 [the `std` library][std], [`mut`][mut], [inference], and [shadowing]
59
60 [std]: https://doc.rust-lang.org/std/
61 [mut]: variable_bindings/mut.html
62 [inference]: types/inference.html
63 [shadowing]: variable_bindings/scope.html