评论

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

这是一个简单的评论:

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

在 Rust 中,惯用的注释样式以两个斜杠开始注释,而 注释一直持续到行尾。对于超出 single line,您需要在每行中包含,如下所示://

#![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 还有另一种注释,文档注释,我们将 在第 14 章的 “将 Crate 发布到 Crates.io” 一节中讨论。

本文档由官方文档翻译而来,如有差异请以官方英文文档(https://doc.rust-lang.org/)为准