liveBook · Manning

Comment

As explained in Learn Rust in a Month of Lunches

A comment is source text that the compiler ignores as program instructions and that can contain explanatory or documentation text.

A comment is source text written for programmers rather than the computer. The compiler ignores it as program instructions, so comments can explain code, preserve reminders, or record why a choice was made. section 1.2

Rust provides three useful comment forms:

  • `//` begins a comment that continues to the end of the line. section 1.2
  • `/* ... */` surrounds a comment and can fit between code or span multiple lines. section 1.2
  • `///` creates a documentation comment that Rust can turn into documentation for code. section 1.2

Explain a small program

In `fn main() { let some_number = 100; // This number is important }`, everything after `//` is ignored by the compiler. A longer explanation can use `/* ... */`, while `/// Converts a string slice in a given base to an integer.` can appear in generated documentation.

/// This is a struct that does nothing
pub struct DoesNothing {}
 
/// This struct only has one method.
pub struct PrintThing {}
 
impl PrintThing {
    /// This function just prints a message.
    pub fn prints_something() {
        println!("I am printing something");
    }
}

Browse documented structs and methods to see how `///` comments become descriptions in generated API documentation. section 18.4

How Learn Rust in a Month of Lunches covers it

How deep it goes
Distinguishes line, block, and documentation comments with focused syntax examples, but defers advanced documentation details.
Examples
Shows inline explanations, multiline block comments, TODO reminders, and documented structs and methods.
What it assumes
Assumes only minimal Rust code familiarity and teaches comment syntax alongside a simple variable declaration.
What you can do after
Apply Rust comment syntax to annotate code and expose formal descriptions through generated documentation.
How it connects
Connects comments with the compiler, variables, documentation, and Cargo-generated API documentation.

Also explained in

Compare how 10 books explain comment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.