mut — mutable variable (value can be reassigned). const — compile-time constant (value is fixed).
mut:
1let mut count = 0;2count += 1; // OK3println!("{}", count); // 145let mut items = Vec::new();6items.push("item1");7items.push("item2");
const:
const mut).1const MAX_SIZE: usize = 1024;2const PI: f64 = 3.14159265358979;3const EMPTY_STR: &str = "";45const fn compute(x: i32) -> i32 { x * 2 }6const RESULT: i32 = compute(5); // 10
When to use:
const for configuration, buffer sizes, protocol constants.mut when values need to change during computation.const over magic numbers for readability.Common mistakes:
const — required.const — compile error.let mut when const is more appropriate.