g-var.com | G-VAR's Blog

Rust 1.1| 注释

Sun Jul 26, 2015

转载请注明出处!

译文链接:Rust 1.1 | 注释

英文原文:Comments


任何程序都需要注释,并且实际上Rust中支持几种不同的注释形式:

  • 常规注释会被编译器忽略掉:
    • // 行注释,一直到行的末尾。
    • /* 块注释,一直到结束分隔符。*/
  • 文档注释会被解析成HTML形式的库文档
    • /// 为后面的内容生成库文档。
    • //! 将包含的内容生成库文档。

例子:

fn main() {
    // This is an example of a line comment
    // Notice how there are two slashes at the beginning of the line
    // And that nothing written inside these will be read by the compiler

    // println!("Hello, world!");

    // Run it. See? Now try deleting the two slashes, and run it again.

    /* 
     * This is another type of comment, the block comment. In general,
     * the line comment is the recommended comment style however the
     * block comment is extremely useful for debugging
     */

     /*
     Note, the previous column of `*` was entirely for style. There's
     no actual need for it.
     */

     // Observe how block comments allow easy expression manipulation
     // which line comments do not. Deleting the comment deliminators
     // will change the result:
     let x = 5 + /* 90 + */ 5;
     println!("Is `x` 10 or 100? x = {}", x);
}

编译:

$ rustc comment.rs

输出:

$ ./comment
Is `x` 10 or 100? x = 10

请参考:

Library documentation

[ 转载必须在正文中标注并保留原文链接、译文链接等信息。]



  « Previous: Next: »