tor*_*eyy 4 documentation rust rust-cargo rustdoc
如何在文档代码中写一行,但让编译器忽略它?
我想写
/// # Examples
///
/// To clone something, do
///
/// ```
/// IGNORE_FOR_COMPILATION_BUT_SHOW: let cloned = myValue.clone();
/// # let cloned = 5.clone();
/// ```
Run Code Online (Sandbox Code Playgroud)
我想得到:
要克隆某事,做
let cloned = myValue.clone();
Run Code Online (Sandbox Code Playgroud)
但编译器仍应编译示例(克隆5).
编辑:我也想要货物运行的例子,但遗漏一行.
该文件说,你可以这样做:
/// ```rust,ignore
/// highlighted as rust code but ignored by rustdoc
/// ```
Run Code Online (Sandbox Code Playgroud)
还有rust,no_run哪些编译但不运行示例代码.
或者,您可以使用与普通代码相同的解决方案:将其注释掉.
/// ```rust
/// let x=5;
/// // x = 6; // won't be run
/// assert_eq!(x, 5);
/// ```
Run Code Online (Sandbox Code Playgroud)
如果要在doctests中忽略Rust代码的一部分,可能需要阅读有关运行文档测试的部分.基本上将该代码提取到不同的块中并将该块设置为rust,ignore.
这将IGNORE_FOR_COMPILATION_BUT_SHOW完全忽略,但其余的将运行:
///```rust,ignore
///IGNORE_FOR_COMPILATION_BUT_SHOW: let cloned = myValue.clone();
///```
///```rust
/// # let cloned = 5.clone();
/// ```
Run Code Online (Sandbox Code Playgroud)
如果您希望rustdoc编译您的文档测试,但不运行它,您可以使用 rust,no_run