Lesson 2 of 22
Lesson Progress: 0%
Comments are notes in your source code that the compiler ignores — they are written for humans, not the computer. Rust supports three styles of comments: single-line comments with //, block comments with /* ... */, and documentation comments with ///. Comments help you explain what your code does, why you made certain decisions, and how to use your functions. They are invaluable when you revisit your own code months later or when other people need to understand your work. Block comments can span multiple lines, making them useful for temporarily disabling larger sections of code or writing longer explanations. Documentation comments (///) are special because Rust can generate HTML documentation from them using the rustdoctool. As a beginner, you should practice adding comments to your programs — it will make you a clearer thinker and a better communicator.
fn main() {
// I am a comment
println!("Hello");
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
// start a single-line comment in Rust.// on the same line is ignored by the compiler.println!("Hello") statement runs normally because the comment above it does not affect execution.Hello— the comment produces no output.fn main() {
/* println!("A");
println!("B"); */
println!("Hi");
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
/* starts a block comment and */ ends it.println!("A") and println!("B") do not run.println!("Hi"), so the output is Hi.fn main() {
println!("A"); // prints letter A
println!("B"); // prints letter B
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
// prints letter A describes what the preceding line does.println! calls execute.A on one line and B on the next line.fn main() {
// println!("Hidden");
println!("Visible");
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
// at the start of a line of code is called commenting out that line.// println!("Hidden"); prevents the println! from running.println!("Visible") executes, so the output is Visible.// prefix./// Prints a friendly greeting
fn greet() {
println!("Hello!");
}
fn main() {
greet();
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
/// create a documentation comment (also called a doc comment).rustdoc tool can read /// comments and generate HTML documentation.greet() function runs normally and prints Hello!.fn main() {
// Single line
/* Block */
println!("Done");
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
// and block comments /* */ in the same program.// Single line occupies one line, and /* Block */ appears on its own line.Done from the println! call.Test Incomplete