Lesson 4: Data Types

Key Concepts

Rust is statically typed with type inference. Types: scalar (integers, floats, bools, chars) and compound (tuples, arrays).

Scalar Types

1. Integers

Signed (i8 to i128), unsigned (u8 to u128), isize/usize. Default: i32.

Literals: 98_222 (decimal), 0xff (hex), etc.

Overflow: Panics in debug, wraps in release.

2. Floating-Point

let x = 2.0; // f64
let y: f32 = 3.0;

3. Boolean

let t = true;
let f: bool = false;

4. Character

let c = 'z';
let heart_eyed_cat = '😻';

Compound Types

1. Tuple

let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup;
let five_hundred = tup.0;

2. Array

let a = [1, 2, 3, 4, 5];
let a: [i32; 5] = [1, 2, 3, 4, 5];
let a = [3; 5]; // [3, 3, 3, 3, 3]
let first = a[0];

Out-of-bounds access panics.

Use Vec<T> for growable collections.

You Try

Edit the tuple or array below (e.g., change values or access different elements) and click Run to see the outputs.

fn main() {
    let tup: (i32, f64, char) = (500, 6.4, 'z');
    let (x, y, z) = tup;
    println!("x: {x}, y: {y}, z: {z}");

    let a = [1, 2, 3, 4, 5];
    println!("First element: {}", a[0]);
}