Basics
In Rust, the idiomatic comment style starts a comment with two slashes, and the comment continues until the end of the line. For comments that extend beyond a single line, you’ll need to include // on each line, like this:
Comments can also be placed at the end of lines containing code:
But if you’re a sane person you’ll comment above the code you’re annotating
Documentation comments
/
This is like phpdoc, but for Rust. Documentation comments use three
slashes ///
and support Markdown notation:
Run cargo doc
to generate HTML documentation from the comments.
cargo doc --open
will open documentation in the browser.
Commonly used sections
-
Panics
The scenarios in which the function being documented could panic. Callers of the function who don’t want their programs to panic should make sure they don’t call the function in these situations.
-
Errors
If the function returns a
Result
, describing the kinds of errors that might occur and what conditions might cause those errors to be returned can be helpful to callers so they can write code to handle the different kinds of errors in different ways. -
Safety
If the function is
unsafe
to call, there should be a section explaining why the function is unsafe and covering the invariants that the function expects callers to uphold.
//!
This is used for general comments: