Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc book doc example error #117036

Closed
csditchfield opened this issue Oct 22, 2023 · 0 comments · Fixed by #117037
Closed

rustdoc book doc example error #117036

csditchfield opened this issue Oct 22, 2023 · 0 comments · Fixed by #117037
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools

Comments

@csditchfield
Copy link
Contributor

Location

rust/src/doc/rustdoc/src/write-documentation/what-to-include.md
The two examples of doc examples in the "Examples" section.

``````text
/// Example
/// ```rust
/// let fourtytwo = "42".parse::<u32>()?;
/// println!("{} + 10 = {}", fourtytwo, fourtytwo+10);
/// ```
``````

and

``````text
/// Example
/// ```rust
/// # main() -> Result<(), std::num::ParseIntError> {
/// let fortytwo = "42".parse::<u32>()?;
/// println!("{} + 10 = {}", fortytwo, fortytwo+10);
/// # Ok(())
/// # }
/// ```
``````

Summary

This section of the rustdoc book is explaining at a very high level how to write example code within docs and deals specifically with how Results should be handled within examples.

The first example that the rustdoc book provides is intended to fail:

/// Example
/// ```rust
/// let fourtytwo = "42".parse::<u32>()?;
/// println!("{} + 10 = {}", fourtytwo, fourtytwo+10);
/// ```

The second example is intended to correct the mistakes of the first:

/// Example
/// ```rust
/// # main() -> Result<(), std::num::ParseIntError> {
/// let fortytwo = "42".parse::<u32>()?;
/// println!("{} + 10 = {}", fortytwo, fortytwo+10);
/// #     Ok(())
/// # }
/// ```

Unfortunately, this second example fails doc-tests too:

error: expected one of `.`, `;`, `?`, `}`, or an operator, found `->`
 --> src\lib.rs:10:8
  |
3 | main() -> Result<(), std::num::ParseIntError> {
  |        ^^ expected one of `.`, `;`, `?`, `}`, or an operator

error: aborting due to previous error

Couldn't compile the test.

To fix this error as simply as possible, a fn should be added to the definition of main:

/// Example
/// ```rust
/// # fn main() -> Result<(), std::num::ParseIntError> {
/// let fortytwo = "42".parse::<u32>()?;
/// println!("{} + 10 = {}", fortytwo, fortytwo+10);
/// #     Ok(())
/// # }
/// ```

alternatively, a more modern implementation compatible with 1.34 could be considered:

/// Example
/// ```rust
/// let fortytwo = "42".parse::<u32>()?;
/// println!("{} + 10 = {}", fortytwo, fortytwo+10);
/// #     Ok::<(), <u32 as std::str::FromStr>::Err>(())
/// ```

I have included a file (lib.txt) that includes examples using all four of these implementations.
When cargo tested, it produces the following output:

   Doc-tests rust_sandbox

running 4 tests
test src\lib.rs - fiz (line 9) ... FAILED
test src\lib.rs - foo (line 2) ... FAILED
test src\lib.rs - buz (line 29) ... ok
test src\lib.rs - baz (line 19) ... ok

failures:

---- src\lib.rs - fiz (line 9) stdout ----
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `->`
 --> src\lib.rs:10:8
  |
3 | main() -> Result<(), std::num::ParseIntError> {
  |        ^^ expected one of `.`, `;`, `?`, `}`, or an operator

error: aborting due to previous error

Couldn't compile the test.
---- src\lib.rs - foo (line 2) stdout ----
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
 --> src\lib.rs:3:36
  |
2 | fn main() { #[allow(non_snake_case)] fn _doctest_main_src_lib_rs_2_0() {
  |                                      --------------------------------- this function should return `Result` or `Option` to accept `?`
3 | let fourtytwo = "42".parse::<u32>()?;
  |                                    ^ cannot use the `?` operator in a function that returns `()`
  |
  = help: the trait `FromResidual<Result<Infallible, ParseIntError>>` is not implemented for `()`

error: aborting due to previous error

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

failures:
    src\lib.rs - fiz (line 9)
    src\lib.rs - foo (line 2)

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

error: doctest failed, to rerun pass `--doc`

Cargo-Process exited abnormally with code 101 at Sat Oct 21 21:00:42

lib.txt

@csditchfield csditchfield added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Oct 22, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 22, 2023
@bors bors closed this as completed in 77d8a73 Oct 22, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 22, 2023
Rollup merge of rust-lang#117037 - csditchfield:fix_doc_writing_result_example, r=notriddle

rustdoc book doc example error

closes rust-lang#117036

This is the minimal change required to make the second what-to-include.md example valid.
Another more modern solution could be considered:
```
/// Example
/// ```rust
/// let fortytwo = "42".parse::<u32>()?;
/// println!("{} + 10 = {}", fortytwo, fortytwo+10);
/// #     Ok::<(), <u32 as std::str::FromStr>::Err>(())
/// ```
```
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants