Loading
0x10Lesson 2 of 6

Declare values and mutate safely

Use `let`, `mut`, and basic scalar types.

15 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • Work with immutable and mutable variables in Rust.

Variables are immutable by default in Rust: let x = 3;. Use let mut when a value must change. Common scalar types include i32, f64, bool, and char. Type inference keeps code concise but still strongly typed.

main.rs
1fn main() {
2    let language = "Rust";
3    let mut year = 2015;
4    year += 10;
5    println!("{} is {} years old", language, year);
6}
Output
Rust is 2025 years old

Default immutability prevents accidental state changes and makes code easier to reason about. If you try to reassign an immutable binding, the compiler stops you with a precise message.

Key takeaways

  • Work with immutable and mutable variables in Rust.

  • Rust uses types and ownership rules to prevent common bugs.

  • Small experiments build intuition quickly.

Lesson quiz

5 questions · pass with 4 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Rust

These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.

Exercise 1

Double an integer

+25 XP

Read one integer from stdin and print twice the value.

  • Input 4
  • Input -3
main.rs
Loading editor…

Rust lessons are authored for modern stable Rust; exercises run on Rust 1.82 via Wandbox, a free public service. Your source code and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: