Boss lesson · Unit 01
A receipt
Prices, a discount and tax, turned into a printed total
What you practise
- Prices, quantities, a discount and tax turned into a formatted total
- Reading a compiler error from its first line to its hint
Try a question from this lesson
Pick one
A café sells three coffees at 2.50 each. What does this print?
let price = 2.5;
let count = 3.0;
println!("{}", price * count);Correct answer
7.5
Why
Three times 2.5 is 7.5. Both numbers are decimals, so Rust multiplies them without complaint. But a receipt should say 7.50, not 7.5. That needs one more trick.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Money has two decimals
A plain gap prints 7.5. A receipt wants 7.50.
Inside the gap, :.2 means "show two digits after the point". It works on its own, {:.2}, or after a name, {total:.2}.
let total = 7.5;
println!("{}", total);
println!("{:.2}", total);
println!("Total: {total:.2}");Concept
Reading a compiler error
Read an error in three steps.
The first line says what is wrong. The --> line says where: file, line, position. The help at the bottom often contains the fix itself.
This program tried total = total + 0.6; on a plain let total. The message is shortened and wrapped to fit.
error[E0384]: cannot assign twice to
immutable variable `total`
--> src/main.rs:3:5
|
3 | total = total + 0.6;
| ^^^^^^^^^^^^^^^^^^^
help: consider making this binding mutable
|
2 | let mut total = 7.5;
| +++10 more exercises in the app
- 3× type the answer
- 4× multiple choice
- 1× fill the blanks
- 1× order the lines
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.