let and Mutability
Bindings and mut.
let and Mutability is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Binding Values with let
In Rust you create a variable with the let keyword. This binds a name to a value.
By default every binding is immutable — once you give it a value, you cannot change it.
fn main() {
let x = 5;
println!("The value of x is: {}", x);
}Immutable by Default
Rust makes variables immutable on purpose. This prevents accidental changes and makes code easier to reason about.
Trying to reassign an immutable binding causes a compile error, not a runtime crash.
let x = 5;thenx = 6;will not compile.
fn main() {
let x = 5;
// x = 6; // this would cause a compile error
println!("x stays {}", x);
}Making Variables Mutable
When you need to change a value, add the mut keyword. This marks the binding as mutable.
Now you can reassign it as many times as you like.
fn main() {
let mut count = 0;
count = count + 1;
count += 1;
println!("count is {}", count);
}Type Inference
Rust usually figures out the type of a variable from its value. This is called type inference.
let x = 5; gives x the type i32 automatically.
fn main() {
let n = 42; // inferred as i32
let pi = 3.14; // inferred as f64
println!("{} and {}", n, pi);
}Type Annotations
You can also state the type explicitly with a colon and a type name. This is a type annotation.
Annotations are useful when the compiler cannot infer the type, or for clarity.
fn main() {
let age: u32 = 30;
let price: f64 = 9.99;
println!("age {} price {}", age, price);
}Constants
A const is always immutable and must have a type annotation. Its value must be known at compile time.
By convention constants use SCREAMING_SNAKE_CASE.
fn main() {
const MAX_POINTS: u32 = 100_000;
println!("Max is {}", MAX_POINTS);
}const vs let
Key differences between const and let:
constcan never bemut.constrequires a type annotation.constmust be a compile-time constant expression.letvalues can be computed at runtime.
fn main() {
const SECONDS_PER_HOUR: u32 = 60 * 60;
let hours = 3;
println!("{} seconds", SECONDS_PER_HOUR * hours);
}Mutating in a Loop
Mutable variables are common in loops where a value accumulates over time.
Here we sum numbers using a mut accumulator.
fn main() {
let mut total = 0;
for i in 1..=5 {
total += i;
}
println!("Sum is {}", total);
}Underscore for Unused
If you bind a value but never use it, Rust warns you. Prefix the name with an underscore to silence the warning.
let _unused = 10;tells the compiler this is intentional.
fn main() {
let _temp = 99;
let used = 1;
println!("{}", used);
}Multiple Bindings
You can declare several variables on separate lines, each with its own let.
Each binding is independent and immutable unless marked mut.
fn main() {
let name = "Rust";
let year = 2010;
let mut version = 1;
version += 1;
println!("{} born {} v{}", name, year, version);
}Why Immutability Helps
Immutability is a safety feature. When the compiler knows a value cannot change, it can make stronger guarantees and prevent whole classes of bugs.
Only opt into mutation with mut when you truly need it.
fn main() {
let config = "production";
// config is guaranteed not to change
println!("Running in {}", config);
}Quick Check
What keyword makes a Rust variable changeable?
Recap
You learned the basics of bindings in Rust:
letbinds a name to a value, immutable by default.mutmakes a binding mutable.- Rust infers types, but you can add annotations with
:. constis compile-time, always immutable, and needs a type.
fn main() {
let mut score: u32 = 0;
const BONUS: u32 = 10;
score += BONUS;
println!("Final score: {}", score);
}Frequently asked questions
Is the “let and Mutability” lesson free?
Yes — the full text of “let and Mutability” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “let and Mutability”?
Bindings and mut. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “let and Mutability” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- let and Mutability
- Scalar Types
- Compound Types
- Shadowing