Skip to content

Update doc examples that use ? (Carrier) to use Termination #49233

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

Closed
frewsxcv opened this issue Mar 21, 2018 · 5 comments
Closed

Update doc examples that use ? (Carrier) to use Termination #49233

frewsxcv opened this issue Mar 21, 2018 · 5 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools

Comments

@frewsxcv
Copy link
Member

Blocked on #49162

Example:

rust/src/libstd/fs.rs

Lines 40 to 49 in c19264f

/// ```no_run
/// use std::fs::File;
/// use std::io::prelude::*;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut file = File::create("foo.txt")?;
/// file.write_all(b"Hello, world!")?;
/// # Ok(())
/// # }
/// ```

Instead of:

use std::fs::File;
use std::io::prelude::*;

# fn foo() -> std::io::Result<()> {
let mut file = File::create("foo.txt")?;
file.write_all(b"Hello, world!")?;
# Ok(())
# }

We can now do:

use std::fs::File;
use std::io::Error;
use std::io::prelude::*;

fn main() -> Result<(), Error> {
    let mut file = File::create("foo.txt")?;
    file.write_all(b"Hello, world!")?;
    Ok(())
}

Which can be copied/pasted straight from the docs such that it will compile, unlike the old example

@frewsxcv frewsxcv added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Mar 21, 2018
@steveklabnik
Copy link
Member

Big 👍 from me

@GuillaumeGomez
Copy link
Member

Seems a good idea to me as well.

@QuietMisdreavus
Copy link
Member

I think this will be a good thing for combating the most common error of pasting the trimmed examples into fn main() without seeing or considering the ?. Being able to have main return the error like that will greatly help cut down the boilerplate these currently have, and allow us to stop hiding most of the test.

@frewsxcv
Copy link
Member Author

cool, i'll start working on this then 🤸‍♂️

@frewsxcv
Copy link
Member Author

opened a pr for this: #49357

frewsxcv added a commit to frewsxcv/rust that referenced this issue Mar 28, 2018
bors added a commit that referenced this issue Mar 28, 2018
…GuillaumeGomez

Remove hidden `foo` functions from doc examples; use `Termination` trait.

Fixes #49233.

Easier to review with the white-space ignoring `?w=1` feature: https://github.com/rust-lang/rust/pull/49357/files?w=1
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

No branches or pull requests

4 participants