Lesson 1 · Unit 01
Values
Bindings, shadowing and code that is all expressions
What you practise
letandmut, shadowing, scalar types, expressions against statements, functions,ifand loops as expressions
Try a question from this lesson
Pick one
In Python you can reassign any name. Which line does the Rust compiler reject?
let x = 5;
x = 6;
println!("{x}");Correct answer
Line 2
Why
let makes a binding that cannot be changed. Assigning to x again is an error at compile time: "cannot assign twice to immutable variable". Nothing runs until the compiler is satisfied.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Immutable unless you say so
let x = 5; binds a name that cannot be reassigned. Add mut to allow it: let mut x = 5;.
The default is the opposite of Python on purpose. When you read a plain let, you know the value never changes, and so does the compiler.
let total = 10;
let mut count = 0;
count += 1;
println!("{}", total + count);Concept
One fixed type per value
Whole numbers are i32 unless you say otherwise. u8 holds 0 to 255 and usize is for lengths and indexes. Decimals are f64. There are also bool and char.
Rust never converts between them silently. as converts when you ask.
let big: i64 = 3_000_000_000;
let ratio = 7 as f64 / 2.0;
let letter = 'R';
println!("{big} {ratio} {letter}");10 more exercises in the app
- 2× fill the blanks
- 4× type the answer
- 2× multiple choice
- 1× match the pairs
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.