如何在文档代码中写入一行但让编译器忽略它?
我要写
/// # Examples
///
/// To clone something, do
///
/// ```
/// IGNORE_FOR_COMPILATION_BUT_SHOW: let cloned = myValue.clone();
/// # let cloned = 5.clone();
/// ```
我想得到:
例子
要克隆一些东西,做
let cloned = myValue.clone();
但编译器仍应编译示例(克隆 5)。
编辑:我也希望 cargo 运行该示例,但省略了一行。
最佳答案
documentation说你可以这样做:
/// ```rust,ignore
/// highlighted as rust code but ignored by rustdoc
/// ```
还有 rust,no_run
编译但不运行示例代码。
或者,您可以使用与在普通代码中相同的解决方案:将其注释掉。
/// ```rust
/// let x=5;
/// // x = 6; // won't be run
/// assert_eq!(x, 5);
/// ```
关于documentation - 如何在文档测试中忽略一行到文档中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42348075/