From 4e9b5735ada47cc391b22a52ae12f6eb39c36fcc Mon Sep 17 00:00:00 2001 From: Kivanc Gunalp Date: Sun, 26 Jul 2026 15:48:35 +0000 Subject: [PATCH] Use more idiomatic Rust in file_io2/file_io3 - file_io2: replace the manual line_number counter with .lines().enumerate(), and replace buffered_file_writer.write(format!(...).as_bytes()) with writeln!, which both formats and guarantees a complete write (write_fmt loops internally, unlike a single Write::write call). - file_io3: replace the manual match on file_path.parent() with .ok_or_else(...)? for a more idiomatic Option-to-Result conversion. Verified locally: both solutions compile under edition 2024, clippy is silent, and running them still produces identical output (3 lines processed / 117-byte file, non-readonly permissions) with no leftover files. --- exercises/24_file_io/file_io2.rs | 17 ++++++++--------- exercises/24_file_io/file_io3.rs | 7 +++---- solutions/24_file_io/file_io2.rs | 17 ++++++++--------- solutions/24_file_io/file_io3.rs | 7 +++---- 4 files changed, 22 insertions(+), 26 deletions(-) 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| {