2015-01-08 12:27:03 -06:00
|
|
|
|
% Comments
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 09:27:16 -05:00
|
|
|
|
Now that we have some functions, it’s a good idea to learn about comments.
|
2014-12-02 08:20:48 -06:00
|
|
|
|
Comments are notes that you leave to other programmers to help explain things
|
|
|
|
|
about your code. The compiler mostly ignores them.
|
|
|
|
|
|
2015-01-08 18:52:50 -06:00
|
|
|
|
Rust has two kinds of comments that you should care about: *line comments*
|
|
|
|
|
and *doc comments*.
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 09:27:16 -05:00
|
|
|
|
```rust
|
|
|
|
|
// Line comments are anything after ‘//’ and extend to the end of the line.
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
|
|
let x = 5; // this is also a line comment.
|
|
|
|
|
|
|
|
|
|
// If you have a long explanation for something, you can put line comments next
|
2015-04-10 09:27:16 -05:00
|
|
|
|
// to each other. Put a space between the // and your comment so that it’s
|
2014-12-02 08:20:48 -06:00
|
|
|
|
// more readable.
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The other kind of comment is a doc comment. Doc comments use `///` instead of
|
|
|
|
|
`//`, and support Markdown notation inside:
|
|
|
|
|
|
2015-04-10 09:27:16 -05:00
|
|
|
|
```rust
|
|
|
|
|
/// Adds one to the number given.
|
2014-12-02 08:20:48 -06:00
|
|
|
|
///
|
2015-03-11 20:11:40 -05:00
|
|
|
|
/// # Examples
|
2014-12-02 08:20:48 -06:00
|
|
|
|
///
|
|
|
|
|
/// ```
|
2015-04-10 09:27:16 -05:00
|
|
|
|
/// let five = 5;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(6, add_one(5));
|
2015-06-07 14:06:58 -05:00
|
|
|
|
/// # fn add_one(x: i32) -> i32 {
|
|
|
|
|
/// # x + 1
|
|
|
|
|
/// # }
|
2015-04-10 09:27:16 -05:00
|
|
|
|
/// ```
|
|
|
|
|
fn add_one(x: i32) -> i32 {
|
|
|
|
|
x + 1
|
2014-12-02 08:20:48 -06:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-07-06 01:55:06 -05:00
|
|
|
|
There is another style of doc comment, `//!`, to comment containing items (e.g.
|
|
|
|
|
crates, modules or functions), instead of the items following it. Commonly used
|
|
|
|
|
inside crates root (lib.rs) or modules root (mod.rs):
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
//! # The Rust Standard Library
|
|
|
|
|
//!
|
|
|
|
|
//! The Rust Standard Library provides the essential runtime
|
|
|
|
|
//! functionality for building portable Rust software.
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-10 09:27:16 -05:00
|
|
|
|
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`.
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-01-31 05:27:57 -06:00
|
|
|
|
You can use the [`rustdoc`](documentation.html) tool to generate HTML documentation
|
2015-04-10 09:27:16 -05:00
|
|
|
|
from these doc comments, and also to run the code examples as tests!
|