Rust has two types of core data types – scalar and compound. Scalar types are single-value types, while compound types are multi-value types.
Integer Data Types
An integer data type is one of Rust’s core data types that represents a numeric value without a fractional component. For instance, 1 is a value of integer type; whereas, 1.99 is not. There are several integer data types in Rust, and each data type represents a different range of values.
Integer Type | Signed / Unsigned | Length (bits) | Range |
---|---|---|---|
i8 | Signed | 8 | -128 to 127 |
u8 | Unsigned | 8 | 0 to 255 |
i16 | Signed | 16 | -32,768 to 32,767 |
u16 | Unsigned | 16 | 0 to 65,535 |
i32 | Signed | 32 | -2,147,483,648 to 2,147,483,647 |
u32 | Unsigned | 32 | 0 to 4294967295 |
i64 | Signed | 64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
u64 | Unsigned | 64 | 0 to 18,446,744,073,709,551,615 |
i128 | Signed | 128 | -1.7014118346046923e+38 to 7014118346046923e+38 |
u128 | Unsigned | 128 | 0 to 3.4028236692+e38 |
isize | Signed | 32 on 32-bit architecture or 64 on 64-bit architecture | -2,147,483,648 to 2,147,483,647 or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
usize | Unsigned | 32 on 32-bit architecture or 64 on 64-bit architecture | 0 to 4294967295 or 0 to 18,446,744,073,709,551,615 |
Rust Floating-Point Data Types
A floating-point type represents a numeric value with a fractional component. For instance, 1.99 is a value of floating-point type, whereas 200 is not.
Floating-point Type | Signed/Unsigned | Length (bits) | Range | |
---|---|---|---|---|
f32 | Signed | 32 | approximately ±3.40282347E+38F (6-7 significant decimal digits) Rust implements IEEE 754 standard | |
f64 | Signed | 64 | approximately ±1.79769313486231570E+308 (15 significant decimal digits) |
When we use a floating-point value in our codes, it defaults to f64. To verify this, run the following codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | trait WhatType { fn type_of(&self) -> String; } impl WhatType for f64 { fn type_of(&self) -> String{ "f64".to_string() } } impl WhatType for f32 { fn type_of(&self) -> String { "f32".to_string() } } fn main() { let my_float1 = 1.99; let my_float2: f32 = 4.88; println!("{} is {}", my_float1, my_float1.type_of()); println!("{} is {}", my_float2, my_float2.type_of()); } |
These codes generate this output:
1 2 | 1.99 is f64 4.88 is f32 |
Boolean Data Type
A boolean type a 16-byte type that represents the true or false value.
Boolean Type | Signed/Unsigned | Length (bits) | Range |
---|---|---|---|
bool | Not Applicable | 8 | true or false only |
1 2 3 4 | fn main() { let is_human: bool = true; let is_robot = false; } |
Rust Character Data Type
The Character data type (or char) typically represents a single letter value enclosed with single quotes. For example:
1 2 3 4 5 6 7 | fn main() { let yes_answer = 'Y'; let no_answer: char = 'N'; println!("Y = {}", yes_answer); println!("N = {}", no_answer); } |
The char data type is also a single 16-bit Unicode character. It has a minimum value of ‘\u{0000}’ (or 0) and a maximum value of ‘\u{ffff}’ (or 65,535 inclusive). For example:
1 2 3 4 5 6 7 | fn main() { let unicode_1: char = '\u{1F60E}'; let unicode_2: char = '\u{ffff}'; println!("{}", unicode_1); println!("{}", unicode_2); } |
It can also refer to a non-Alphabetical character. For example:
1 2 3 4 | fn main() { let unicode_non_english: char = '東'; println!("{}", unicode_non_english); } |
Array Type
Unlike the previous Rust’s core data types, an Array type represents a collection of more than one value (or element) of the same data type. Hence, it is a compound type.
1 2 3 | let letters:[char; 3] = ['a', 'b', 'c']; let integers:[i32; 3] = [1, 2, 3]; let float_64:[f64; 3] = [1.1, 2.2, 3.3]; |
Declaring and initializing arrays is straightforward.
There are two ways to access each value in the array – using [] and the get() function.
1 2 3 | println!("{}", letters.get(0).unwrap()); println!("{}", letters[1]); println!("{}", letters.get(2).unwrap()); |
However, there is only one way to modify each element value – using [].
1 2 3 4 5 6 7 8 | fn main() { let mut integers:[i32; 3] = [1, 2, 3]; integers[1] = 100; println!("{}", integers[0]); println!("{}", integers[1]); println!("{}", integers[2]); } |
So far, the collections of values we’ve briefly touched on are all single-dimensional. Arrays can also be multi-dimensional.
Tuple Type
A Tuple type is akin to the Array type except that it can have values of different types. For example, we can have a tuple with an integer, an character, and an array of f64.
1 | let mut my_stuff: (i32, char, [f64; 3]) = (28, 'A', [1.99, 2.99, 3.99]); |
String Type
A String type is a collection of elements of character type enclosed with double-quotes.
1 2 3 4 | fn main() { let my_string = "this is a string"; println!("{}", my_string); } |
There are some things we need to know about Strings when learning Rust. Understanding these, among other things, will make learning the advanced stuff in Rust easier to grasp.
This post is now part of Rust Programming Language For Beginners Tutorial.