评论


所有程序员都努力使他们的代码易于理解,但有时需要额外的解释。在这些情况下,程序员会在源代码中留下注释,编译器会忽略这些注释,但阅读源代码的人可能会觉得有用。


这是一个简单的评论:

#![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” 中讨论。 第 14 章的章节。