代码注释
文档注释
- 文档注释需要位于 lib 类型的包中,例如 src/lib.rs 中
- 文档注释可以使用 markdown语法!例如 # Examples 的标题,以及代码块高亮
- 被注释的对象需要使用 pub 对外可见,记住:文档注释是给用户看的,内部实现细节不应该被暴露出去
pub fn add_one(x: i32) -> i32 { x + 1 }
|
文档块注释
let arg = 5;
let answer = my_crate::add_two(arg);
assert_eq!(7, answer);
*/ pub fn add_two(x: i32) -> i32 { x + 2 }
|
cargo test 可以运行文档中的测试
查看文档doc
cargo doc
cargo doc --open
|
包和模块注释
保留测试 隐藏文档
在某些时候,希望保留文档测试的功能,但是又要将某些测试用例的内容从文档中隐藏起来:
pub fn try_div(a: i32, b: i32) -> Result<i32, String> { if b == 0 { Err(String::from("Divide-by-zero")) } else { Ok(a / b) } }
|
文档注释中的代码跳转
pub fn add_one(x: i32) -> Option<i32> { Some(x + 1) }
|
use std::sync::mpsc::Receiver;
pub struct AsyncReceiver<T> { sender: Receiver<T>, }
impl<T> AsyncReceiver<T> { pub async fn recv() -> T { unimplemented!() } }
pub mod a { pub fn add_one(x: i32) -> Option<i32> { Some(x + 1) } }
pub struct MySpecialFormatter;
|
pub struct Bar;
pub struct Foo {}
pub fn Foo() {}
#[macro_export] macro_rules! foo { () => {} }
|
文档搜索别名
#[doc(alias = "x")] #[doc(alias = "big")] pub struct BigX;
#[doc(alias("y", "big"))] pub struct BigY;
|