Rust

Data Types

Every value in Rust is of a certain data type, which tells Rust what kind of data is being specified so that it knows how to work with that data. We’ll look at two data type subsets: scalar and compound.

Keep in mind that Rust is a statically typed language, which means that it must know the types of all variables at compile time.

Scalar types

Integer

Floating point

Boolean

Character

Compound types

Tuple

Array

Integer types

Length Signed Unsigned
8 bit i8 u8
16 bit i16 u16
32 bit i32 u32
64 bit i64 u64
128 bit i128 u128
Architecture-dependent isize usize

Filename: src/main.rs

fn main() {
let x = 2;
let y: i8 = 3;
let z: u16 = 100;
println!("The value of x is: {x}");
println!("The value of y is: {y}");
println!("The value of z is: {z}");
}

Floating point types

Filename: src/main.rs

fn main() {
let x = 2.0;
let y: f64 = 2.5;
let z: f32 = 3.0;
println!("The value of x is: {x}");
println!("The value of y is: {y}");
println!("The value of z is: {z}");
}

Boolean types

Filename: src/main.rs

fn main() {
let t = true;
let f: bool = false;
println!("The value of t is: {t}");
println!("The value of f is: {f}");
}

Character types

Filename: src/main.rs

fn main() {
let c = 'c';
let z: char = 'Z';
println!("The value of c is: {c}");
println!("The value of z is: {z}");
}

Tuple types

Filename: src/main.rs

fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of x is: {x}");
println!("The value of y is: {y}");
println!("The value of z is: {z}");
}

Filename: src/main.rs

fn main() {
let x: (i32, f64, u8) = (500, 6.4, 1);
let five_hundred = x.0;
let six_point_four = x.1;
let one = x.2;
println!("The value of 1 is: {five_hundred}");
println!("The value of 2 is: {six_point_four}");
println!("The value of 3 is: {one}");
}

Array types

Filename: src/main.rs

fn main() {
let a = [1, 2, 3, 4, 5];
let months = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
let b: [i32; 5] = [1, 2, 3, 4, 5];
let c: [3; 5];
}

Filename: src/main.rs

fn main() {
let a = [1, 2, 3, 4, 5];
let first = a[0];
let second = a[1];
}