A program is a list of instructions for a computer. You write those instructions as source code, text that people can read. Rust is a compiled language: before your program runs, a tool called the compiler reads the whole source, checks that it makes sense and turns it into something the computer can execute. If the compiler finds a problem, nothing runs and you get an error message instead. That sounds strict, but it is the point. Rust catches many mistakes while you are still writing, not later when someone is using your program.
In this course, the editor compiles Rust 2024 with an experimental browser compiler based on rustc, the standard Rust compiler. On your first Run, the editor downloads about 57 MB of compiler files and caches them, so give it time to finish. Later runs are much faster. The output appears below the editor, and each run starts the program from the beginning.
Over the week you will build a small study planner: a program that tracks topics, session lengths and progress. Today it only prints text, but every later piece builds on these first lines.
Predict and run
Rust starts this program in fn main(). The curly braces surround the instructions that belong to main. println! prints a line of text, and the quotation marks show where the text starts and ends. The exclamation mark means println! is a macro, a special kind of instruction; for now, just keep it. A semicolon ends each instruction. A line beginning with // is a comment, a note for people reading the code.
How many lines will appear? Which line comes first?
fn main() {
// Welcome the learner.
println!("Hello, Rust!");
println!("Time to practice.");
}Check your prediction
Hello, Rust!
Time to practice.Two calls to println! produce two lines, in the order they appear in the source. The comment produces nothing at all. Change the greeting to your own text and run again. Then swap the two println! lines and predict the new order before you run.
Read what the compiler tells you
Everyone makes small typing mistakes, and the compiler's messages are written to help you fix them. Suppose you forget the semicolon after the first line:
fn main() {
println!("Hello, Rust!")
println!("Time to practice.");
}The compiler stops with error: expected ;, found println. It points at the end of the first line, where it expected the instruction to finish, and suggests adding the semicolon there. A misspelled name gets a similar hint: writing printn! gives error: cannot find macro printn in this scope, followed by a suggestion of a macro with a similar name.
Read errors from the top. The first one is usually the real problem, and later ones are often side effects of it. Try it yourself: delete the closing quotation mark of the greeting in the playground above, run, find the line the compiler points to, then restore the quote and rerun.
Put values into the text
A pair of curly braces, {}, inside the quotes is a placeholder. println! fills each placeholder with the next value listed after the text, separated by commas. The values can be numbers or calculations.
fn main() {
println!("Study planner");
println!();
println!("Sessions today: {}", 3);
println!("Minutes planned: {}", 3 * 25);
println!("{} topics, {} breaks", 2, 4);
}Study planner
Sessions today: 3
Minutes planned: 75
2 topics, 4 breaksprintln!() with nothing inside prints an empty line. Rust calculated 3 * 25 before printing, so the output shows 75, not the formula. With two placeholders, the values fill them from left to right. If the number of placeholders and values does not match, the program does not compile, so a forgotten value cannot slip through unnoticed.
Change one thing at a time
When you experiment, make one change and run. If something breaks, you know exactly which edit caused it.
Practice
Your turn
Write a three-line welcome for the planner. Print Study planner on the first line and Ready to begin on the second. On the third line, print Sessions today: 2, with the 2 filled in through a {} placeholder.
fn main() {
// Add three instructions here.
}Hint 1: the idea
Each call to println! starts a new output line. The third call needs a placeholder in its text and a value after a comma.
Hint 2: a first step
Inside the braces, start with println!("Study planner");.
Worked solution
fn main() {
println!("Study planner");
println!("Ready to begin");
println!("Sessions today: {}", 2);
}Study planner
Ready to begin
Sessions today: 2Rust runs the three instructions in source order. The 2 sits outside the quotes, so Rust treats it as a value that fills the placeholder. In the next lesson you give values names, and a placeholder can use that name directly.
Completion is saved in this browser only. It does not measure mastery.