44 lines
2.1 KiB
HTML
44 lines
2.1 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>Comments</title>
|
||
</head>
|
||
<body>
|
||
<h2 id="comments"><a class="header" href="#comments">Comments</a></h2>
|
||
<p>All programmers strive to make their code easy to understand, but sometimes
|
||
extra explanation is warranted. In these cases, programmers leave <em>comments</em> in
|
||
their source code that the compiler will ignore but that people reading the
|
||
source code may find useful.</p>
|
||
<p>Here’s a simple comment:</p>
|
||
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
|
||
</span><span class="boring">fn main() {
|
||
</span>// hello, world
|
||
<span class="boring">}</span></code></pre>
|
||
<p>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 <code>//</code> on each line, like this:</p>
|
||
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
|
||
</span><span class="boring">fn main() {
|
||
</span>// So we're doing something complicated here, long enough that we need
|
||
// multiple lines of comments to do it! Whew! Hopefully, this comment will
|
||
// explain what's going on.
|
||
<span class="boring">}</span></code></pre>
|
||
<p>Comments can also be placed at the end of lines containing code:</p>
|
||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
let lucky_number = 7; // I'm feeling lucky today
|
||
}</code></pre>
|
||
<p>But you’ll more often see them used in this format, with the comment on a
|
||
separate line above the code it’s annotating:</p>
|
||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
// I'm feeling lucky today
|
||
let lucky_number = 7;
|
||
}</code></pre>
|
||
<p>Rust also has another kind of comment, documentation comments, which we’ll
|
||
discuss in the <a href="../ch14/ch14-02-publishing-to-crates-io.html">“Publishing a Crate to Crates.io”</a><!-- ignore -->
|
||
section of Chapter 14.</p>
|
||
</body>
|
||
</html>
|