Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

注释

ch03-04-comments.md

所有程序员都努力让自己的代码易于理解,不过有时仍然需要额外的解释。在这种情况下,程序员会在源码中留下 注释comments),编译器会忽略它们,但阅读源码的人可能会觉得这些注释很有帮助。

这是一个简单的注释:

#![allow(unused)]
fn main() {
// hello, world
}

在 Rust 中,惯用的注释风格是用两个斜杠开始一条注释,并让注释持续到该行末尾。对于跨越多行的注释,你需要在每一行前面都加上 //,像这样:

#![allow(unused)]
fn main() {
// 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.
}

注释也可以放在包含代码的行的末尾:

文件名:src/main.rs

fn main() {
    let lucky_number = 7; // I'm feeling lucky today
}

不过,你更常见到的用法是把注释放在它所解释的代码上一行,像这样:

文件名:src/main.rs

fn main() {
    // I'm feeling lucky today
    let lucky_number = 7;
}

Rust 还有另一种注释,叫作文档注释,我们会在第十四章的“将 crate 发布到 Crates.io”部分讨论它。