Comments

Any program requires comments and indeed 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.
  1. fn main() {
  2. // This is an example of a line comment
  3. // Notice how there are two slashes at the beginning of the line
  4. // And that nothing written inside these will be read by the compiler
  5. // println!("Hello, world!");
  6. // Run it. See? Now try deleting the two slashes, and run it again.
  7. /*
  8. * This is another type of comment, the block comment. In general,
  9. * the line comment is the recommended comment style however the
  10. * block comment is extremely useful for temporarily disabling
  11. * a large chunk of code. /* Block comments can be /* nested, */ */
  12. * so it takes only a few keystrokes to comment out all the lines
  13. * in this main() function. /*/*/* Try it yourself! */*/*/
  14. */
  15. /*
  16. Note, the previous column of `*` was entirely for style. There's
  17. no actual need for it.
  18. */
  19. // Observe how block comments allow easy expression manipulation
  20. // which line comments do not. Deleting the comment delimiters
  21. // will change the result:
  22. let x = 5 + /* 90 + */ 5;
  23. println!("Is `x` 10 or 100? x = {}", x);
  24. }

See also:

Library documentation