1

I wanted to add documentation tests to my code, but I can't seem to get a very basic example working. Here is what I have:

/// Foo function inside foo.rs
///
/// # Examples
///
/// ```
/// foo();
/// ```
fn foo() {
}

The error I get is:

<anon>:2:5: 2:8 error: unresolved name foo [E0425]

I also have a lib.rs but its only contents are:

// Contents of lib.rs
pub mod foo;
user3284913
  • 113
  • 2

1 Answers1

0

I think I figured it out. I added the following line to lib.rs:

pub use self::foo::foo

Then I modified foo.rs to:

/// Foo function inside foo.rs
///
/// # Examples
///
/// ```
/// use foo::foo;
/// foo();
/// ```
fn foo() {
}

After doing so, the documentation test ran successfully.

user3284913
  • 113
  • 2
  • I'd like to add that you can start lines in example code with `///#` so they are omitted in the docs. – llogiq Mar 04 '16 at 08:28
  • `fn foo()` is private, yet with `pub use self::foo::foo` you are [publicly re-exporting it](https://doc.rust-lang.org/reference/visibility-and-privacy.html#re-exporting-and-visibility). This may be an unintended side-effect. – mori Jan 03 '22 at 15:38