码迷,mamicode.com
首页 > 其他好文 > 详细

Rust语法之注释

时间:2015-05-15 09:08:21      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:rust   rustdoc   mozilla   注释   rust语言   

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

     你需要注意的是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中运行这些示例代码。

Rust语法之注释

标签:rust   rustdoc   mozilla   注释   rust语言   

原文地址:http://blog.csdn.net/ucan23/article/details/45725677

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!