2017-02-13 15:43:15 +08:00
|
|
|
|
## 注释
|
|
|
|
|
|
2017-03-19 14:53:41 +08:00
|
|
|
|
> [ch03-04-comments.md](https://github.com/rust-lang/book/blob/master/second-edition/src/ch03-04-comments.md)
|
2017-02-13 15:43:15 +08:00
|
|
|
|
> <br>
|
2017-03-19 14:53:41 +08:00
|
|
|
|
> commit 4f2dc564851dc04b271a2260c834643dfd86c724
|
2017-02-13 15:43:15 +08:00
|
|
|
|
|
2017-03-19 14:53:41 +08:00
|
|
|
|
所有编程语言都力求使他们的代码易于理解,不过有时需要提供额外的解释。在这种情况下,程序员在源码中留下记录,或者**注释**(*comments*),编译器会忽略他们不过其他阅读代码的人可能会用得上。
|
2017-02-13 15:43:15 +08:00
|
|
|
|
|
|
|
|
|
这是一个注释的例子:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
// Hello, world.
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
在 Rust 中,注释必须以两道斜杠开始并持续到本行的结尾。对于超过一行的注释,需要在每一行都加上`//`,像这样:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
// 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.
|
|
|
|
|
```
|
|
|
|
|
|
2017-03-19 14:53:41 +08:00
|
|
|
|
注释也可以在放在包含代码的行的末尾:
|
2017-02-13 15:43:15 +08:00
|
|
|
|
|
|
|
|
|
<span class="filename">Filename: src/main.rs</span>
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
fn main() {
|
|
|
|
|
let lucky_number = 7; // I’m feeling lucky today.
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2017-03-19 14:53:41 +08:00
|
|
|
|
不过你会经常看到他们被以这种格式使用,也就是位于它所解释的代码行的上面一行:
|
2017-02-13 15:43:15 +08:00
|
|
|
|
|
|
|
|
|
<span class="filename">Filename: src/main.rs</span>
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
fn main() {
|
|
|
|
|
// I’m feeling lucky today.
|
|
|
|
|
let lucky_number = 7;
|
|
|
|
|
}
|
2017-02-14 22:42:54 +08:00
|
|
|
|
```
|
2017-02-13 15:43:15 +08:00
|
|
|
|
|
|
|
|
|
这就是注释的全部。并没有什么特别复杂的。
|