book/src/ch03-04-comments.md

1.5 KiB
Raw Blame History

Comments

All programmers strive to make their code easy to understand, but sometimes extra explanation is warranted. In these cases, programmers leave comments in their source code that the compiler will ignore but people reading the source code may find useful.

Heres a simple comment:

// hello, world

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, youll need to include // on each line, like this:

// So were doing something complicated here, long enough that we need
// multiple lines of comments to do it! Whew! Hopefully, this comment will
// explain whats going on.

Comments can also be placed at the end of lines containing code:

Filename: src/main.rs

{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/src/main.rs}}

But youll more often see them used in this format, with the comment on a separate line above the code its annotating:

Filename: src/main.rs

{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/src/main.rs}}

Rust also has another kind of comment, documentation comments, which well discuss in the “Publishing a Crate to Crates.io” section of Chapter 14.