rustrustdoc

How to try doctesting in play.rust-lang.org? Rust Doctest example doesn't work


The doctest example off Rust-lang's rust-by-example page doesn't work, ironically hehe. How do I use doctesting? I successfully learned how to do unit testing in Rust, the chapter before doctesting. https://doc.rust-lang.org/rust-by-example/testing/doc_testing.html

fn main() {
    println!("Hello 3+2: {}", add(3,2));
}

/// First line is a short summary describing function.
///
/// The next lines present detailed documentation. Code blocks start with
/// triple backquotes and have implicit `fn main()` inside
/// and `extern crate <cratename>`. Assume we're testing `doccomments` crate:
///
/// ```
/// let result = doccomments::add(2, 3);
/// assert_eq!(result, 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=643def7ce943cca11820b379d671e21e

Expected Results

Doctest to run (and pass or fail accordingly)

Actual Results

Doctest doesn't run. Not sure if it's possible to run a doctest in the rust playground due to this crate/module error.

when ran (equivalent to cargo run)

stdout:

Hello 3+2: 5

stderr:

Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.70s
     Running `target/debug/playground`

when tested (equivalent to cargo test)

stdout:

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


running 1 test
test src/lib.rs - add (line 11) ... FAILED

failures:

---- src/lib.rs - add (line 11) stdout ----
error[E0433]: failed to resolve: use of undeclared crate or module `doccomments`
 --> src/lib.rs:12:14
  |
3 | let result = doccomments::add(2, 3);
  |              ^^^^^^^^^^^ use of undeclared crate or module `doccomments`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0433`.
Couldn't compile the test.

failures:
    src/lib.rs - add (line 11)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.21s

stderr:

   Compiling playground v0.0.1 (/playground)
warning: function `main` is never used
 --> src/lib.rs:1:4
  |
1 | fn main() {
  |    ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `playground` (lib) generated 1 warning
    Finished test [unoptimized + debuginfo] target(s) in 0.66s
     Running unittests src/lib.rs (target/debug/deps/playground-a7fbc171eada2644)
   Doc-tests playground
error: doctest failed, to rerun pass `--doc`

Solution

  • In the Rust Playground, the crate generated is playground.

    You can reference playground::<testname> in the doc comments.

    ///
    /// ```
    /// let result = playground::add(2, 3);
    /// assert_eq!(result, 5);
    /// ```
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
    

    https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1b1150ff6a63f423c6f86de0020e8dac