Your first program
- Rust course
- Beginner
- Lesson 3/115
- 4 min
Write your first Rust program with fn main and println!.
The first program
Every executable Rust program starts at the main function. The exclamation mark at the end of println! shows it is a macro; it prints and adds a newline.
fn main() {
println!("Merhaba, dünya!");
}Statements end with a semicolon; the body is inside curly braces. The file can also be compiled on its own with rustc main.rs.
print! and eprintln!
print! doesn't add a newline; eprintln! writes to the standard error stream (stderr) and is used for error messages:
print!("Yükleniyor");
print!("...");
println!(); // just a newline
eprintln!("uyarı: dosya bulunamadı");When output is redirected to a file (> cikti.txt), stderr messages stay on the screen.
Practice Loading bar
Call print!("Yükleniyor"), then print!(".") three times, and finally println!(" tamam").
Starter code
fn main() {
// buraya yaz
}Sample solution
fn main() {
print!("Yükleniyor");
print!(".");
print!(".");
print!(".");
println!(" tamam");
}Expected output
Yükleniyor... tamam
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why is <code>println!</code> written with an exclamation mark?
- Because it's important
- Because it can fail
- It's optional
- It is a macro; it expands into code at compile time Correct answer
Explanation: Macros can take a variable number of arguments and check the format string at compile time; so if the {} count is wrong, the code doesn't compile.
2 Print without a newline
____!("Yükleniyor...");
print!("Yükleniyor...");
Explanation: print! writes text without adding a newline.
3 Macro and target
| Term | Meaning |
|---|---|
println! |
stdout + newline |
print! |
stdout, no newline |
eprintln! |
stderr + newline |
fn main |
Entry point |