mut — mutable variable (value can be reassigned). const — compile-time constant (value is fixed).
mut:
&mut T) for borrowing.1let mut total = 0;2for i in 1..=10 {3 total += i;4}5println!("{}", total); // 5567let mut name = String::from("hello");8name.push_str(" world");
const:
const mut in stable Rust).1const MAX_BUFFER: usize = 4096;2const API_URL: &str = "https://api.example.com";3const VERSION: (u8, u8, u8) = (1, 0, 0);45const fn add(a: i32, b: i32) -> i32 { a + b }6const RESULT: i32 = add(10, 20); // 30
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 by compiler.const — compile error.let mut when const is more appropriate.