【问题标题】:Cannot ignore failures to compile example code in documentation不能忽略在文档中编译示例代码的失败
【发布时间】:2016-11-30 01:07:25
【问题描述】:

我正在尝试为我的 Rust 库编写示例代码,但我不需要编译示例代码。

重现步骤:

  1. cargo new

  2. 将此添加到src/lib.rs

    //! ## How to use
    //! Validator usage:
    //! ```ignore
    //! fn validate(values: &Map) -> ValidateResults {
    //!    ValidateResults(vec!(
    //!        Validator::<String>::new(btreemap! {
    //!            "requiered".to_string() => true.to_json(),
    //!            "vtype".to_string() => "string".to_json(),
    //!        }).validate("title".to_string(), values.find(&["pages", "title"]$
    //!
    //!        Validator::<bool>::new(btreemap! {
    //!            "default".to_string() => false.to_json(),
    //!        }).validate("published".to_string(), values.find(&["published"])$
    //!    ))
    //! }
    //! ```
    pub fn main() {
        println!("Hello, world!");
    }
    
  3. cargo test

我收到一个错误:

$ cargo test
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
     Running target/debug/deps/sample-661c50cdfb6a999f

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

   Doc-tests sample

running 1 test
test _0 ... FAILED

failures:

---- _0 stdout ----
    error: expected one of `.`, `;`, `?`, `}`, or an operator, found `,`
 --> <anon>:4:69
  |
4 |    }).validate("published".to_string(), values.find(&["published"])),
  |                                                                     ^

error: macro undefined: 'btreemap!'
 --> <anon>:2:31
  |
2 |        Validator::<bool>::new(btreemap! {
  |                               ^^^^^^^^

error: aborting due to 2 previous errors

thread '_0' panicked at 'Box<Any>', ../src/librustc_errors/lib.rs:694
note: Run with `RUST_BACKTRACE=1` for a backtrace.
thread '_0' panicked at 'couldn't compile the test', ../src/librustdoc/test.rs:283


failures:
    _0

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured

error: test failed

如何忽略编译此示例代码的失败?我正在使用 Rust 1.13.0 和 Cargo 0.13.0。

【问题讨论】:

  • 降价的乐趣……

标签: rust rust-cargo


【解决方案1】:

你在 Rust 的文档解析器中找到了 known issue。 Rust (Hoedown) 使用的 Markdown 解析器似乎无法正确识别围栏代码块(三个反引号),除非它前面有一个空行。有some dispute这是否是所需的行为,但无论哪种方式,问题都可以通过修改您的示例来解决,如下所示:

//! ## How to use
//! Validator usage:
//!
//! ```ignore
//! fn validate(values: &Map) -> ValidateResults {
//!    ValidateResults(vec!(
//!        Validator::<String>::new(btreemap! {
//!            "requiered".to_string() => true.to_json(),
//!            "vtype".to_string() => "string".to_json(),
//!        }).validate("title".to_string(), values.find(&["pages", "title"]$
//!
//!        Validator::<bool>::new(btreemap! {
//!            "default".to_string() => false.to_json(),
//!        }).validate("published".to_string(), values.find(&["published"])$
//!    ))
//! }
//! ```
pub fn main() {
    println!("Hello, world!");
}

注意代码块前的//! 行,这使Hoedown 能够成功识别代码块并适当地忽略它。

【讨论】:

    猜你喜欢
    • 2014-08-07
    • 2017-05-26
    • 1970-01-01
    • 2015-11-04
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    相关资源
    最近更新 更多