-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Persist printing error, NOT ErrorKind #4259
Conversation
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.
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. |
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()
}
},
};
} New 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:?}"),
},
_ => {
panic!("Problem opening the file: {error:?}"); // Panics with the error
}
},
};
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. I agree that we should keep using error
for consistency. 👍🏼
@chriskrycho - thanks for the approval! I'll keep reading through the book and making (hopefully small) edits to make sure that a) I understand the content and b) maybe others can have an easier on-ramp with the book. |
Update books ## rust-lang/book 23 commits in 81a976a237f84b8392c4ce1bd5fd076eb757a2eb..45f05367360f033f89235eacbbb54e8d73ce6b70 2025-03-21 23:23:52 UTC to 2025-03-13 14:14:37 UTC - Ch. 21: call out Chrome multiple-connections issue (rust-lang/book#4297) - Ch. 16: refactor 16-6 to using listing component (rust-lang/book#4295) - Ch. 01: Show how to work offline (rust-lang/book#4294) - Ch. 07: Clarify sentences about `pub use` (rust-lang/book#4293) - Ch. 02: Consistent ordering of `use` statements (rust-lang/book#4292) - Anchors on listings (rust-lang/book#4271) - Ch. 17: another tweak to how we phrase things about sections (rust-lang/book#4288) - Ch. 20: correct listing number (rust-lang/book#4287) - Ch. 10.3: clarify language detail (rust-lang/book#4284) - Ch. 17: minor typos and link reference (rust-lang/book#4286) - Ch. 9: correctly demonstrate privacy with module (rust-lang/book#4282) - Ch. 18: correct discussion of delegation in `Post` methods (rust-lang/book#4281) - Ch. 20: tell folks to see the Reference for more ABI info (rust-lang/book#4165) - Ch 10.1 minor clarifications (rust-lang/book#4256) - Clarified the misunderstanding b/w crates, module, items (rust-lang/book#4232) - Ferris: always show, even when it’s small (rust-lang/book#4280) - Ch. 17: mention `use std::pin::{Pin, pin};` on introduction (rust-lang/book#4279) - Persist printing error, NOT ErrorKind (rust-lang/book#4259) - Typo: "2" should be "2 seconds" (rust-lang/book#4263) - Ch. 17: fix tiny example consistency issue (rust-lang/book#4270) - Bump ring from 0.17.8 to 0.17.13 in /listings/ch17-async-await/listing-17-02 (rust-lang/book#4261) - Bump ring from 0.17.8 to 0.17.14 in /packages/trpl (rust-lang/book#4273) - 2024 Print Edition: updates to Word docs and more fixes to Markdown text (rust-lang/book#4272) ## rust-lang/reference 5 commits in dda31c85f2ef2e5d2f0f2f643c9231690a30a626..e95ebdfee02514d93f79ec92ae310a804e87f01f 2025-03-24 15:56:46 UTC to 2025-03-18 02:25:06 UTC - Fix diagnostic attribute typo (rust-lang/reference#1767) - Mention that “every address” ≠ “every pointer” (rust-lang/reference#1761) - Rework range pattern rules (rust-lang/reference#1756) - Use warning block in behavior-considered-undefined (rust-lang/reference#1759) - Add reference for asm-goto (rust-lang/reference#1693)
Rollup merge of rust-lang#138894 - rustbot:docs-update, r=ehuss Update books ## rust-lang/book 23 commits in 81a976a237f84b8392c4ce1bd5fd076eb757a2eb..45f05367360f033f89235eacbbb54e8d73ce6b70 2025-03-21 23:23:52 UTC to 2025-03-13 14:14:37 UTC - Ch. 21: call out Chrome multiple-connections issue (rust-lang/book#4297) - Ch. 16: refactor 16-6 to using listing component (rust-lang/book#4295) - Ch. 01: Show how to work offline (rust-lang/book#4294) - Ch. 07: Clarify sentences about `pub use` (rust-lang/book#4293) - Ch. 02: Consistent ordering of `use` statements (rust-lang/book#4292) - Anchors on listings (rust-lang/book#4271) - Ch. 17: another tweak to how we phrase things about sections (rust-lang/book#4288) - Ch. 20: correct listing number (rust-lang/book#4287) - Ch. 10.3: clarify language detail (rust-lang/book#4284) - Ch. 17: minor typos and link reference (rust-lang/book#4286) - Ch. 9: correctly demonstrate privacy with module (rust-lang/book#4282) - Ch. 18: correct discussion of delegation in `Post` methods (rust-lang/book#4281) - Ch. 20: tell folks to see the Reference for more ABI info (rust-lang/book#4165) - Ch 10.1 minor clarifications (rust-lang/book#4256) - Clarified the misunderstanding b/w crates, module, items (rust-lang/book#4232) - Ferris: always show, even when it’s small (rust-lang/book#4280) - Ch. 17: mention `use std::pin::{Pin, pin};` on introduction (rust-lang/book#4279) - Persist printing error, NOT ErrorKind (rust-lang/book#4259) - Typo: "2" should be "2 seconds" (rust-lang/book#4263) - Ch. 17: fix tiny example consistency issue (rust-lang/book#4270) - Bump ring from 0.17.8 to 0.17.13 in /listings/ch17-async-await/listing-17-02 (rust-lang/book#4261) - Bump ring from 0.17.8 to 0.17.14 in /packages/trpl (rust-lang/book#4273) - 2024 Print Edition: updates to Word docs and more fixes to Markdown text (rust-lang/book#4272) ## rust-lang/reference 5 commits in dda31c85f2ef2e5d2f0f2f643c9231690a30a626..e95ebdfee02514d93f79ec92ae310a804e87f01f 2025-03-24 15:56:46 UTC to 2025-03-18 02:25:06 UTC - Fix diagnostic attribute typo (rust-lang/reference#1767) - Mention that “every address” ≠ “every pointer” (rust-lang/reference#1761) - Rework range pattern rules (rust-lang/reference#1756) - Use warning block in behavior-considered-undefined (rust-lang/reference#1759) - Add reference for asm-goto (rust-lang/reference#1693)
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.