Rust Comment Types and Documentation

RustRustBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we learn about the different types of comments supported by Rust, including regular comments and doc comments. Regular comments can be line comments, which start with two slashes and go to the end of the line, or block comments, which are enclosed between /* and */ and can be nested. Doc comments, on the other hand, are used to generate HTML library documentation and start with /// or //!.

Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use main.rs, compile and run it with rustc main.rs && ./main.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("`Rust`")) -.-> rust/BasicConceptsGroup(["`Basic Concepts`"]) rust(("`Rust`")) -.-> rust/FunctionsandClosuresGroup(["`Functions and Closures`"]) rust(("`Rust`")) -.-> rust/MemorySafetyandManagementGroup(["`Memory Safety and Management`"]) rust(("`Rust`")) -.-> rust/ErrorHandlingandDebuggingGroup(["`Error Handling and Debugging`"]) rust/BasicConceptsGroup -.-> rust/variable_declarations("`Variable Declarations`") rust/FunctionsandClosuresGroup -.-> rust/function_syntax("`Function Syntax`") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("`Expressions and Statements`") rust/MemorySafetyandManagementGroup -.-> rust/lifetime_specifiers("`Lifetime Specifiers`") rust/ErrorHandlingandDebuggingGroup -.-> rust/error_propagation("`Error Propagation`") subgraph Lab Skills rust/variable_declarations -.-> lab-99185{{"`Rust Comment Types and Documentation`"}} rust/function_syntax -.-> lab-99185{{"`Rust Comment Types and Documentation`"}} rust/expressions_statements -.-> lab-99185{{"`Rust Comment Types and Documentation`"}} rust/lifetime_specifiers -.-> lab-99185{{"`Rust Comment Types and Documentation`"}} rust/error_propagation -.-> lab-99185{{"`Rust Comment Types and Documentation`"}} end

Comments

Any program requires comments, and Rust supports a few different varieties:

  • Regular comments which are ignored by the compiler:
    • // Line comments which go to the end of the line.
    • /* Block comments which go to the closing delimiter. */
  • Doc comments which are parsed into HTML library documentation:
    • /// Generate library docs for the following item.
    • //! Generate library docs for the enclosing item.
fn main() {
    // This is an example of a line comment.
    // There are two slashes at the beginning of the line.
    // And nothing written after these will be read by the compiler.

    // println!("Hello, world!");

    // Run it. See? Now try deleting the two slashes, and run it again.

    /*
     * This is another type of comment, a block comment. In general,
     * line comments are the recommended comment style. But block comments
     * are extremely useful for temporarily disabling chunks of code.
     * /* Block comments can be /* nested, */ */ so it takes only a few
     * keystrokes to comment out everything in this main() function.
     * /*/*/* Try it yourself! */*/*/
     */

    /*
    Note: The previous column of `*` was entirely for style. There's
    no actual need for it.
    */

    // You can manipulate expressions more easily with block comments
    // than with line comments. Try deleting the comment delimiters
    // to change the result:
    let x = 5 + /* 90 + */ 5;
    println!("Is `x` 10 or 100? x = {}", x);
}

Summary

Congratulations! You have completed the Comments lab. You can practice more labs in LabEx to improve your skills.

Other Rust Tutorials you may like