起因
今天看了Rust的文档注释,以往在源码中在文档看到有代码示例,以为是就单纯就是示例呢? 原来文档注释中的示例是可以进行单元测试的.
代码注释
pub fn code_comment(){
let a = 10;
let b = 20;
}
文档注释
pub fn document_comment(){
println!("文档注释....");
}
cargo doc -p mylib
cargo doc -p mylib --open
包注释,或者是包说明
测试文档注释的示例
cargo test -p mylib
对代码进行格式化
代码未格式化.
pub fn sum(x:i32, y:i32) ->i32 {
x+y
}
使用fmt工具进行格式话.
cargo fmt -p mylib
pub fn sum(x: i32, y: i32) -> i32 {
x + y
}
代码中,有了一个间隔.