diff --git a/src/error-handling/deriving-error-enums.md b/src/error-handling/deriving-error-enums.md index c38dc56e..1b18ae0f 100644 --- a/src/error-handling/deriving-error-enums.md +++ b/src/error-handling/deriving-error-enums.md @@ -17,7 +17,7 @@ enum ReadUsernameError { } fn read_username(path: &str) -> Result { - let mut username = String::with_capacity(100); + let mut username = String::new(); fs::File::open(path)?.read_to_string(&mut username)?; if username.is_empty() { return Err(ReadUsernameError::EmptyUsername(String::from(path))); diff --git a/src/error-handling/dynamic-errors.md b/src/error-handling/dynamic-errors.md index ea9716dc..dd5b0ae0 100644 --- a/src/error-handling/dynamic-errors.md +++ b/src/error-handling/dynamic-errors.md @@ -4,7 +4,7 @@ Sometimes we want to allow any type of error to be returned without writing our all the different possibilities. `std::error::Error` makes this easy. ```rust,editable,compile_fail -use std::fs::{self, File}; +use std::fs; use std::io::Read; use thiserror::Error; use std::error::Error; @@ -14,8 +14,8 @@ use std::error::Error; struct EmptyUsernameError(String); fn read_username(path: &str) -> Result> { - let mut username = String::with_capacity(100); - File::open(path)?.read_to_string(&mut username)?; + let mut username = String::new(); + fs::File::open(path)?.read_to_string(&mut username)?; if username.is_empty() { return Err(EmptyUsernameError(String::from(path)).into()); } diff --git a/src/error-handling/result.md b/src/error-handling/result.md index 0a6740e7..88d16365 100644 --- a/src/error-handling/result.md +++ b/src/error-handling/result.md @@ -4,11 +4,11 @@ We have already seen the `Result` enum. This is used pervasively when errors are expected as part of normal operation: ```rust,editable -use std::fs::File; +use std::fs; use std::io::Read; fn main() { - let file = File::open("diary.txt"); + let file = fs::File::open("diary.txt"); match file { Ok(mut file) => { let mut contents = String::new(); diff --git a/src/error-handling/try-operator.md b/src/error-handling/try-operator.md index 049a5d81..c960dfbc 100644 --- a/src/error-handling/try-operator.md +++ b/src/error-handling/try-operator.md @@ -19,8 +19,8 @@ some_expression? We can use this to simplify our error handing code: ```rust,editable -use std::fs; -use std::io::{self, Read}; +use std::{fs, io}; +use std::io::Read; fn read_username(path: &str) -> Result { let username_file_result = fs::File::open(path);