Lesson 1 · Unit 01
Values and expressions
Printing, numbers, text and arithmetic
What you practise
fn main,println!and{}, numbers and text, arithmetic and its order, whole-number division and%
Try a question from this lesson
Pick one
This is a whole Rust program. A computer runs it. What do you think appears on the screen?
fn main() {
println!("Hello!");
}Correct answer
Hello!
Why
println! puts text on the screen. The quotes only mark where the text starts and ends, so they are not printed. The rest of the line is the instruction itself, which the computer follows and does not show.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
A list of instructions
A program is a list of instructions for the computer.
Every Rust program starts at fn main() {. The computer runs the lines between the braces one at a time, from top to bottom, and stops at the closing }.
Each instruction ends with a semicolon, ;, the way a sentence ends with a full stop.
fn main() {
println!("first");
println!("second");
}Concept
Text, numbers and gaps
Text goes in double quotes. Numbers are written bare.
Each {} in the text is a gap. The values after the comma fill the gaps from left to right.
To fit your phone, the snippets from here on show only the lines inside fn main() { }. Picture them wrapped in it: they always are.
println!("{} apples", 3);
println!("{} + {} = {}", 2, 3, 2 + 3);Concept
Whole numbers stay whole
Rust keeps two kinds of number apart: whole numbers like 7 and decimals like 7.0.
Divide two whole numbers and you get a whole number. Whatever is left over is thrown away.
% gives you that leftover: the remainder.
println!("{}", 7 / 2);
println!("{}", 7 % 2);
println!("{}", 7.0 / 2.0);9 more exercises in the app
- 1× order the lines
- 2× multiple choice
- 3× type the answer
- 1× true or false
- 1× fill the blanks
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.