For a user following the path of reading Chapter 5: Syntax & Symantics prior to Chapter 4: Learn Rust, this will be the first time they have encountered executable tests inside documentation comments. The test will fail because the `add_one` function is not defined in the context of the doctest. This might not be the optimal place to introduce and explain the `/// #` notation but I think it is important that this snippet pass as a test when `rustdoc --test` is run against it if it is going to be shown.
1.5 KiB
% Comments
Now that we have some functions, it’s a good idea to learn about comments. Comments are notes that you leave to other programmers to help explain things about your code. The compiler mostly ignores them.
Rust has two kinds of comments that you should care about: line comments and doc comments.
// Line comments are anything after ‘//’ and extend to the end of the line.
let x = 5; // this is also a line comment.
// If you have a long explanation for something, you can put line comments next
// to each other. Put a space between the // and your comment so that it’s
// more readable.
The other kind of comment is a doc comment. Doc comments use ///
instead of
//
, and support Markdown notation inside:
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let five = 5;
///
/// assert_eq!(6, add_one(5));
/// # fn add_one(x: i32) -> i32 {
/// # x + 1
/// # }
/// ```
fn add_one(x: i32) -> i32 {
x + 1
}
When writing doc comments, providing some examples of usage is very, very
helpful. You’ll notice we’ve used a new macro here: assert_eq!
. This compares
two values, and panic!
s if they’re not equal to each other. It’s very helpful
in documentation. There’s another macro, assert!
, which panic!
s if the
value passed to it is false
.
You can use the rustdoc
tool to generate HTML documentation
from these doc comments, and also to run the code examples as tests!