Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot ignore failures to compile example code in documentation

I am trying to write example code for my Rust library, but I don't need the sample code to be compiled.

Steps to reproduce:

  1. cargo new

  2. Add this to 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

I get an error:

$ 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

How do I ignore failures to compile this example code? I'm using Rust 1.13.0 and Cargo 0.13.0.

like image 519
mrLSD Avatar asked Sep 14 '25 16:09

mrLSD


1 Answers

You have hit a known issue in Rust's documentation parser. The Markdown parser used by Rust (Hoedown) does not seem to recognise a fenced code block (the three backticks) properly unless there is a blank line before it. There is some dispute whether this is desired behaviour or not, but either way the problem can be solved by revising your example as follows:

//! ## 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!");
}

Notice the leading //! line before the code block, which allows Hoedown to successfully identify the code block and ignore it appropriately.

like image 142
Aurora0001 Avatar answered Sep 17 '25 21:09

Aurora0001