mut — mutable variable (value can change). const — compile-time constant (value fixed).
mut:
mut.&mut T) allow modifying borrowed data.1let mut count = 0;2count += 1; // OK — value changes3println!("{}", count); // 145let mut s = String::from("hello");6s.push_str(" world"); // Modifying the String
const:
const mut in standard Rust.1const MAX_SIZE: usize = 1024;2const PI: f64 = 3.14159265358979;3const APP_NAME: &str = "MyApp";45// const fn — can be evaluated at compile time6const fn add(a: i32, b: i32) -> i32 {7 a + b8}9const RESULT: i32 = add(1, 2); // 3
When to use:
const for magic numbers, configuration values, buffer sizes.mut when you need to reassign or build up a value.static for global values that need a fixed address.Common mistakes:
const — required.const — compile error.let mut for values that never change — use const instead.