Step 1 of 5 · Reading · ~3 min
Learn
Getting Started
Hello, World
By the end of this lesson you can write a complete Rust program, print fixed text and text that contains a value, and read the compiler's error messages instead of guessing.
The smallest program
fn main() {
println!("Rust compiles to a native binary");
}
Output:
Rust compiles to a native binary
fn main()is the entry point.fndeclares a function; the program starts at the first line insidemain's{ }body and ends when that body ends.println!prints its text followed by a newline. The!means it is a macro, not a function: code the compiler expands before building your program. Leave the!off and the program does not compile.;ends each statement.
You build and run it with rustc main.rs && ./main, or with cargo run in a real project. Cargo is Rust's build tool; rustc is the compiler it drives.
println! versus print!
print! is the same macro without the newline. The next output continues on the same line:
fn main() {
print!("tick ");
print!("tock");
println!(); // just a newline
println!("done");
}
Output:
tick tock
done
When an exercise asks for two lines, two println! calls give you two lines. Two print! calls give you one line with the texts glued together.
Putting a value inside the text
Inside the text, {} is a placeholder: a slot the macro fills with the next argument after the comma.
fn main() {
let city = "Oslo";
let stops = 3;
println!("Next stop: {}.", city);
println!("{} stops to {}", stops, city);
}
Output:
Next stop: Oslo.
3 stops to Oslo
let city = "Oslo"; gives the name city to a piece of text; the next lesson covers let in full. Placeholders are filled left to right. Everything outside the braces, including spaces and punctuation, is printed exactly as written. The value's own characters are printed as-is too, so if city held the text {}, the output would show {} literally.
Writing the variable's name inside the quotes does not insert its value:
println!("Next stop: city."); // prints: Next stop: city.
println!("Next stop: {}.", city); // prints: Next stop: Oslo.
The compiler checks your placeholders
println! counts its placeholders while compiling. One slot with no argument is an error, not a blank:
error: 1 positional argument in format string, but no arguments were given
--> main.rs:3:26
|
3 | println!("Next stop: {}.");
| ^^
The first argument must be the quoted text itself. println!(city); fails with format argument must be a string literal, and gluing text with + inside println! does not compile either. Read every error top to bottom: what went wrong, the file, line and column, the code with the problem underlined, and often a help: line with the fix.
Common mistakes
- Typing the name instead of using a placeholder. A hard-coded
Hello, Ada!passes the one example and fails every hidden test, which use different names. - Using
print!for the first line. Both lines come out glued together as one line, so the output no longer matches. - Wrong spacing or punctuation.
Hello,Ada!orHello, Ada !is a different string. The comparison is character for character. - Dropping or rewording the second line, including its full stop.
Your exercise
Print a two-line greeting for a name read from input. The starter already reads the name into name; you write only the printing. The most likely mistake is typing a name into the text instead of using {}.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…