mut — mutable variable (value can be reassigned). const — compile-time constant (value is fixed).
mut:
1let mut total = 0;2for i in 1..=10 {3 total += i;4}5println!("{}", total); // 5567let mut s = String::new();8s.push_str("hello");
const:
1const MAX_SIZE: usize = 4096;2const PI: f64 = 3.14159265358979;3const EMPTY: &str = "";45const fn add(a: i32, b: i32) -> i32 { a + b }6const RESULT: i32 = add(10, 20); // 30
When to use:
const for configuration, constants.mut when values need to change.const over magic numbers.Common mistakes:
const.const.let mut when const is better.