Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

コメント

あらゆるプログラムにはコメントが必要です。Rustには何種類かのコメントがあります

Regular Comments

These are ignored by the compiler:

  • Line comments: Start with // and continue to the end of the line
  • Block comments: Enclosed in /* ... */ and can span multiple lines

Documentation Comments (Doc Comments) which are parsed into HTML library documentation:

  • /// - Generates docs for the item that follows it
  • //! - Generates docs for the enclosing item (typically used at the top of a file or module)

fn main() {
    // Line comments start with two slashes.
    // Everything after the slashes is ignored by the compiler.

    // Example: This line won't execute
    // println!("Hello, world!");

    // Try removing the slashes above and running the code again.

    /*
     * Block comments are useful for temporarily disabling code.
     * They can also be nested: /* like this */ which makes it easy
     * to comment out large sections quickly.
     */

    /*
    Note: The asterisk column on the left is just for style - 
    it's not required by the language.
    */

    // Block comments make it easy to toggle code on/off by adding
    // or removing just one slash:

    /* <- Add a '/' here to uncomment the entire block below

    println!("Now");
    println!("everything");
    println!("executes!");
    // Line comments inside remain unaffected

    // */

    // Block comments can also be used within expressions:
    let x = 5 + /* 90 + */ 5;
    println!("Is `x` 10 or 100? x = {}", x);
}

参照

ライブラリドキュメンテーション