Declare values and mutate safely
Use `let`, `mut`, and basic scalar types.
- 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.
1fn main() {
2 let language = "Rust";
3 let mut year = 2015;
4 year += 10;
5 println!("{} is {} years old", language, year);
6}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.
Double an integer
Read one integer from stdin and print twice the value.
- Input 4
- Input -3
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…