let — variable binding (can be mutable). const — compile-time constant (always immutable).
let:
let mut for mutable variables.1let x = 5; // Immutable2let mut y = 10; // Mutable3y += 5; // OK45// Destructuring6let (name, age) = ("Alice", 30);7let Point { x, y } = point;8let &value = &reference;
const:
const mut).1const MAX_CONNECTIONS: u32 = 100;2const PI: f64 = 3.14159265358979;3const COLORS: [u8; 3] = [255, 128, 0];45// const fn for compile-time computation6const fn factorial(n: u32) -> u32 {7 match n {8 0 | 1 => 1,9 _ => n * factorial(n - 1),10 }11}12const FACT5: u32 = factorial(5); // 120
When to use:
const for configuration values and compile-time constants.let for general variables and runtime computations.static for global values with fixed memory addresses.Common mistakes:
const — compiler requires it.const at runtime — must be a constant expression.static instead of const without understanding the difference.