您的位置:首页 > 其它

Rust语法之注释

2015-05-15 08:07 267 查看
     既然已经学习了函数,学习注释是个不错的主意。注释是你留给其他程序员帮助介绍你的代码的笔记。编译器将绝大部分的忽略他们。

     你需要注意的是Rust有两种形式的注释:单行注释和文本注释。

// Line comments are anything after ‘//’ and extend to the end of the line.

let x = 5; // this is also a line comment.

// If you have a long explanation for something, you can put line comments next
// to each other. Put a space between the // and your comment so that it’s
// more readable.

     另外一种注释是文本注释。文本注释使用///代替//,并且支持Markdown注解:

/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let five = 5;
///
/// assert_eq!(6, add_one(5));
/// ```
fn add_one(x: i32) -> i32 {
x + 1
}

     当书写文本注释时,提供许多使用的例子是非常非常有用的。你会注意到我们使用了一个新宏:assert_eq!。这个宏比较两个值,如果不相等程序就会出错退出。这在文档中是非常有用的。还有另外一个宏assert!,如果是false程序就报错退出。

     你可以使用rustdoc工具从这些文档注释生成HTML文档,并且可以在tests中运行这些示例代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息