Data Types
Scalar Data Types
Rust has several categories of primitive types: integers, floating-point numbers, Booleans, unicode characters, 'Unit', and 'Never'.
Type Family | Types | Examples |
---|---|---|
Signed Integers | i8 ⮳, i16 ⮳, i32 ⮳, i64 ⮳, i128 ⮳, isize ⮳. | -8i8 , -32i32 . |
Unsigned Integers | u8 ⮳, u16 ⮳, u32 ⮳, u64 ⮳, u128 ⮳, usize ⮳. | 6u8 . |
Floating point | f32 ⮳, f64 ⮳. | 0.15 . |
Boolean | bool ⮳. | true , false . |
Unicode Character | char ⮳ | let z: char = 'ℤ'; |
Unit | unit ⮳. | The () type (aka 'void' in other languages) has exactly one value () , and is used when there is no other meaningful value that could be returned. |
Never | never ⮳. | ! represents the type of computations which never resolve to any value at all. For example, the exit function fn exit(code: i32) -> ! exits the process without ever returning, and so returns ! . |
usize
⮳ and isize
⮳ are 32 or 64 bits, depending on the architecture of the computer.
The following illustrates the various scalar data types:
Compound Data Types: Tuples and Arrays
Compound types can group multiple values into one type. Rust has two primitive compound types: tuples and arrays.
Type | Examples |
---|---|
Tuples | let tup: (i32, f64, u8) = (500, 6.4, 1); . Access via let five_hundred = x.0; . Destructuring via let (x, y, z) = tup; . |
Arrays | let a: [i32; 5] = [1, 2, 3, 4, 5]; allocated on the stack. Access via let first = a[0]; . |
Both are fixed length. A Vector is a similar collection type provided by the standard library that is allowed to grow or shrink in size.
The following provides examples of tuples and arrays:
Type Aliases
Use the type
keyword to declare type aliases: type Kilometers = i32;
.
Related Topics
See also:
- Enums.
- Option.
- Result.
- Slices.
- Strings.
- Structs.
- Vectors.
- Data Structures.