mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-08-14 14:26:56 +00:00
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:
parent
e0859583b3
commit
4e9b5735ad
@ -24,23 +24,22 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
|
|
||||||
let mut buffered_file_writer = BufWriter::new(output_file);
|
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| {
|
let line = line.inspect_err(|err| {
|
||||||
eprintln!("{} line parse error {:?}", TEST_INPUT_FILE_NAME, err);
|
eprintln!("{} line parse error {:?}", TEST_INPUT_FILE_NAME, err);
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
buffered_file_writer
|
let line_number = index + 1;
|
||||||
.write(format!("Line {} : {}\n", line_number, line).as_bytes())
|
writeln!(buffered_file_writer, "Line {} : {}", line_number, line).inspect_err(|err| {
|
||||||
.inspect_err(|err| {
|
|
||||||
eprintln!("{} line write error {:?}", TEST_INPUT_FILE_NAME, 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()
|
file_cleanup()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -38,10 +38,9 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
fn create_required_files() -> Result<(), std::io::Error> {
|
fn create_required_files() -> Result<(), std::io::Error> {
|
||||||
let file_path = sample_file_path();
|
let file_path = sample_file_path();
|
||||||
|
|
||||||
let dir_path = match file_path.parent() {
|
let dir_path = file_path
|
||||||
Some(parent) => parent,
|
.parent()
|
||||||
None => return Err(Error::other("Could not get parent path")),
|
.ok_or_else(|| Error::other("Could not get parent path"))?;
|
||||||
};
|
|
||||||
|
|
||||||
if !dir_path.exists() {
|
if !dir_path.exists() {
|
||||||
fs::create_dir_all(dir_path).inspect_err(|x| {
|
fs::create_dir_all(dir_path).inspect_err(|x| {
|
||||||
|
|||||||
@ -23,23 +23,22 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
|
|
||||||
let mut buffered_file_writer = BufWriter::new(output_file);
|
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| {
|
let line = line.inspect_err(|err| {
|
||||||
eprintln!("{} line parse error {:?}", TEST_INPUT_FILE_NAME, err);
|
eprintln!("{} line parse error {:?}", TEST_INPUT_FILE_NAME, err);
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
buffered_file_writer
|
let line_number = index + 1;
|
||||||
.write(format!("Line {} : {}\n", line_number, line).as_bytes())
|
writeln!(buffered_file_writer, "Line {} : {}", line_number, line).inspect_err(|err| {
|
||||||
.inspect_err(|err| {
|
|
||||||
eprintln!("{} line write error {:?}", TEST_INPUT_FILE_NAME, 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()
|
file_cleanup()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -37,10 +37,9 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
fn create_required_files() -> Result<(), std::io::Error> {
|
fn create_required_files() -> Result<(), std::io::Error> {
|
||||||
let file_path = sample_file_path();
|
let file_path = sample_file_path();
|
||||||
|
|
||||||
let dir_path = match file_path.parent() {
|
let dir_path = file_path
|
||||||
Some(parent) => parent,
|
.parent()
|
||||||
None => return Err(Error::other("Could not get parent path")),
|
.ok_or_else(|| Error::other("Could not get parent path"))?;
|
||||||
};
|
|
||||||
|
|
||||||
if !dir_path.exists() {
|
if !dir_path.exists() {
|
||||||
fs::create_dir_all(dir_path).inspect_err(|x| {
|
fs::create_dir_all(dir_path).inspect_err(|x| {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user