rust - 为什么没有围栏的评论会触发 doctest?

标签 rust

<分区>

我在 crate 中有以下注释代码:

/// Complex Expression
///
///     - The `Undefined` variant is used as a placeholder during code 
///       transformations. It must never occur in the final result.
pub enum Cexp {
    Undefined,
    Unimplemented,
}

在 crate 上运行 cargo test 时,它会触发 doctest 运行,但编译失败。 (在 playground 中确保运行 TEST 而不是 BUILD。)

   Doc-tests playground

running 1 test
test src/lib.rs - Cexp (line 3) ... FAILED

failures:

---- src/lib.rs - Cexp (line 3) stdout ----
error: unknown start of token: `
 --> src/lib.rs:4:7
  |
1 | - The `Undefined` variant is used as a placeholder during code 
  |       ^

实际的错误消息无关紧要。 Rust 编译器在注释文本上窒息,这并不奇怪。但为什么这个评论会触发 doctest 运行?根据documentation doctests 应该用 ``` 围起来,这里不是这种情况。

最佳答案

此行为实际上在 documentation 中进行了解释.在最底部它说(强调我的):

Rustdoc also accepts indented code blocks as an alternative to fenced code blocks: instead of surrounding your code with three backticks, you can indent each line by four or more spaces.

...

However, it's preferable to use fenced code blocks over indented code blocks. Not only are fenced code blocks considered more idiomatic for Rust code, but there is no way to use directives such as ignore or should_panic with indented code blocks.

关于rust - 为什么没有围栏的评论会触发 doctest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57159029/

相关文章:

rust - 错误[E0597] : borrowed value does not live long enough when trying to return &'static

Rust 导入错误

rust - 无法在不导致非法指令的情况下传递新类型包装的不安全 C 结构

rust - 取消引用从 Rust 传递给 C 的类型定义指针时的段错误

rust - 如果不是为了对象安全,Rust 的哪些安全保证会丢失?

rust - 如何将枚举的向量转换为该枚举的特定变体的内部值的向量

rust - 为什么使用 println 宏时可以格式化变量,但不能格式化数组元素?

rust - 编译器建议我添加一个 'static lifetime because the parameter type may not live long enough, but I don' t think that's what I want

macos - MacOS 中的 WebView : How to request file system permissions correctly

rust - 如何在 Rust 中声明类型为 <&str, String> 的 hashmap?