Chapter 5: Rust Output
Rust Output
This one is straightforward but super important for beginners. When people (especially in tutorials like W3Schools Rust series, Programiz, or YouTube beginner videos) say “Rust Output”, they almost always mean:
How to print / display / show things on the screen (console/terminal) in Rust → Basically: “How do I make my program talk back to me?” 😄
In Rust, “output” = producing visible text/numbers/data in the terminal (stdout), or sometimes error messages (stderr). The main tools for this are a few simple macros (those things with ! at the end — remember from syntax lesson?).
Let me explain it slowly like your teacher sitting next to you in a Banjara Hills café, with lots of copy-paste examples you can run right now.
1. The Two Main Ways to Output in Rust (Most Common)
Rust gives you these macros from the standard library:
- println! → prints text + adds a new line at the end (most used!)
- print! → prints text without new line (useful when you want things on same line)
Both are macros (not functions), so they can do fancy formatting.
Basic example — create a new project if you want fresh:
|
0 1 2 3 4 5 6 7 |
cargo new rust_output cd rust_output |
Replace src/main.rs with this:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
fn main() { // Basic println! println!("Namaste Hyderabad!"); // new line automatic println!("Today is February 26, 2026 ☀️"); // Multiple lines println!("I'm learning Rust output right now."); println!("Very exciting! 🚀"); // print! without newline print!("Hello "); print!("from "); println!("the same line!"); // this one adds newline at end // Empty line (just newline) println!(); // prints nothing + newline = blank line } |
Run:
|
0 1 2 3 4 5 6 |
cargo run |
You’ll see:
|
0 1 2 3 4 5 6 7 8 9 10 |
Namaste Hyderabad! Today is February 26, 2026 ☀️ I'm learning Rust output right now. Very exciting! 🚀 Hello from the same line! |
See? println! is your best friend for normal output.
2. Printing Variables & Values (Formatting — very powerful!)
You put {} placeholders inside the string — Rust fills them.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
fn main() { let city = "Hyderabad"; let temp = 32; let is_hot = true; // Simple insert println!("City: {}", city); // Multiple placeholders (order matters!) println!("Temperature in {} is {}°C. Hot? {}", city, temp, is_hot); // Named arguments (very readable) println!("{city} weather: {temp}°C → {is_hot}", city=city, temp=temp, is_hot=is_hot); // Debug print (great for seeing structs/vectors) let numbers = vec![1, 42, 100]; println!("Debug: {:?}", numbers); // {:?} = debug format println!("Pretty debug: {:#?}", numbers); // {:#?} = pretty indented } |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
City: Hyderabad Temperature in Hyderabad is 32°C. Hot? true Hyderabad weather: 32°C → true Debug: [1, 42, 100] Pretty debug: [ 1, 42, 100, ] |
Tip: Use {:?} or {:#?} when debugging — shows internal structure nicely.
3. Fancy Formatting Options (Numbers, Alignment, etc.)
Rust formatting is inspired by Python’s f-strings / format().
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
fn main() { let pi = 3.1415926535; let score = 99.5; let population = 10000000; // Precision for floats println!("Pi ≈ {:.2}", pi); // 3.14 println!("Pi more precise: {:.4}", pi); // 3.1416 // Width & alignment println!("Score |{:>8.1}|", score); // right align, width 8 → " 99.5" println!("Score |{:<8.1}|", score); // left align → "99.5 " // Thousands separator (nice for big numbers) println!("Population: {:_}", population); // 10_000_000 println!("Population: {:,}", population); // 10,000,000 (comma) // Binary, hex, octal let num = 255; println!("Binary: {:b}", num); // 11111111 println!("Hex: {:X}", num); // FF } |
4. Printing to Error Stream (stderr) — Important!
Use eprintln! and eprint! when something is wrong (errors, warnings).
|
0 1 2 3 4 5 6 7 8 9 |
fn main() { println!("Normal output goes here (stdout)"); eprintln!("This is an ERROR message! (stderr)"); } |
Why separate?
- When you pipe program output to file: cargo run > output.txt → normal println goes to file, eprintln still shows on screen.
5. Other Kinds of “Output” You Might Hear About (Advanced)
Sometimes “Rust output” means:
- Program exit code → std::process::exit(0) (0 = success)
- Command output → when running external programs (std::process::Command) → gives Output struct with stdout/stderr bytes
- Build output → what cargo build or cargo run prints in terminal
- Test output → cargo test hides passing test prints by default → use — –nocapture to see them
But 95% of beginner “Rust Output” questions = printing to console with println!.
Quick Summary in Teacher Voice
Rust Output = how your program shows information to you (or user) on the screen. Main tool: println! (with newline) and print! (no newline). Use {} placeholders to insert variables. {:?} / {:#?} for debugging. eprintln! for error/warning messages.
Practice tip: Open your rust_output project and try changing numbers, adding your name, temperature from today — run cargo run many times. Play with formats until it feels natural!
Next lesson ready? Want deep dive on formatting tricks? Or how to read input (so program can talk back-and-forth)? Or debug printing with structs?
Just tell me — your Rust class is going strong! 🦀🚀
