文档注释

  1. //! 是对my_crate包的介绍
  2. /// #Example加上markdown写```是示例
  3. pub之后的为函数体
  4. 该文档只限于lib中使用
1
cargo new mylib --lib
  • 在mylib/lib.rs中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//! My Crate
//!
//! 'my_crate' is a collection of utilites to make performing certain calcuations more convenient
//!
/// add one to the number
///
/// #Example
///
/// ```
/// let five = 5;
///
/// assert_eq!(6,mylib::add_one(5));
/// ```
pub fn add_one(x: i32) -> i32 {
x + 1
}//测试示例

//#[cfg(test)]
//mod tests {
// #[test]
// fn it_works() {
// assert_eq!(2+2,4);
// }
// }
  • 之后再运行cargo doc
1
cargo doc

在文件中出现

1
2
3
4
5
6
7
8
9
10
src-----
|
-------main.rs
Cargo.toml
Cargo.lock
target---
|
-------doc------------mylib-------index.html//说明文档
| //多出的文件夹
-------debug
  • 使用doc --open
1
cargo doc --open

这会打开你的文档

  • 使用cargo test可以测试这个例子
1
cargo test