How do you handle the “could not parse code block

2020-05-01 08:40发布

问题:

I'm writing some rust doc examples (that are compiling):

/// ```rust
/// # #[macro_use]
/// # extern crate ...
/// ...
/// ```

But cargo doc gives me this [incorrect] warning:

warning: could not parse code block as Rust code
   --> srml/support/src/dispatch.rs:105:5
    |
105 |   ///    ```rust
    |  ________^
106 | | /// # #[macro_use]
    | |_
    |
    = note: error from rustc: unknown start of token: `
help: mark blocks that do not contain Rust code as text
    |
105 | ///    ```textrust
    |        ^^^^^^^

Should I just suppress this warning.. or is something off here?

回答1:

You fix the error by using valid Rust code inside the code block.


This reproduces the problem:

///    ```rust
///
///    ```
pub fn foo() {}

Don't add spurious whitespace before your code blocks. In Markdown, four spaces counts as the beginning of code, so you have actually done the equivalent of the HTML:

<code>```rust  ```</code>

As it tells you, ``` isn't valid Rust code.



标签: rust rustdoc