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.
This commit is contained in:
Kivanc Gunalp 2026-07-26 15:48:35 +00:00
parent e0859583b3
commit 4e9b5735ad
4 changed files with 22 additions and 26 deletions

View File

@ -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()
}

View File

@ -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| {

View File

@ -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()
}

View File

@ -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| {