rust - 如何使用具有 doctests 的 Rust 项目实现条件编译?

标签 rust rustdoc

我已经使用条件编译来更改函数的类型签名,现在无法为两种“功能”模式运行相同的 doctest,因此我需要一种选择退出 doctest 的方法。

我尝试合并在正常测试中使用的#[cfg_attr(feature = "rss_loose", ignore)]///rust,ignore 来制作///rust,cfg_attr(feature = "rss_loose", ignore) 但这似乎不起作用。

最佳答案

只需编写两套不同的文档和测试,一切都将按原样运行:

/// ```
/// assert_eq!(42, dt::foo());
/// ```
#[cfg(not(feature = "alternate"))]
pub fn foo() -> u8 { 42 }

/// ```
/// assert_eq!(true, dt::foo());
/// ```
#[cfg(feature = "alternate")]
pub fn foo() -> bool { true }
$ cargo test
   Compiling dt v0.1.0 (file:///private/tmp/dt)
     Running target/debug/dt-c3e297f8592542b5

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

   Doc-tests dt

running 1 test
test foo_0 ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
$ cargo test --features=alternate
   Compiling dt v0.1.0 (file:///private/tmp/dt)
     Running target/debug/dt-c3e297f8592542b5

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

   Doc-tests dt

running 1 test
test foo_0 ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

关于rust - 如何使用具有 doctests 的 Rust 项目实现条件编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38292741/

相关文章:

serialization - Rust bincode enum 二进制序列化

rust - 如何在文档测试中使用具有 `test` 属性的模块?

rust - 如何将任意 Markdown 文件包含为文档属性?

c++ - 如何将 C++ 程序链接到 Rust 程序,然后将该 Rust 程序链接回 C++ 程序? (cpp -> rust -> cpp)

documentation - 如何在文档测试中忽略一行到文档中?

rust - Cargo doc 不会为既是库又是二进制的项目中的私有(private)项目生成文档

rust - Rust 的通用 FromStr 对象可以做什么?

rust - 为什么 iter 在模式保护中使用时可变借用?

iterator - 我如何修复 Clippy 的 needless_range_loop for loops that copy between slices with offset?