Simple Data Types
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.
// COMING SOON
Composite Types
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]; . |
A Vector is a similar collection type provided by the standard library that is allowed to grow or shrink in size.
See also:
- Enums.
- Option.
- Result.
- Slices.
- Strings.
- Structs.
- Vectors.
Type Aliases
Use the type
keyword to declare type aliases: type Kilometers = i32;
.
Handle Overflows
- Wrap in all modes with the
wrapping_*
methods, such aswrapping_add
⮳. - Return the
std::option::Option::None
⮳ value if there is overflow with thechecked_*
methods. - Return the value and a boolean indicating whether there was overflow with the
overflowing_*
methods. - Saturate at the value's minimum or maximum values with the
saturating_*
methods.
Related Topics
- Data Structures