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

标签 documentation rust rust-cargo rustdoc

如何在文档代码中写入一行但让编译器忽略它?

我要写

/// # 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/

相关文章:

rust - 如何从闭包内部修改在闭包外部定义的变量?

rust - 如何在Rust的 cargo 检查中解决E0658(下划线命名)?

docker - 在 docker 中构建 rust 项目导致 Cargo 卡住下载依赖项

r - 如何交叉引用 R 帮助文件/roxygen2 中的方程

php - Doxygen 和许可证/版权信息

c# - 使用 CodeDOM 以编程方式生成 C# 代码文档(摘要)

rust - 为什么在 Rust/Wasm 初学者书籍的第 4.2 节中,从 JavaScript 传递到 Wasm 的字符串总是空的?

rust - 调用交换两个值的 FFI 函数的正确方法是什么?

installation - 通过 cargo : specified package has no binaries 安装箱子时出错

c# - 您应该为接口(interface)、具体实现或两者编写 XML 注释吗?