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

Persist printing error, NOT ErrorKind #4259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

distortedsignal
Copy link

@distortedsignal distortedsignal commented Mar 4, 2025

Reading through the Rust Book, I found this code that appears to be trying to take different actions based on the error type. Looking closer, I realized that the author changed the code (I believe, unintentionally) to log only the error type instead of the whole error when certain conditions are met.

This is a simple fix - the wildcard match for other_error is removed, and replaced with a non-binding wildcard, and the panic is changed to consuming the base error. This is better because it changes the minimal amount of functionality in the example code and it refamiliarizes readers with the non-binding wildcard matching system.

Reading through the Rust Book, I found this code that appears to be
trying to take different actions based on the error type. Looking
closer, I realized that the author changed the code (I believe,
unintentionally) to log only the error type instead of the whole
error when certain conditions are met.

This is a simple fix - the wildcard match for `other_error` is
removed, and replaced with a non-binding wildcard, and the panic
is changed to consuming the base error. This is better because it
changes the minimal amount of functionality in the example code
and it refamiliarizes readers with the wildcard matching system.
@distortedsignal
Copy link
Author

I fired off this PR before I read the rest of the chapter - the highlight box actually calls out that the code in listing 9-6 should have the same effect as in 9-5. This PR brings the code in 9-5 into alignment with the listing in 9-6.

@distortedsignal
Copy link
Author

Supporting documentation:

Listing 9-4:

use std::fs::File;

fn main() {
    let greeting_file_result = File::open("hello.txt");

    let greeting_file = match greeting_file_result {
        Ok(file) => file,
        Err(error) => panic!("Problem opening the file: {error:?}"), // Panics with the error
    };
}

Listing 9-6:

use std::fs::File;
use std::io::ErrorKind;

fn main() {
    let greeting_file = File::open("hello.txt").unwrap_or_else(|error| {
        if error.kind() == ErrorKind::NotFound {
            File::create("hello.txt").unwrap_or_else(|error| {
                panic!("Problem creating the file: {error:?}");
            })
        } else {
            panic!("Problem opening the file: {error:?}"); // Panics with the error
        }
    });
}

Old listing 9-5:

use std::fs::File;
use std::io::ErrorKind;

fn main() {
    let greeting_file_result = File::open("hello.txt");

    let greeting_file = match greeting_file_result {
        Ok(file) => file,
        Err(error) => match error.kind() {
            ErrorKind::NotFound => match File::create("hello.txt") {
                Ok(fc) => fc,
                Err(e) => panic!("Problem creating the file: {e:?}"),
            },
            other_error => {
                panic!("Problem opening the file: {other_error:?}"); // Panics with the result of error.kind()
            }
        },
    };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant