文档测试 #

一、文档注释 #

rust
/// 将两个数相加
/// 
/// # Examples
/// 
/// ```
/// let result = my_project::add(2, 3);
/// assert_eq!(result, 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

二、运行文档测试 #

bash
cargo test --doc

三、忽略测试 #

rust
/// ```ignore
/// let result = some_unstable_feature();
/// ```

四、no_run #

rust
/// ```no_run
/// loop {
///     println!("运行中");
/// }
/// ```

五、总结 #

本章学习了:

  • 文档注释语法
  • 文档测试运行
  • ignore 和 no_run
最后更新:2026-03-27