如何选择退出 运行 文档测试?

How to opt out of running a doc test?

我正在编写一个 Rust 库,我想在我的文档中提供示例

  1. 编译为 运行ning cargo test
  2. 的一部分
  3. 运行。

这可能吗?

我正在编写一个数据库客户端库,这些示例使用了一个假设的、不存在的数据库服务器。因此,示例总是在 运行 时失败,但示例在语法上有效很重要。因此我的上述要求。

如果没有办法做我想做的事,那么如何选择不进行 cargo test 运行 特定的文档测试?即,cargo run compile-and-运行 一些文档测试但完全忽略其他一些?

这在 The rustdoc book, specifically the chapter about attributes 中有记录。

您的起始代码块定界符应如下所示:

/// ```no_run

摘自本书:

/// ```no_run
/// loop {
///     println!("Hello, world");
/// }
/// ```

The no_run attribute will compile your code, but not run it. This is important for examples such as "Here's how to retrieve a web page," which you would want to ensure compiles, but might be run in a test environment that has no network access.

要完全省略构建,请使用 ignore 而不是 no_run

将其放入 Cargo.toml:

[lib]
doctest = false

在此处找到:https://doc.rust-lang.org/cargo/commands/cargo-test.html