Disable line numbers on a specific doctest/example?

Hi,

sometimes I write documentation like this:

/// Add two numbers.
///
/// TODO: detailed explanation
///
/// **Note:** This function is equivalent to:
/// ```
/// # let left = 4;
/// # let right = 7;
/// # assert_eq!(
/// left + right
/// # , my_crate::add_two_numbers(left, right)
/// # );
/// ```
/// but you do not have to type that weird math symbol.
///
/// # Example
/// ```
/// let x = 1;
/// let y = 2;
///
/// assert_eq!(my_crate::add_two_numbers(x, y), 3);
/// ```
pub fn add_two_numbers(left: i32, right: i32) -> i32 {
    left + right
}

For the first doctest, which renders as a one-liner left + right, I would like to disable line numbers. For the second one (the actual example) the line numbers are fine. Is that possible?

I would also like to keep it tested (so no ```text).

Note that showing line numbers is a global setting of the generated docs. Everyone can enable or disable line numbers however they see fit. I wouldn't recommend messing with this feature, trying to disable line numbers for one-line snippets, but you can get it done with a custom css class you put on the snippet:

/// ```{.hide-line-numbers}
/// let x = 0;
/// ```
///
/// ```
/// let x = 0;
/// let y = 0;
/// ```
///
fn foo() {}

For this to work you need to add a style.css file to your project with the following statement:

.hide-line-numbers > code > span[data-nosnippet] {
    display: none;
}

And then you can compile your docs with:

cargo rustdoc --lib -- --extend-css style.css
1 Like