diff --git a/exercises/24_file_io/file_io2.rs b/exercises/24_file_io/file_io2.rs index 279c17dc..37680891 100644 --- a/exercises/24_file_io/file_io2.rs +++ b/exercises/24_file_io/file_io2.rs @@ -24,23 +24,22 @@ fn main() -> Result<(), std::io::Error> { let mut buffered_file_writer = BufWriter::new(output_file); - let mut line_number = 1; + let mut lines_processed = 0; - for line in buffered_input_file.lines() { + for (index, line) in buffered_input_file.lines().enumerate() { let line = line.inspect_err(|err| { eprintln!("{} line parse error {:?}", TEST_INPUT_FILE_NAME, err); })?; - buffered_file_writer - .write(format!("Line {} : {}\n", line_number, line).as_bytes()) - .inspect_err(|err| { - eprintln!("{} line write error {:?}", TEST_INPUT_FILE_NAME, err); - })?; + let line_number = index + 1; + writeln!(buffered_file_writer, "Line {} : {}", line_number, line).inspect_err(|err| { + eprintln!("{} line write error {:?}", TEST_INPUT_FILE_NAME, err); + })?; - line_number += 1; + lines_processed = line_number; } - println!("{} : lines processed", line_number - 1); + println!("{} : lines processed", lines_processed); file_cleanup() } diff --git a/exercises/24_file_io/file_io3.rs b/exercises/24_file_io/file_io3.rs index 0f25c03d..52cc8ed0 100644 --- a/exercises/24_file_io/file_io3.rs +++ b/exercises/24_file_io/file_io3.rs @@ -38,10 +38,9 @@ fn main() -> Result<(), std::io::Error> { fn create_required_files() -> Result<(), std::io::Error> { let file_path = sample_file_path(); - let dir_path = match file_path.parent() { - Some(parent) => parent, - None => return Err(Error::other("Could not get parent path")), - }; + let dir_path = file_path + .parent() + .ok_or_else(|| Error::other("Could not get parent path"))?; if !dir_path.exists() { fs::create_dir_all(dir_path).inspect_err(|x| { diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index fd67e70b..4b2cb0f7 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -23,23 +23,22 @@ fn main() -> Result<(), std::io::Error> { let mut buffered_file_writer = BufWriter::new(output_file); - let mut line_number = 1; + let mut lines_processed = 0; - for line in buffered_input_file.lines() { + for (index, line) in buffered_input_file.lines().enumerate() { let line = line.inspect_err(|err| { eprintln!("{} line parse error {:?}", TEST_INPUT_FILE_NAME, err); })?; - buffered_file_writer - .write(format!("Line {} : {}\n", line_number, line).as_bytes()) - .inspect_err(|err| { - eprintln!("{} line write error {:?}", TEST_INPUT_FILE_NAME, err); - })?; + let line_number = index + 1; + writeln!(buffered_file_writer, "Line {} : {}", line_number, line).inspect_err(|err| { + eprintln!("{} line write error {:?}", TEST_INPUT_FILE_NAME, err); + })?; - line_number += 1; + lines_processed = line_number; } - println!("{} : lines processed", line_number - 1); + println!("{} : lines processed", lines_processed); file_cleanup() } diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index c6a05d4e..67557696 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -37,10 +37,9 @@ fn main() -> Result<(), std::io::Error> { fn create_required_files() -> Result<(), std::io::Error> { let file_path = sample_file_path(); - let dir_path = match file_path.parent() { - Some(parent) => parent, - None => return Err(Error::other("Could not get parent path")), - }; + let dir_path = file_path + .parent() + .ok_or_else(|| Error::other("Could not get parent path"))?; if !dir_path.exists() { fs::create_dir_all(dir_path).inspect_err(|x| {