Rust Comments and Printing
Comments explain code to human readers, and printing shows results on the screen. Both skills appear in nearly every program you write. Good comments act like labels on kitchen jars: they tell you what sits inside without opening the lid.
Types of Comments
Rust offers three comment styles. The compiler ignores all of them when building the program.
Line Comments
Two slashes start a line comment. Everything after the slashes on that line is skipped.
// This line is a comment
let age = 25; // A comment can follow codeBlock Comments
A block comment starts with /* and ends with */. It can span many lines.
/* This comment
covers several lines */Documentation Comments
Three slashes create a documentation comment. Rust turns these into web pages that describe your functions, using the cargo doc command.
/// Adds two numbers and returns the sum.
fn add(a: i32, b: i32) -> i32 {
a + b
}Printing with println!
The println! macro prints text and moves to a new line. The exclamation mark tells you that it is a macro, not a regular function. Curly braces inside the text mark spots where values appear.
fn main() {
let name = "Maya";
let score = 92;
println!("Student: {}", name);
println!("Score: {}", score);
}Print Without a New Line
Use print! when the next output should continue on the same line.
print!("Loading");
print!("...");
println!(" done");The three calls produce a single line: Loading... done.
Placeholder Styles
Rust supports several placeholder forms. Each form suits a different job.
| Placeholder | Purpose |
|---|---|
| {} | Prints a value in a friendly form |
| {:?} | Prints a value in debug form, useful for vectors and structs |
| {:#?} | Prints debug form spread over many lines |
| {name} | Inserts the variable called name directly |
Inline Variables
Modern Rust lets you place a variable name inside the braces. The code reads like a sentence.
let city = "Pune";
let temp = 31;
println!("{city} reaches {temp} degrees today");Debug Printing
A vector has no friendly form, so {} fails for it. Choose {:?} instead.
let scores = vec![80, 90, 75];
println!("{:?}", scores);
[80, 90, 75]
Formatting Numbers
Extra characters after the colon control width and decimal places.
let price = 3.14159;
println!("{:.2}", price); // 3.14
println!("{:8.2}", price); // " 3.14"
println!("{:05}", 42); // 00042How Output Flows
The diagram below shows where each printing macro sends its text.
println! ------> Standard output (normal screen text) print! ------> Standard output (no new line) eprintln! ------> Standard error (warnings and failures)
Use eprintln! for error messages. Terminal tools can separate error text from regular results, which helps when a program runs inside scripts.
Escaping Braces
Double braces print a literal brace. The line println!("{{ok}}") shows {ok} on screen.
Comment Habits That Help
- Explain why the code exists, not what each symbol does.
- Remove outdated comments when the code changes.
- Keep documentation comments short and accurate.
- Choose clear variable names so fewer comments are required.
Common Mistakes
Forgetting the exclamation mark ranks first. The text println("Hi") fails because println is a macro and needs the exclamation mark.
Passing too few values to a format string causes a compile error. The line println!("{} {}", a) promises two values and delivers one. Count the braces and the arguments.
Using {} with a vector or struct fails, because those types lack a friendly display form. Switch to {:?} and the compiler stays quiet.
Practice Ideas
- Print a small receipt with item names left-aligned and prices showing two decimals.
- Add a documentation comment to a function and run
cargo doc --open. - Print the same variable using
{},{:?}, and{:#?}and compare the results.
Key Points
- Use
//for line notes and///for documentation. println!adds a new line, whileprint!does not.{:?}prints debug output for collections.- Format codes control width and decimals.
