← SEARCHABLE ARCHIVE
Rust · 2026-07-19

Rust from Basics to Mastery — Part 2

Learn variables, types, functions, and pattern matching

INTERACTIVE READING MODE →

The articles written on this blog were created with the goal of teaching. Also, the information in the article may be incorrect, so please be careful.

Rust checks the valid range of variable types and values ​​at the compile stage. If you understand basic grammar, expressions, and pattern matching, subsequent proprietary code will become easier to read.

Variables and Types

let level: u32 = 10;
let speed = 4.5_f32;
let alive = true;

let mut health = 100;
health -= 20;

Variables cannot be changed by default, and the value can be changed only by adding mut. When immutability is the default, it becomes easier to find which code is changing state.

Integer types have a fixed size and sign. When converting to another numeric type, you must indicate your intention using as or a conversion function.

expression

let state = if health <= 0 {
    "Dead"
} else if health < 30 {
    "Danger"
} else {
    "Normal"
};

Rust’s if can be used as an expression that returns a value. The types returned by each branch must be the same.

If there is no semicolon, the last expression in the block becomes the return value. If you add a semicolon, it becomes a unit type with no value.

match and repeat

let reward = match rank {
    'S' => 1000,
    'A' => 500,
    _ => 100,
};

let mut total = 0;
for damage in [10, 20, 15] {
    total += damage;
}

match checks to ensure that no possible cases are omitted. _ is the remaining pattern that was not processed previously.

function

fn calculate_damage(attack: i32, defense: i32) -> i32 {
    (attack - defense).max(1)
}

Parameters and return types are specified, and the last expression is the result. Functions are not simply a tool to reduce code, but a tool to create boundaries between input, output, and responsibility.