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

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

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

10. /*
11. * This is another type of comment, the block comment. In general,
12. * the line comment is the recommended comment style however the
13. * block comment is extremely useful for temporarily disabling
14. * a large chunk of code. /* Block comments can be /* nested, */ */
15. * so it takes only a few keystrokes to comment out all the lines
16. * in this main() function. /*/*/* Try it yourself! */*/*/
17. */

19. /*
20. Note, the previous column of `*` was entirely for style. There's
21. no actual need for it.
22. */

24. // Observe how block comments allow easy expression manipulation
25. // which line comments do not. Deleting the comment delimiters
26. // will change the result:
27. let x = 5 + /* 90 + */ 5;
28. println!("Is `x` 10 or 100? x = {}", x);
29. }

See also:

Library documentation