Files
docs-rust/ch03/ch03-04-comments.html
2026-06-22 21:27:36 +05:30

44 lines
2.1 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!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>Heres 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, youll 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 youll more often see them used in this format, with the comment on a
separate line above the code its 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 well
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>