From e916628d844ef2196b75ad1242bf80a918fde013 Mon Sep 17 00:00:00 2001 From: kivancgnlp Date: Thu, 11 Dec 2025 09:13:20 +0300 Subject: [PATCH 01/16] File IO Exercises added and tested --- exercises/24_file_io/README.md | 16 +++++++ .../SampleFilesFolder/MultiLineTextFile.txt | 3 ++ .../SampleFilesFolder/SampleTextFile.txt | 1 + exercises/24_file_io/file_io1.rs | 18 ++++++++ exercises/24_file_io/file_io2.rs | 43 +++++++++++++++++++ exercises/24_file_io/file_io3.rs | 30 +++++++++++++ rustlings-macros/info.toml | 23 ++++++++++ .../SampleFilesFolder/MultiLineTextFile.txt | 3 ++ .../SampleFilesFolder/SampleTextFile.txt | 1 + solutions/24_file_io/file_io1.rs | 16 +++++++ solutions/24_file_io/file_io2.rs | 42 ++++++++++++++++++ solutions/24_file_io/file_io3.rs | 29 +++++++++++++ 12 files changed, 225 insertions(+) create mode 100644 exercises/24_file_io/README.md create mode 100644 exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt create mode 100644 exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt create mode 100644 exercises/24_file_io/file_io1.rs create mode 100644 exercises/24_file_io/file_io2.rs create mode 100644 exercises/24_file_io/file_io3.rs create mode 100644 solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt create mode 100644 solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt create mode 100644 solutions/24_file_io/file_io1.rs create mode 100644 solutions/24_file_io/file_io2.rs create mode 100644 solutions/24_file_io/file_io3.rs diff --git a/exercises/24_file_io/README.md b/exercises/24_file_io/README.md new file mode 100644 index 0000000000..9277959155 --- /dev/null +++ b/exercises/24_file_io/README.md @@ -0,0 +1,16 @@ +# File IO + +Rust Provides several file I/O functions in the standard library. Buffered reads and writes provides better performance by reducing underlying reads. + +## Further information + +Here is the documentation for these functions in the standard library. + +- [ReadToString](https://doc.rust-lang.org/std/fs/fn.read_to_string.html) +- [BufReader](https://doc.rust-lang.org/std/io/struct.BufReader.html) +- [BufWriter](https://doc.rust-lang.org/std/io/struct.BufWriter.html) +- [Path](https://doc.rust-lang.org/stable/std/path/struct.Path.html) +- [PathBuf](https://doc.rust-lang.org/std/path/struct.PathBuf.html) + + + diff --git a/exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt b/exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt new file mode 100644 index 0000000000..0d63d0069c --- /dev/null +++ b/exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt @@ -0,0 +1,3 @@ +This is the first line of the text. +This is the second line. +And this is the third and the last line. \ No newline at end of file diff --git a/exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt b/exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt new file mode 100644 index 0000000000..029eee94e6 --- /dev/null +++ b/exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt @@ -0,0 +1 @@ +This is the file content. \ No newline at end of file diff --git a/exercises/24_file_io/file_io1.rs b/exercises/24_file_io/file_io1.rs new file mode 100644 index 0000000000..b022ce6514 --- /dev/null +++ b/exercises/24_file_io/file_io1.rs @@ -0,0 +1,18 @@ +use std::fs; + +fn main() { + + let read_str_result = fs::read_to_string("exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt"); + + match read_str_result { + Ok(contents) => { + // TODO : What should the read string would be ? + let expected_string = + assert_eq!(expected_string, contents); + } + Err(e) => { + eprintln!("Error reading file: {}", e); + assert!(false); + } + } +} diff --git a/exercises/24_file_io/file_io2.rs b/exercises/24_file_io/file_io2.rs new file mode 100644 index 0000000000..c594a76f01 --- /dev/null +++ b/exercises/24_file_io/file_io2.rs @@ -0,0 +1,43 @@ + +use std::fs; +use std::io::{BufRead, BufReader, BufWriter, Write}; + +fn main() { + + let input_file = fs::File::open("exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt"); + + if input_file.is_err() { + eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err()); + assert!(false); + } + + // TODO : How can we create a BufReader using input_file as parameter + let buffered_input_file = ; + + let output_file = fs::File::create("MultiLineOutputFile.txt"); + + if output_file.is_err() { + eprintln!("Output file open error : {}", output_file.as_ref().unwrap_err()); + assert!(false); + } + let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap()); + + let mut line_number = 1; + let mut lines = buffered_input_file.lines(); + while let Some(line) = lines.next() { + if let Ok(line) = line { + let write_result = buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); + if write_result.is_err() { + eprintln!("Write result error: {}", write_result.unwrap_err()); + break; + } + line_number += 1; + }else { + eprintln!("Write line error : {}", line_number); + assert!(false); + } + + } + + println!("{} : lines processed", line_number - 1); +} \ No newline at end of file diff --git a/exercises/24_file_io/file_io3.rs b/exercises/24_file_io/file_io3.rs new file mode 100644 index 0000000000..0c47aaa9e6 --- /dev/null +++ b/exercises/24_file_io/file_io3.rs @@ -0,0 +1,30 @@ +use std::path::{Path, PathBuf}; + +fn main() { + + let mut path_buffer = PathBuf::new(); + + path_buffer.push("exercises"); + path_buffer.push("24_file_io"); + path_buffer.push("SampleFilesFolder"); + path_buffer.push("MultiLineTextFile.txt"); + + // TODO : How we can get the metadata from path_buffer ? + let meta_data_result = path_buffer. + + if let Ok(meta_data) = meta_data_result { + println!("Metadata about the file : {:?}", path_buffer); + println!("File creation time {:?}", meta_data.created().unwrap()); + println!("File size {}", meta_data.len()); + assert_eq!(meta_data.len(), 101); + println!("File permissions {:?}", meta_data.permissions()); + assert_eq!(meta_data.permissions().readonly(), false); + }else { + println!("Could not get metadata"); + assert!(false); + } + + + + +} \ No newline at end of file diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index ca3ecf1f03..a6ddd1b9ed 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -1200,3 +1200,26 @@ name = "as_ref_mut" dir = "23_conversions" hint = """ Add `AsRef` or `AsMut` as a trait bound to the functions.""" + +# File IO Exercises + +[[exercises]] +name = "file_io1" +dir = "24_file_io" +hint = """ +Basic File Reading +""" + +[[exercises]] +name = "file_io2" +dir = "24_file_io" +hint = """ +Buffered Reading & Writing +""" + +[[exercises]] +name = "file_io3" +dir = "24_file_io" +hint = """ +Path Manipulation & Metadata +""" diff --git a/solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt b/solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt new file mode 100644 index 0000000000..0d63d0069c --- /dev/null +++ b/solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt @@ -0,0 +1,3 @@ +This is the first line of the text. +This is the second line. +And this is the third and the last line. \ No newline at end of file diff --git a/solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt b/solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt new file mode 100644 index 0000000000..029eee94e6 --- /dev/null +++ b/solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt @@ -0,0 +1 @@ +This is the file content. \ No newline at end of file diff --git a/solutions/24_file_io/file_io1.rs b/solutions/24_file_io/file_io1.rs new file mode 100644 index 0000000000..f9d75d6e9e --- /dev/null +++ b/solutions/24_file_io/file_io1.rs @@ -0,0 +1,16 @@ +use std::fs; + +fn main() { + + let read_str_result = fs::read_to_string("solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt"); + + match read_str_result { + Ok(contents) => { + assert_eq!("This is the file content.", contents); + } + Err(e) => { + eprintln!("Error reading file: {}", e); + assert!(false); + } + } +} diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs new file mode 100644 index 0000000000..49518f30a1 --- /dev/null +++ b/solutions/24_file_io/file_io2.rs @@ -0,0 +1,42 @@ + +use std::fs; +use std::io::{BufRead, BufReader, BufWriter, Write}; + +fn main() { + + let input_file = fs::File::open("solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt"); + + if input_file.is_err() { + eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err()); + assert!(false); + } + + let buffered_input_file = BufReader::new(input_file.unwrap()); + + let output_file = fs::File::create("MultiLineOutputFile.txt"); + + if output_file.is_err() { + eprintln!("Output file open error : {}", output_file.as_ref().unwrap_err()); + assert!(false); + } + let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap()); + + let mut line_number = 1; + let mut lines = buffered_input_file.lines(); + while let Some(line) = lines.next() { + if let Ok(line) = line { + let write_result = buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); + if write_result.is_err() { + eprintln!("Write result error: {}", write_result.unwrap_err()); + break; + } + line_number += 1; + }else { + eprintln!("Write line error : {}", line_number); + assert!(false); + } + + } + + println!("{} : lines processed", line_number - 1); +} \ No newline at end of file diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs new file mode 100644 index 0000000000..73b5070429 --- /dev/null +++ b/solutions/24_file_io/file_io3.rs @@ -0,0 +1,29 @@ +use std::path::{Path, PathBuf}; + +fn main() { + + let mut path_buffer = PathBuf::new(); + + path_buffer.push("solutions"); + path_buffer.push("24_file_io"); + path_buffer.push("SampleFilesFolder"); + path_buffer.push("MultiLineTextFile.txt"); + + let meta_data_result = path_buffer.metadata(); + + if let Ok(meta_data) = meta_data_result { + println!("Metadata about the file : {:?}", path_buffer); + println!("File creation time {:?}", meta_data.created().unwrap()); + println!("File size {}", meta_data.len()); + assert_eq!(meta_data.len(), 101); + println!("File permissions {:?}", meta_data.permissions()); + assert_eq!(meta_data.permissions().readonly(), false); + }else { + println!("Could not get metadata"); + assert!(false); + } + + + + +} \ No newline at end of file From 3314022480ada901e5422ca73bd72658f62ca89f Mon Sep 17 00:00:00 2001 From: kivancgnlp Date: Thu, 11 Dec 2025 10:51:44 +0300 Subject: [PATCH 02/16] Test properties updated --- rustlings-macros/info.toml | 3 +++ solutions/24_file_io/file_io1.rs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index a6ddd1b9ed..b0dbda543d 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -1206,6 +1206,7 @@ Add `AsRef` or `AsMut` as a trait bound to the functions.""" [[exercises]] name = "file_io1" dir = "24_file_io" +test = false hint = """ Basic File Reading """ @@ -1213,6 +1214,7 @@ Basic File Reading [[exercises]] name = "file_io2" dir = "24_file_io" +test = false hint = """ Buffered Reading & Writing """ @@ -1220,6 +1222,7 @@ Buffered Reading & Writing [[exercises]] name = "file_io3" dir = "24_file_io" +test = false hint = """ Path Manipulation & Metadata """ diff --git a/solutions/24_file_io/file_io1.rs b/solutions/24_file_io/file_io1.rs index f9d75d6e9e..2f170e4750 100644 --- a/solutions/24_file_io/file_io1.rs +++ b/solutions/24_file_io/file_io1.rs @@ -14,3 +14,22 @@ fn main() { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_working_directory() { + let working_directory_result = std::path::Path::new(".").canonicalize(); + + match working_directory_result { + Ok(working_directory) => { + println!("The working directory is {:?}", working_directory); + } + Err(error) => { + println!("Error: {:?}", error); + } + } + } +} \ No newline at end of file From 23c482b72e5a4525fb7d7bac727d184f7ab1966c Mon Sep 17 00:00:00 2001 From: kivancgnlp Date: Thu, 11 Dec 2025 10:53:00 +0300 Subject: [PATCH 03/16] Update Cargo.toml --- dev/Cargo.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dev/Cargo.toml b/dev/Cargo.toml index 4f725b704b..dc3b67ca27 100644 --- a/dev/Cargo.toml +++ b/dev/Cargo.toml @@ -188,6 +188,12 @@ bin = [ { name = "try_from_into_sol", path = "../solutions/23_conversions/try_from_into.rs" }, { name = "as_ref_mut", path = "../exercises/23_conversions/as_ref_mut.rs" }, { name = "as_ref_mut_sol", path = "../solutions/23_conversions/as_ref_mut.rs" }, + { name = "file_io1", path = "../exercises/24_file_io/file_io1.rs" }, + { name = "file_io1_sol", path = "../solutions/24_file_io/file_io1.rs" }, + { name = "file_io2", path = "../exercises/24_file_io/file_io2.rs" }, + { name = "file_io2_sol", path = "../solutions/24_file_io/file_io2.rs" }, + { name = "file_io3", path = "../exercises/24_file_io/file_io3.rs" }, + { name = "file_io3_sol", path = "../solutions/24_file_io/file_io3.rs" }, ] [package] From 61824af087f7a8c65aba0ea2bd6683e22fec7ed7 Mon Sep 17 00:00:00 2001 From: kivancgnlp Date: Thu, 11 Dec 2025 11:20:50 +0300 Subject: [PATCH 04/16] File samples replaced by programatically generated files --- .../SampleFilesFolder/MultiLineTextFile.txt | 3 -- .../SampleFilesFolder/SampleTextFile.txt | 1 - exercises/24_file_io/file_io1.rs | 22 +++++++++++--- exercises/24_file_io/file_io2.rs | 23 ++++++++++++-- exercises/24_file_io/file_io3.rs | 29 +++++++++++++++--- .../SampleFilesFolder/MultiLineTextFile.txt | 3 -- .../SampleFilesFolder/SampleTextFile.txt | 1 - solutions/24_file_io/file_io1.rs | 30 ++++++++----------- solutions/24_file_io/file_io2.rs | 19 +++++++++++- solutions/24_file_io/file_io3.rs | 27 +++++++++++++++-- 10 files changed, 118 insertions(+), 40 deletions(-) delete mode 100644 exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt delete mode 100644 exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt delete mode 100644 solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt delete mode 100644 solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt diff --git a/exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt b/exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt deleted file mode 100644 index 0d63d0069c..0000000000 --- a/exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt +++ /dev/null @@ -1,3 +0,0 @@ -This is the first line of the text. -This is the second line. -And this is the third and the last line. \ No newline at end of file diff --git a/exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt b/exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt deleted file mode 100644 index 029eee94e6..0000000000 --- a/exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt +++ /dev/null @@ -1 +0,0 @@ -This is the file content. \ No newline at end of file diff --git a/exercises/24_file_io/file_io1.rs b/exercises/24_file_io/file_io1.rs index b022ce6514..7b0895e0a2 100644 --- a/exercises/24_file_io/file_io1.rs +++ b/exercises/24_file_io/file_io1.rs @@ -1,14 +1,17 @@ use std::fs; +use std::path::Path; +const TEST_FILE_NAME: &str = "SampleTextFile.txt"; fn main() { - let read_str_result = fs::read_to_string("exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt"); + create_required_files(); + + let read_str_result = fs::read_to_string(TEST_FILE_NAME); match read_str_result { Ok(contents) => { - // TODO : What should the read string would be ? - let expected_string = - assert_eq!(expected_string, contents); + // TODO : What would be the expected text ? + assert_eq!(, contents); } Err(e) => { eprintln!("Error reading file: {}", e); @@ -16,3 +19,14 @@ fn main() { } } } + + +fn create_required_files(){ + let file_path = Path::new(TEST_FILE_NAME); + + if file_path.exists() == false { + fs::write(&file_path, "This is the file content.").unwrap(); + println!("File created."); + } + +} \ No newline at end of file diff --git a/exercises/24_file_io/file_io2.rs b/exercises/24_file_io/file_io2.rs index c594a76f01..7f6921d128 100644 --- a/exercises/24_file_io/file_io2.rs +++ b/exercises/24_file_io/file_io2.rs @@ -1,18 +1,22 @@ use std::fs; use std::io::{BufRead, BufReader, BufWriter, Write}; +use std::path::Path; + +const TEST_FILE_NAME: &str = "MultiLineTextFile.txt"; fn main() { - let input_file = fs::File::open("exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt"); + create_required_files(); + let input_file = fs::File::open(TEST_FILE_NAME); if input_file.is_err() { eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err()); assert!(false); } - // TODO : How can we create a BufReader using input_file as parameter - let buffered_input_file = ; + // TODO : How to create a new BufReader using input file + let buffered_input_file =; let output_file = fs::File::create("MultiLineOutputFile.txt"); @@ -40,4 +44,17 @@ fn main() { } println!("{} : lines processed", line_number - 1); +} + +fn create_required_files(){ + let file_path = Path::new(TEST_FILE_NAME); + + if file_path.exists() == false { + let text = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; + fs::write(&file_path, text).unwrap(); + println!("File created."); + } + } \ No newline at end of file diff --git a/exercises/24_file_io/file_io3.rs b/exercises/24_file_io/file_io3.rs index 0c47aaa9e6..052913dc35 100644 --- a/exercises/24_file_io/file_io3.rs +++ b/exercises/24_file_io/file_io3.rs @@ -1,22 +1,22 @@ +use std::fs; use std::path::{Path, PathBuf}; fn main() { + create_required_files(); let mut path_buffer = PathBuf::new(); - path_buffer.push("exercises"); - path_buffer.push("24_file_io"); path_buffer.push("SampleFilesFolder"); path_buffer.push("MultiLineTextFile.txt"); - // TODO : How we can get the metadata from path_buffer ? + // TODO : How to get metadata using path_buffer ? let meta_data_result = path_buffer. if let Ok(meta_data) = meta_data_result { println!("Metadata about the file : {:?}", path_buffer); println!("File creation time {:?}", meta_data.created().unwrap()); println!("File size {}", meta_data.len()); - assert_eq!(meta_data.len(), 101); + assert_eq!(meta_data.len(), 117); println!("File permissions {:?}", meta_data.permissions()); assert_eq!(meta_data.permissions().readonly(), false); }else { @@ -27,4 +27,25 @@ fn main() { +} + +fn create_required_files(){ + let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); + + let dir_path = file_path.parent().unwrap(); + + if dir_path.exists() == false { + fs::create_dir(dir_path).unwrap(); + println!("Created directory {:?}", dir_path); + } + + if file_path.exists() == false { + + let text = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; + fs::write(&file_path, text).unwrap(); + println!("File created."); + } + } \ No newline at end of file diff --git a/solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt b/solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt deleted file mode 100644 index 0d63d0069c..0000000000 --- a/solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt +++ /dev/null @@ -1,3 +0,0 @@ -This is the first line of the text. -This is the second line. -And this is the third and the last line. \ No newline at end of file diff --git a/solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt b/solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt deleted file mode 100644 index 029eee94e6..0000000000 --- a/solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt +++ /dev/null @@ -1 +0,0 @@ -This is the file content. \ No newline at end of file diff --git a/solutions/24_file_io/file_io1.rs b/solutions/24_file_io/file_io1.rs index 2f170e4750..9012f54e4d 100644 --- a/solutions/24_file_io/file_io1.rs +++ b/solutions/24_file_io/file_io1.rs @@ -1,8 +1,12 @@ use std::fs; +use std::path::Path; +const TEST_FILE_NAME: &str = "SampleTextFile.txt"; fn main() { - let read_str_result = fs::read_to_string("solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt"); + create_required_files(); + + let read_str_result = fs::read_to_string(TEST_FILE_NAME); match read_str_result { Ok(contents) => { @@ -15,21 +19,13 @@ fn main() { } } -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_working_directory() { - let working_directory_result = std::path::Path::new(".").canonicalize(); - - match working_directory_result { - Ok(working_directory) => { - println!("The working directory is {:?}", working_directory); - } - Err(error) => { - println!("Error: {:?}", error); - } - } + +fn create_required_files(){ + let file_path = Path::new(TEST_FILE_NAME); + + if !file_path.exists() { + fs::write(file_path, "This is the file content.").unwrap(); + println!("File created."); } + } \ No newline at end of file diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index 49518f30a1..af7af411a7 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -1,10 +1,14 @@ use std::fs; use std::io::{BufRead, BufReader, BufWriter, Write}; +use std::path::Path; + +const TEST_FILE_NAME: &str = "MultiLineTextFile.txt"; fn main() { - let input_file = fs::File::open("solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt"); + create_required_files(); + let input_file = fs::File::open(TEST_FILE_NAME); if input_file.is_err() { eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err()); @@ -39,4 +43,17 @@ fn main() { } println!("{} : lines processed", line_number - 1); +} + +fn create_required_files(){ + let file_path = Path::new(TEST_FILE_NAME); + + if file_path.exists() == false { + let text = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; + fs::write(&file_path, text).unwrap(); + println!("File created."); + } + } \ No newline at end of file diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index 73b5070429..f4e95d1270 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -1,11 +1,11 @@ +use std::fs; use std::path::{Path, PathBuf}; fn main() { + create_required_files(); let mut path_buffer = PathBuf::new(); - path_buffer.push("solutions"); - path_buffer.push("24_file_io"); path_buffer.push("SampleFilesFolder"); path_buffer.push("MultiLineTextFile.txt"); @@ -15,7 +15,7 @@ fn main() { println!("Metadata about the file : {:?}", path_buffer); println!("File creation time {:?}", meta_data.created().unwrap()); println!("File size {}", meta_data.len()); - assert_eq!(meta_data.len(), 101); + assert_eq!(meta_data.len(), 117); println!("File permissions {:?}", meta_data.permissions()); assert_eq!(meta_data.permissions().readonly(), false); }else { @@ -26,4 +26,25 @@ fn main() { +} + +fn create_required_files(){ + let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); + + let dir_path = file_path.parent().unwrap(); + + if dir_path.exists() == false { + fs::create_dir(dir_path).unwrap(); + println!("Created directory {:?}", dir_path); + } + + if file_path.exists() == false { + + let text = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; + fs::write(&file_path, text).unwrap(); + println!("File created."); + } + } \ No newline at end of file From 2b5356aab56ab6acd8d54ee0e7c3eec828472447 Mon Sep 17 00:00:00 2001 From: Kivanc Date: Thu, 11 Dec 2025 13:54:17 +0300 Subject: [PATCH 05/16] Checker fixes --- solutions/24_file_io/file_io1.rs | 4 ++-- solutions/24_file_io/file_io2.rs | 4 ++-- solutions/24_file_io/file_io3.rs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/solutions/24_file_io/file_io1.rs b/solutions/24_file_io/file_io1.rs index 9012f54e4d..ece68a67a5 100644 --- a/solutions/24_file_io/file_io1.rs +++ b/solutions/24_file_io/file_io1.rs @@ -13,8 +13,8 @@ fn main() { assert_eq!("This is the file content.", contents); } Err(e) => { - eprintln!("Error reading file: {}", e); - assert!(false); + panic!("Error reading file."); + } } } diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index af7af411a7..ba6aff201c 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -48,11 +48,11 @@ fn main() { fn create_required_files(){ let file_path = Path::new(TEST_FILE_NAME); - if file_path.exists() == false { + if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - fs::write(&file_path, text).unwrap(); + fs::write(file_path, text).unwrap(); println!("File created."); } diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index f4e95d1270..1bc2a3e8e0 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -33,17 +33,17 @@ fn create_required_files(){ let dir_path = file_path.parent().unwrap(); - if dir_path.exists() == false { + if !dir_path.exists() { fs::create_dir(dir_path).unwrap(); println!("Created directory {:?}", dir_path); } - if file_path.exists() == false { + if !file_path.exists(){ let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - fs::write(&file_path, text).unwrap(); + fs::write(file_path, text).unwrap(); println!("File created."); } From b210fdf9eea467613958fb9921c4077de7f9e276 Mon Sep 17 00:00:00 2001 From: Kivanc Date: Thu, 11 Dec 2025 13:56:27 +0300 Subject: [PATCH 06/16] Checker fix --- solutions/24_file_io/file_io1.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/24_file_io/file_io1.rs b/solutions/24_file_io/file_io1.rs index ece68a67a5..f73b2a8408 100644 --- a/solutions/24_file_io/file_io1.rs +++ b/solutions/24_file_io/file_io1.rs @@ -12,7 +12,7 @@ fn main() { Ok(contents) => { assert_eq!("This is the file content.", contents); } - Err(e) => { + Err(_) => { panic!("Error reading file."); } From 3e52e56a2ae03a6237e36646d9ff942e8143f976 Mon Sep 17 00:00:00 2001 From: Kivanc Date: Thu, 11 Dec 2025 14:02:26 +0300 Subject: [PATCH 07/16] Checker fixes --- solutions/24_file_io/file_io2.rs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index ba6aff201c..5e41b8350d 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -1,4 +1,3 @@ - use std::fs; use std::io::{BufRead, BufReader, BufWriter, Write}; use std::path::Path; @@ -6,13 +5,11 @@ use std::path::Path; const TEST_FILE_NAME: &str = "MultiLineTextFile.txt"; fn main() { - create_required_files(); let input_file = fs::File::open(TEST_FILE_NAME); if input_file.is_err() { - eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err()); - assert!(false); + panic!("Input file open error"); } let buffered_input_file = BufReader::new(input_file.unwrap()); @@ -20,32 +17,34 @@ fn main() { let output_file = fs::File::create("MultiLineOutputFile.txt"); if output_file.is_err() { - eprintln!("Output file open error : {}", output_file.as_ref().unwrap_err()); - assert!(false); + eprintln!( + "Output file open error : {}", + output_file.as_ref().unwrap_err() + ); + panic!("Output file open error"); } let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap()); let mut line_number = 1; let mut lines = buffered_input_file.lines(); - while let Some(line) = lines.next() { + + for line in lines { if let Ok(line) = line { - let write_result = buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); + let write_result =buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); if write_result.is_err() { eprintln!("Write result error: {}", write_result.unwrap_err()); break; } line_number += 1; - }else { - eprintln!("Write line error : {}", line_number); - assert!(false); + } else { + panic!("Write line error"); } - } println!("{} : lines processed", line_number - 1); } -fn create_required_files(){ +fn create_required_files() { let file_path = Path::new(TEST_FILE_NAME); if !file_path.exists() { @@ -55,5 +54,4 @@ fn create_required_files(){ fs::write(file_path, text).unwrap(); println!("File created."); } - -} \ No newline at end of file +} From cbdc96e6cb4ab12509bdb5b878cf57963e567b48 Mon Sep 17 00:00:00 2001 From: Kivanc Date: Thu, 11 Dec 2025 14:04:16 +0300 Subject: [PATCH 08/16] Checker fixes --- solutions/24_file_io/file_io2.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index 5e41b8350d..c4ed30ba91 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -26,9 +26,8 @@ fn main() { let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap()); let mut line_number = 1; - let mut lines = buffered_input_file.lines(); - - for line in lines { + + for line in buffered_input_file.lines() { if let Ok(line) = line { let write_result =buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); if write_result.is_err() { From ce156a349c26793326e91c4b6507b769a119bdad Mon Sep 17 00:00:00 2001 From: Kivanc Date: Thu, 11 Dec 2025 14:07:37 +0300 Subject: [PATCH 09/16] Checker fixes --- solutions/24_file_io/file_io3.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index 1bc2a3e8e0..39e5aa6d51 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -1,5 +1,5 @@ use std::fs; -use std::path::{Path, PathBuf}; +use std::path::{PathBuf}; fn main() { @@ -17,10 +17,10 @@ fn main() { println!("File size {}", meta_data.len()); assert_eq!(meta_data.len(), 117); println!("File permissions {:?}", meta_data.permissions()); - assert_eq!(meta_data.permissions().readonly(), false); + assert!(!meta_data.permissions().readonly()); }else { - println!("Could not get metadata"); - assert!(false); + panic!("Could not get metadata"); + } From c7fd0e4a879f48bcdf5377104142492b4d5d3a45 Mon Sep 17 00:00:00 2001 From: Kivanc Date: Thu, 11 Dec 2025 14:11:25 +0300 Subject: [PATCH 10/16] Rustfmt --- exercises/24_file_io/file_io1.rs | 7 ++----- solutions/24_file_io/file_io1.rs | 8 ++------ solutions/24_file_io/file_io2.rs | 5 +++-- solutions/24_file_io/file_io3.rs | 20 ++++++-------------- 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/exercises/24_file_io/file_io1.rs b/exercises/24_file_io/file_io1.rs index 7b0895e0a2..a6142dbc0f 100644 --- a/exercises/24_file_io/file_io1.rs +++ b/exercises/24_file_io/file_io1.rs @@ -3,7 +3,6 @@ use std::path::Path; const TEST_FILE_NAME: &str = "SampleTextFile.txt"; fn main() { - create_required_files(); let read_str_result = fs::read_to_string(TEST_FILE_NAME); @@ -20,13 +19,11 @@ fn main() { } } - -fn create_required_files(){ +fn create_required_files() { let file_path = Path::new(TEST_FILE_NAME); if file_path.exists() == false { fs::write(&file_path, "This is the file content.").unwrap(); println!("File created."); } - -} \ No newline at end of file +} diff --git a/solutions/24_file_io/file_io1.rs b/solutions/24_file_io/file_io1.rs index f73b2a8408..e08dfdcb91 100644 --- a/solutions/24_file_io/file_io1.rs +++ b/solutions/24_file_io/file_io1.rs @@ -3,7 +3,6 @@ use std::path::Path; const TEST_FILE_NAME: &str = "SampleTextFile.txt"; fn main() { - create_required_files(); let read_str_result = fs::read_to_string(TEST_FILE_NAME); @@ -14,18 +13,15 @@ fn main() { } Err(_) => { panic!("Error reading file."); - } } } - -fn create_required_files(){ +fn create_required_files() { let file_path = Path::new(TEST_FILE_NAME); if !file_path.exists() { fs::write(file_path, "This is the file content.").unwrap(); println!("File created."); } - -} \ No newline at end of file +} diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index c4ed30ba91..d5b2778391 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -26,10 +26,11 @@ fn main() { let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap()); let mut line_number = 1; - + for line in buffered_input_file.lines() { if let Ok(line) = line { - let write_result =buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); + let write_result = + buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); if write_result.is_err() { eprintln!("Write result error: {}", write_result.unwrap_err()); break; diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index 39e5aa6d51..1ddb31530e 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -1,8 +1,7 @@ use std::fs; -use std::path::{PathBuf}; +use std::path::PathBuf; fn main() { - create_required_files(); let mut path_buffer = PathBuf::new(); @@ -18,33 +17,26 @@ fn main() { assert_eq!(meta_data.len(), 117); println!("File permissions {:?}", meta_data.permissions()); assert!(!meta_data.permissions().readonly()); - }else { + } else { panic!("Could not get metadata"); - } - - - - } -fn create_required_files(){ +fn create_required_files() { let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); let dir_path = file_path.parent().unwrap(); - if !dir_path.exists() { + if !dir_path.exists() { fs::create_dir(dir_path).unwrap(); println!("Created directory {:?}", dir_path); } - if !file_path.exists(){ - + if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; fs::write(file_path, text).unwrap(); println!("File created."); } - -} \ No newline at end of file +} From 8e7e878eab97b5da4da7198cf3693f6e612bbfe5 Mon Sep 17 00:00:00 2001 From: Kivanc Date: Thu, 11 Dec 2025 14:16:49 +0300 Subject: [PATCH 11/16] Changes in solutions are reflected to the exercises --- exercises/24_file_io/file_io1.rs | 9 ++++--- exercises/24_file_io/file_io2.rs | 40 +++++++++++++++----------------- exercises/24_file_io/file_io3.rs | 26 +++++++-------------- 3 files changed, 32 insertions(+), 43 deletions(-) diff --git a/exercises/24_file_io/file_io1.rs b/exercises/24_file_io/file_io1.rs index a6142dbc0f..830708a6ca 100644 --- a/exercises/24_file_io/file_io1.rs +++ b/exercises/24_file_io/file_io1.rs @@ -12,9 +12,8 @@ fn main() { // TODO : What would be the expected text ? assert_eq!(, contents); } - Err(e) => { - eprintln!("Error reading file: {}", e); - assert!(false); + Err(_) => { + panic!("Error reading file."); } } } @@ -22,8 +21,8 @@ fn main() { fn create_required_files() { let file_path = Path::new(TEST_FILE_NAME); - if file_path.exists() == false { - fs::write(&file_path, "This is the file content.").unwrap(); + if !file_path.exists() { + fs::write(file_path, "This is the file content.").unwrap(); println!("File created."); } } diff --git a/exercises/24_file_io/file_io2.rs b/exercises/24_file_io/file_io2.rs index 7f6921d128..495a65282b 100644 --- a/exercises/24_file_io/file_io2.rs +++ b/exercises/24_file_io/file_io2.rs @@ -1,4 +1,3 @@ - use std::fs; use std::io::{BufRead, BufReader, BufWriter, Write}; use std::path::Path; @@ -6,55 +5,54 @@ use std::path::Path; const TEST_FILE_NAME: &str = "MultiLineTextFile.txt"; fn main() { - create_required_files(); let input_file = fs::File::open(TEST_FILE_NAME); if input_file.is_err() { - eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err()); - assert!(false); + panic!("Input file open error"); } - // TODO : How to create a new BufReader using input file - let buffered_input_file =; + let buffered_input_file = BufReader::new(input_file.unwrap()); let output_file = fs::File::create("MultiLineOutputFile.txt"); if output_file.is_err() { - eprintln!("Output file open error : {}", output_file.as_ref().unwrap_err()); - assert!(false); + eprintln!( + "Output file open error : {}", + output_file.as_ref().unwrap_err() + ); + panic!("Output file open error"); } - let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap()); + // TODO : How to create a new BufReader using input file + let buffered_input_file =; let mut line_number = 1; - let mut lines = buffered_input_file.lines(); - while let Some(line) = lines.next() { + + for line in buffered_input_file.lines() { if let Ok(line) = line { - let write_result = buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); + let write_result = + buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); if write_result.is_err() { eprintln!("Write result error: {}", write_result.unwrap_err()); break; } line_number += 1; - }else { - eprintln!("Write line error : {}", line_number); - assert!(false); + } else { + panic!("Write line error"); } - } println!("{} : lines processed", line_number - 1); } -fn create_required_files(){ +fn create_required_files() { let file_path = Path::new(TEST_FILE_NAME); - if file_path.exists() == false { + if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - fs::write(&file_path, text).unwrap(); + fs::write(file_path, text).unwrap(); println!("File created."); } - -} \ No newline at end of file +} diff --git a/exercises/24_file_io/file_io3.rs b/exercises/24_file_io/file_io3.rs index 052913dc35..86c4aad47b 100644 --- a/exercises/24_file_io/file_io3.rs +++ b/exercises/24_file_io/file_io3.rs @@ -1,8 +1,7 @@ use std::fs; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; fn main() { - create_required_files(); let mut path_buffer = PathBuf::new(); @@ -18,34 +17,27 @@ fn main() { println!("File size {}", meta_data.len()); assert_eq!(meta_data.len(), 117); println!("File permissions {:?}", meta_data.permissions()); - assert_eq!(meta_data.permissions().readonly(), false); - }else { - println!("Could not get metadata"); - assert!(false); + assert!(!meta_data.permissions().readonly()); + } else { + panic!("Could not get metadata"); } - - - - } -fn create_required_files(){ +fn create_required_files() { let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); let dir_path = file_path.parent().unwrap(); - if dir_path.exists() == false { + if !dir_path.exists() { fs::create_dir(dir_path).unwrap(); println!("Created directory {:?}", dir_path); } - if file_path.exists() == false { - + if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - fs::write(&file_path, text).unwrap(); + fs::write(file_path, text).unwrap(); println!("File created."); } - -} \ No newline at end of file +} From aef794d020544c3efdb08da73e2bcf37476d7ae2 Mon Sep 17 00:00:00 2001 From: kivancgnlp Date: Thu, 11 Dec 2025 18:43:52 +0300 Subject: [PATCH 12/16] Cleanup routines added for the created files during runs --- exercises/24_file_io/file_io1.rs | 31 +++++++++++++++--- exercises/24_file_io/file_io2.rs | 56 +++++++++++++++++++++++++------- exercises/24_file_io/file_io3.rs | 55 +++++++++++++++++++++++++++---- solutions/24_file_io/file_io1.rs | 31 +++++++++++++++--- solutions/24_file_io/file_io2.rs | 50 +++++++++++++++++++++++----- solutions/24_file_io/file_io3.rs | 55 +++++++++++++++++++++++++++---- 6 files changed, 239 insertions(+), 39 deletions(-) diff --git a/exercises/24_file_io/file_io1.rs b/exercises/24_file_io/file_io1.rs index 830708a6ca..bdd838e35d 100644 --- a/exercises/24_file_io/file_io1.rs +++ b/exercises/24_file_io/file_io1.rs @@ -12,17 +12,40 @@ fn main() { // TODO : What would be the expected text ? assert_eq!(, contents); } - Err(_) => { - panic!("Error reading file."); + Err(err) => { + eprintln!("File read error. {}", err); } } + + file_cleanup(); } fn create_required_files() { let file_path = Path::new(TEST_FILE_NAME); if !file_path.exists() { - fs::write(file_path, "This is the file content.").unwrap(); - println!("File created."); + let file_write_result = fs::write(file_path, "This is the file content."); + if file_write_result.is_ok() { + println!("Successfully wrote to file."); + } else { + panic!("Error writing to file."); + } + } else { + println!("File already exist."); + } +} + +fn file_cleanup() { + let file_path = Path::new(TEST_FILE_NAME); + + if file_path.exists() { + let remove_status = fs::remove_file(file_path); + if remove_status.is_ok() { + println!("Successfully removed file."); + } else { + panic!("Error deleting file."); + } + } else { + println!("No cleanup necassary since file not exist."); } } diff --git a/exercises/24_file_io/file_io2.rs b/exercises/24_file_io/file_io2.rs index 495a65282b..a65b1533e3 100644 --- a/exercises/24_file_io/file_io2.rs +++ b/exercises/24_file_io/file_io2.rs @@ -2,19 +2,21 @@ use std::fs; use std::io::{BufRead, BufReader, BufWriter, Write}; use std::path::Path; -const TEST_FILE_NAME: &str = "MultiLineTextFile.txt"; +const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt"; +const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; fn main() { create_required_files(); - let input_file = fs::File::open(TEST_FILE_NAME); + let input_file = fs::File::open(TEST_INPUT_FILE_NAME); if input_file.is_err() { panic!("Input file open error"); } - let buffered_input_file = BufReader::new(input_file.unwrap()); + // TODO : How to create a new BufReader using input file + let buffered_input_file =; - let output_file = fs::File::create("MultiLineOutputFile.txt"); + let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME); if output_file.is_err() { eprintln!( @@ -23,8 +25,7 @@ fn main() { ); panic!("Output file open error"); } - // TODO : How to create a new BufReader using input file - let buffered_input_file =; + let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap()); let mut line_number = 1; @@ -33,26 +34,59 @@ fn main() { let write_result = buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); if write_result.is_err() { - eprintln!("Write result error: {}", write_result.unwrap_err()); + eprintln!("Line write error: {}", write_result.unwrap_err()); break; } line_number += 1; } else { - panic!("Write line error"); + panic!("Line read error"); } } println!("{} : lines processed", line_number - 1); + file_cleanup(); } fn create_required_files() { - let file_path = Path::new(TEST_FILE_NAME); + let file_path = Path::new(TEST_INPUT_FILE_NAME); if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - fs::write(file_path, text).unwrap(); - println!("File created."); + let file_write_result = fs::write(file_path, text); + + if file_write_result.is_ok() { + println!("Multi line file created successfully!"); + } else { + eprintln!( + "Error creating file : {} , error : {:?}", + file_path.display(), + file_write_result.err() + ); + } + } +} + +fn file_cleanup() { + let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME]; + + for file_name in file_names { + let file_path = Path::new(file_name); + + if file_path.exists() { + let remove_status = fs::remove_file(file_path); + if remove_status.is_ok() { + println!("Successfully deleted file {}", file_name); + } else { + eprintln!( + "Error deleting file {}, err : {:?}", + file_name, + remove_status.err() + ); + } + } else { + println!("No cleanup necassary since {} not exist.", file_name); + } } } diff --git a/exercises/24_file_io/file_io3.rs b/exercises/24_file_io/file_io3.rs index 86c4aad47b..533ce247fe 100644 --- a/exercises/24_file_io/file_io3.rs +++ b/exercises/24_file_io/file_io3.rs @@ -13,14 +13,19 @@ fn main() { if let Ok(meta_data) = meta_data_result { println!("Metadata about the file : {:?}", path_buffer); - println!("File creation time {:?}", meta_data.created().unwrap()); + println!("File creation time {:?}", meta_data.created()); println!("File size {}", meta_data.len()); assert_eq!(meta_data.len(), 117); println!("File permissions {:?}", meta_data.permissions()); assert!(!meta_data.permissions().readonly()); } else { - panic!("Could not get metadata"); + eprintln!( + "Could not get metadata. Error: {:?}", + meta_data_result.err() + ); } + + file_cleanup(); } fn create_required_files() { @@ -29,15 +34,53 @@ fn create_required_files() { let dir_path = file_path.parent().unwrap(); if !dir_path.exists() { - fs::create_dir(dir_path).unwrap(); - println!("Created directory {:?}", dir_path); + let dir_create_result = fs::create_dir(dir_path); + if dir_create_result.is_ok() { + println!("{:?} created", dir_path); + } } if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - fs::write(file_path, text).unwrap(); - println!("File created."); + let file_write_result = fs::write(&file_path, text); + + if file_write_result.is_ok() { + println!("Multi line file created successfully!"); + } else { + eprintln!( + "Error creating file : {} , error : {:?}", + file_path.display(), + file_write_result.err() + ); + } + } +} + +fn file_cleanup() { + let mut path_buffer = PathBuf::new(); + + path_buffer.push("SampleFilesFolder"); + path_buffer.push("MultiLineTextFile.txt"); + + if path_buffer.exists() { + let remove_status = fs::remove_file(&path_buffer); + if remove_status.is_ok() { + println!("Test file deleted."); + } else { + panic!("Error deleting file."); + } + } + + path_buffer.pop(); + + if path_buffer.exists() { + let remove_status = fs::remove_dir(&path_buffer); + if remove_status.is_ok() { + println!("Test directory deleted."); + } else { + panic!("Error deleting directory."); + } } } diff --git a/solutions/24_file_io/file_io1.rs b/solutions/24_file_io/file_io1.rs index e08dfdcb91..b690b65c78 100644 --- a/solutions/24_file_io/file_io1.rs +++ b/solutions/24_file_io/file_io1.rs @@ -11,17 +11,40 @@ fn main() { Ok(contents) => { assert_eq!("This is the file content.", contents); } - Err(_) => { - panic!("Error reading file."); + Err(err) => { + eprintln!("File read error. {}", err); } } + + file_cleanup(); } fn create_required_files() { let file_path = Path::new(TEST_FILE_NAME); if !file_path.exists() { - fs::write(file_path, "This is the file content.").unwrap(); - println!("File created."); + let file_write_result = fs::write(file_path, "This is the file content."); + if file_write_result.is_ok() { + println!("Successfully wrote to file."); + } else { + panic!("Error writing to file."); + } + } else { + println!("File already exist."); + } +} + +fn file_cleanup() { + let file_path = Path::new(TEST_FILE_NAME); + + if file_path.exists() { + let remove_status = fs::remove_file(file_path); + if remove_status.is_ok() { + println!("Successfully removed file."); + } else { + panic!("Error deleting file."); + } + } else { + println!("No cleanup necassary since file not exist."); } } diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index d5b2778391..e700bcfad0 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -2,11 +2,12 @@ use std::fs; use std::io::{BufRead, BufReader, BufWriter, Write}; use std::path::Path; -const TEST_FILE_NAME: &str = "MultiLineTextFile.txt"; +const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt"; +const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; fn main() { create_required_files(); - let input_file = fs::File::open(TEST_FILE_NAME); + let input_file = fs::File::open(TEST_INPUT_FILE_NAME); if input_file.is_err() { panic!("Input file open error"); @@ -14,7 +15,7 @@ fn main() { let buffered_input_file = BufReader::new(input_file.unwrap()); - let output_file = fs::File::create("MultiLineOutputFile.txt"); + let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME); if output_file.is_err() { eprintln!( @@ -32,26 +33,59 @@ fn main() { let write_result = buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); if write_result.is_err() { - eprintln!("Write result error: {}", write_result.unwrap_err()); + eprintln!("Line write error: {}", write_result.unwrap_err()); break; } line_number += 1; } else { - panic!("Write line error"); + panic!("Line read error"); } } println!("{} : lines processed", line_number - 1); + file_cleanup(); } fn create_required_files() { - let file_path = Path::new(TEST_FILE_NAME); + let file_path = Path::new(TEST_INPUT_FILE_NAME); if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - fs::write(file_path, text).unwrap(); - println!("File created."); + let file_write_result = fs::write(file_path, text); + + if file_write_result.is_ok() { + println!("Multi line file created successfully!"); + } else { + eprintln!( + "Error creating file : {} , error : {:?}", + file_path.display(), + file_write_result.err() + ); + } + } +} + +fn file_cleanup() { + let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME]; + + for file_name in file_names { + let file_path = Path::new(file_name); + + if file_path.exists() { + let remove_status = fs::remove_file(file_path); + if remove_status.is_ok() { + println!("Successfully deleted file {}", file_name); + } else { + eprintln!( + "Error deleting file {}, err : {:?}", + file_name, + remove_status.err() + ); + } + } else { + println!("No cleanup necassary since {} not exist.", file_name); + } } } diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index 1ddb31530e..90fb47da8e 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -12,14 +12,19 @@ fn main() { if let Ok(meta_data) = meta_data_result { println!("Metadata about the file : {:?}", path_buffer); - println!("File creation time {:?}", meta_data.created().unwrap()); + println!("File creation time {:?}", meta_data.created()); println!("File size {}", meta_data.len()); assert_eq!(meta_data.len(), 117); println!("File permissions {:?}", meta_data.permissions()); assert!(!meta_data.permissions().readonly()); } else { - panic!("Could not get metadata"); + eprintln!( + "Could not get metadata. Error: {:?}", + meta_data_result.err() + ); } + + file_cleanup(); } fn create_required_files() { @@ -28,15 +33,53 @@ fn create_required_files() { let dir_path = file_path.parent().unwrap(); if !dir_path.exists() { - fs::create_dir(dir_path).unwrap(); - println!("Created directory {:?}", dir_path); + let dir_create_result = fs::create_dir(dir_path); + if dir_create_result.is_ok() { + println!("{:?} created", dir_path); + } } if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - fs::write(file_path, text).unwrap(); - println!("File created."); + let file_write_result = fs::write(&file_path, text); + + if file_write_result.is_ok() { + println!("Multi line file created successfully!"); + } else { + eprintln!( + "Error creating file : {} , error : {:?}", + file_path.display(), + file_write_result.err() + ); + } + } +} + +fn file_cleanup() { + let mut path_buffer = PathBuf::new(); + + path_buffer.push("SampleFilesFolder"); + path_buffer.push("MultiLineTextFile.txt"); + + if path_buffer.exists() { + let remove_status = fs::remove_file(&path_buffer); + if remove_status.is_ok() { + println!("Test file deleted."); + } else { + panic!("Error deleting file."); + } + } + + path_buffer.pop(); + + if path_buffer.exists() { + let remove_status = fs::remove_dir(&path_buffer); + if remove_status.is_ok() { + println!("Test directory deleted."); + } else { + panic!("Error deleting directory."); + } } } From 55c6d574f64fc3818e9ff34d22cb97bcab380a4f Mon Sep 17 00:00:00 2001 From: kivancgnlp Date: Fri, 12 Dec 2025 08:42:50 +0300 Subject: [PATCH 13/16] Error handling ways updated --- exercises/24_file_io/file_io2.rs | 47 +++++++++++++------------------- exercises/24_file_io/file_io3.rs | 37 ++++++++++++------------- solutions/24_file_io/file_io2.rs | 24 +++++++--------- solutions/24_file_io/file_io3.rs | 24 ++++++++-------- 4 files changed, 58 insertions(+), 74 deletions(-) diff --git a/exercises/24_file_io/file_io2.rs b/exercises/24_file_io/file_io2.rs index a65b1533e3..66579294db 100644 --- a/exercises/24_file_io/file_io2.rs +++ b/exercises/24_file_io/file_io2.rs @@ -7,25 +7,21 @@ const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; fn main() { create_required_files(); - let input_file = fs::File::open(TEST_INPUT_FILE_NAME); - if input_file.is_err() { - panic!("Input file open error"); - } + let input_file = match fs::File::open(TEST_INPUT_FILE_NAME) { + Ok(f) => f, + Err(e) => panic!("Input file open error : {}", e), + }; - // TODO : How to create a new BufReader using input file - let buffered_input_file =; + let buffered_input_file = BufReader::new(input_file); - let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME); + let output_file = match fs::File::create(TEST_OUTPUT_FILE_NAME){ + Ok(f) => f, + Err(e) => panic!("Output file open error : {}", e), + }; - if output_file.is_err() { - eprintln!( - "Output file open error : {}", - output_file.as_ref().unwrap_err() - ); - panic!("Output file open error"); - } - let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap()); + // TODO : How to create a new BufReader using input file + let buffered_input_file =; let mut line_number = 1; @@ -58,17 +54,14 @@ fn create_required_files() { if file_write_result.is_ok() { println!("Multi line file created successfully!"); - } else { - eprintln!( - "Error creating file : {} , error : {:?}", - file_path.display(), - file_write_result.err() - ); + }else { + eprintln!("Error creating file : {} , error : {:?}", file_path.display(), file_write_result.err()); } } } fn file_cleanup() { + let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME]; for file_name in file_names { @@ -78,15 +71,13 @@ fn file_cleanup() { let remove_status = fs::remove_file(file_path); if remove_status.is_ok() { println!("Successfully deleted file {}", file_name); - } else { - eprintln!( - "Error deleting file {}, err : {:?}", - file_name, - remove_status.err() - ); + }else { + eprintln!("Error deleting file {}, err : {:?}", file_name, remove_status.err()); } - } else { + }else { println!("No cleanup necassary since {} not exist.", file_name); } } + } + diff --git a/exercises/24_file_io/file_io3.rs b/exercises/24_file_io/file_io3.rs index 533ce247fe..94e7e5d43f 100644 --- a/exercises/24_file_io/file_io3.rs +++ b/exercises/24_file_io/file_io3.rs @@ -11,20 +11,21 @@ fn main() { // TODO : How to get metadata using path_buffer ? let meta_data_result = path_buffer. - if let Ok(meta_data) = meta_data_result { - println!("Metadata about the file : {:?}", path_buffer); - println!("File creation time {:?}", meta_data.created()); - println!("File size {}", meta_data.len()); - assert_eq!(meta_data.len(), 117); - println!("File permissions {:?}", meta_data.permissions()); - assert!(!meta_data.permissions().readonly()); - } else { - eprintln!( - "Could not get metadata. Error: {:?}", - meta_data_result.err() - ); + match meta_data_result { + Ok(meta_data) => { + println!("Metadata about the file : {:?}", path_buffer); + println!("File creation time {:?}", meta_data.created()); + println!("File size {}", meta_data.len()); + assert_eq!(meta_data.len(), 117); + println!("File permissions {:?}", meta_data.permissions()); + assert!(!meta_data.permissions().readonly()); + } + Err(error) => { + eprintln!("Could not get metadata. Error: {:?}", error); + } } + file_cleanup(); } @@ -48,12 +49,8 @@ fn create_required_files() { if file_write_result.is_ok() { println!("Multi line file created successfully!"); - } else { - eprintln!( - "Error creating file : {} , error : {:?}", - file_path.display(), - file_write_result.err() - ); + }else { + eprintln!("Error creating file : {} , error : {:?}", file_path.display(), file_write_result.err()); } } } @@ -68,7 +65,7 @@ fn file_cleanup() { let remove_status = fs::remove_file(&path_buffer); if remove_status.is_ok() { println!("Test file deleted."); - } else { + }else { panic!("Error deleting file."); } } @@ -79,7 +76,7 @@ fn file_cleanup() { let remove_status = fs::remove_dir(&path_buffer); if remove_status.is_ok() { println!("Test directory deleted."); - } else { + }else { panic!("Error deleting directory."); } } diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index e700bcfad0..b836387494 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -7,24 +7,20 @@ const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; fn main() { create_required_files(); - let input_file = fs::File::open(TEST_INPUT_FILE_NAME); - if input_file.is_err() { - panic!("Input file open error"); - } + let input_file = match fs::File::open(TEST_INPUT_FILE_NAME) { + Ok(f) => f, + Err(e) => panic!("Input file open error : {}", e), + }; - let buffered_input_file = BufReader::new(input_file.unwrap()); + let buffered_input_file = BufReader::new(input_file); - let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME); + let output_file = match fs::File::create(TEST_OUTPUT_FILE_NAME) { + Ok(f) => f, + Err(e) => panic!("Output file open error : {}", e), + }; - if output_file.is_err() { - eprintln!( - "Output file open error : {}", - output_file.as_ref().unwrap_err() - ); - panic!("Output file open error"); - } - let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap()); + let mut buffered_file_writer = BufWriter::new(output_file); let mut line_number = 1; diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index 90fb47da8e..dc1b4e9b4c 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -10,18 +10,18 @@ fn main() { let meta_data_result = path_buffer.metadata(); - if let Ok(meta_data) = meta_data_result { - println!("Metadata about the file : {:?}", path_buffer); - println!("File creation time {:?}", meta_data.created()); - println!("File size {}", meta_data.len()); - assert_eq!(meta_data.len(), 117); - println!("File permissions {:?}", meta_data.permissions()); - assert!(!meta_data.permissions().readonly()); - } else { - eprintln!( - "Could not get metadata. Error: {:?}", - meta_data_result.err() - ); + match meta_data_result { + Ok(meta_data) => { + println!("Metadata about the file : {:?}", path_buffer); + println!("File creation time {:?}", meta_data.created()); + println!("File size {}", meta_data.len()); + assert_eq!(meta_data.len(), 117); + println!("File permissions {:?}", meta_data.permissions()); + assert!(!meta_data.permissions().readonly()); + } + Err(error) => { + eprintln!("Could not get metadata. Error: {:?}", error); + } } file_cleanup(); From 4d461e03812e97cea0604eb4b750a958379d1acc Mon Sep 17 00:00:00 2001 From: Kivanc Date: Fri, 12 Dec 2025 14:38:51 +0300 Subject: [PATCH 14/16] Error handling methods improved --- exercises/24_file_io/file_io1.rs | 36 +++++++------- exercises/24_file_io/file_io2.rs | 80 ++++++++++++++------------------ exercises/24_file_io/file_io3.rs | 56 ++++++++++------------ solutions/24_file_io/file_io1.rs | 36 +++++++------- solutions/24_file_io/file_io2.rs | 79 +++++++++++++------------------ solutions/24_file_io/file_io3.rs | 64 ++++++++++++------------- 6 files changed, 160 insertions(+), 191 deletions(-) diff --git a/exercises/24_file_io/file_io1.rs b/exercises/24_file_io/file_io1.rs index bdd838e35d..54647dc461 100644 --- a/exercises/24_file_io/file_io1.rs +++ b/exercises/24_file_io/file_io1.rs @@ -2,8 +2,8 @@ use std::fs; use std::path::Path; const TEST_FILE_NAME: &str = "SampleTextFile.txt"; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; let read_str_result = fs::read_to_string(TEST_FILE_NAME); @@ -17,35 +17,35 @@ fn main() { } } - file_cleanup(); + file_cleanup()?; + Ok(()) } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_FILE_NAME); if !file_path.exists() { - let file_write_result = fs::write(file_path, "This is the file content."); - if file_write_result.is_ok() { - println!("Successfully wrote to file."); - } else { - panic!("Error writing to file."); - } + fs::write(file_path, "This is the file content.")?; } else { println!("File already exist."); } + + Ok(()) } -fn file_cleanup() { +fn file_cleanup() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_FILE_NAME); if file_path.exists() { - let remove_status = fs::remove_file(file_path); - if remove_status.is_ok() { - println!("Successfully removed file."); - } else { - panic!("Error deleting file."); - } + fs::remove_file(file_path).inspect(|_| { + println!("Test file {} deleted.", TEST_FILE_NAME); + })?; } else { - println!("No cleanup necassary since file not exist."); + println!( + "No cleanup necessary since {} not exist.", + file_path.display() + ); } + + Ok(()) } diff --git a/exercises/24_file_io/file_io2.rs b/exercises/24_file_io/file_io2.rs index 66579294db..f19d2a4784 100644 --- a/exercises/24_file_io/file_io2.rs +++ b/exercises/24_file_io/file_io2.rs @@ -5,79 +5,71 @@ use std::path::Path; const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt"; const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; - let input_file = match fs::File::open(TEST_INPUT_FILE_NAME) { - Ok(f) => f, - Err(e) => panic!("Input file open error : {}", e), - }; - - let buffered_input_file = BufReader::new(input_file); - - let output_file = match fs::File::create(TEST_OUTPUT_FILE_NAME){ - Ok(f) => f, - Err(e) => panic!("Output file open error : {}", e), - }; + let input_file = fs::File::open(TEST_INPUT_FILE_NAME).inspect_err(|err| { + eprintln!("{} file open error {:?}", TEST_INPUT_FILE_NAME, err); + })?; // TODO : How to create a new BufReader using input file let buffered_input_file =; + let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME).inspect_err(|err| { + eprintln!("{} file open error {:?}", TEST_OUTPUT_FILE_NAME, err); + })?; + + let mut buffered_file_writer = BufWriter::new(output_file); + let mut line_number = 1; for line in buffered_input_file.lines() { - if let Ok(line) = line { - let write_result = - buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); - if write_result.is_err() { - eprintln!("Line write error: {}", write_result.unwrap_err()); - break; - } - line_number += 1; - } else { - panic!("Line read error"); - } + 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); + })?; + + line_number += 1; } println!("{} : lines processed", line_number - 1); - file_cleanup(); + file_cleanup() } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_INPUT_FILE_NAME); if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - let file_write_result = fs::write(file_path, text); - - if file_write_result.is_ok() { - println!("Multi line file created successfully!"); - }else { - eprintln!("Error creating file : {} , error : {:?}", file_path.display(), file_write_result.err()); - } + fs::write(file_path, text).inspect_err(|err| { + eprintln!("Couldn't create the test file : {}", err); + })?; } -} -fn file_cleanup() { + Ok(()) +} +fn file_cleanup() -> Result<(), std::io::Error> { let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME]; for file_name in file_names { let file_path = Path::new(file_name); if file_path.exists() { - let remove_status = fs::remove_file(file_path); - if remove_status.is_ok() { - println!("Successfully deleted file {}", file_name); - }else { - eprintln!("Error deleting file {}, err : {:?}", file_name, remove_status.err()); - } - }else { - println!("No cleanup necassary since {} not exist.", file_name); + fs::remove_file(file_path).inspect(|_| { + println!("Test file {} removed", file_name); + })?; + } else { + println!("No cleanup necessary since {} not exist.", file_name); } } + Ok(()) } - diff --git a/exercises/24_file_io/file_io3.rs b/exercises/24_file_io/file_io3.rs index 94e7e5d43f..6ac3be9ba0 100644 --- a/exercises/24_file_io/file_io3.rs +++ b/exercises/24_file_io/file_io3.rs @@ -1,8 +1,8 @@ use std::fs; use std::path::PathBuf; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; let mut path_buffer = PathBuf::new(); path_buffer.push("SampleFilesFolder"); @@ -25,59 +25,53 @@ fn main() { } } - - file_cleanup(); + file_cleanup() } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); - let dir_path = file_path.parent().unwrap(); + let dir_path = match file_path.parent(){ + Some(parent) => parent, + None => return Err(std::io::Error::new(std::io::ErrorKind::Other, "Could not get parent path")), + }; if !dir_path.exists() { - let dir_create_result = fs::create_dir(dir_path); - if dir_create_result.is_ok() { - println!("{:?} created", dir_path); - } + fs::create_dir(dir_path).inspect_err(|x| { + eprintln!("Could not create directory: {:?}", x); + })?; } if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - let file_write_result = fs::write(&file_path, text); - - if file_write_result.is_ok() { - println!("Multi line file created successfully!"); - }else { - eprintln!("Error creating file : {} , error : {:?}", file_path.display(), file_write_result.err()); - } + fs::write(&file_path, text).inspect_err(|err| { + eprintln!("Couldn't create test file: {:?}", err); + })?; } + + Ok(()) } -fn file_cleanup() { +fn file_cleanup() -> Result<(), std::io::Error> { let mut path_buffer = PathBuf::new(); path_buffer.push("SampleFilesFolder"); path_buffer.push("MultiLineTextFile.txt"); if path_buffer.exists() { - let remove_status = fs::remove_file(&path_buffer); - if remove_status.is_ok() { - println!("Test file deleted."); - }else { - panic!("Error deleting file."); - } + fs::remove_file(&path_buffer).inspect(|_| { + println!("Test file removed"); + })?; } - path_buffer.pop(); if path_buffer.exists() { - let remove_status = fs::remove_dir(&path_buffer); - if remove_status.is_ok() { - println!("Test directory deleted."); - }else { - panic!("Error deleting directory."); - } + fs::remove_dir(&path_buffer).inspect(|_| { + println!("Test dir removed"); + })?; } + + Ok(()) } diff --git a/solutions/24_file_io/file_io1.rs b/solutions/24_file_io/file_io1.rs index b690b65c78..9331eeea57 100644 --- a/solutions/24_file_io/file_io1.rs +++ b/solutions/24_file_io/file_io1.rs @@ -2,8 +2,8 @@ use std::fs; use std::path::Path; const TEST_FILE_NAME: &str = "SampleTextFile.txt"; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; let read_str_result = fs::read_to_string(TEST_FILE_NAME); @@ -16,35 +16,35 @@ fn main() { } } - file_cleanup(); + file_cleanup()?; + Ok(()) } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_FILE_NAME); if !file_path.exists() { - let file_write_result = fs::write(file_path, "This is the file content."); - if file_write_result.is_ok() { - println!("Successfully wrote to file."); - } else { - panic!("Error writing to file."); - } + fs::write(file_path, "This is the file content.")?; } else { println!("File already exist."); } + + Ok(()) } -fn file_cleanup() { +fn file_cleanup() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_FILE_NAME); if file_path.exists() { - let remove_status = fs::remove_file(file_path); - if remove_status.is_ok() { - println!("Successfully removed file."); - } else { - panic!("Error deleting file."); - } + fs::remove_file(file_path).inspect(|_| { + println!("Test file {} deleted.", TEST_FILE_NAME); + })?; } else { - println!("No cleanup necassary since file not exist."); + println!( + "No cleanup necessary since {} not exist.", + file_path.display() + ); } + + Ok(()) } diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index b836387494..bfcaff1ead 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -5,83 +5,70 @@ use std::path::Path; const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt"; const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; - let input_file = match fs::File::open(TEST_INPUT_FILE_NAME) { - Ok(f) => f, - Err(e) => panic!("Input file open error : {}", e), - }; + let input_file = fs::File::open(TEST_INPUT_FILE_NAME).inspect_err(|err| { + eprintln!("{} file open error {:?}", TEST_INPUT_FILE_NAME, err); + })?; let buffered_input_file = BufReader::new(input_file); - let output_file = match fs::File::create(TEST_OUTPUT_FILE_NAME) { - Ok(f) => f, - Err(e) => panic!("Output file open error : {}", e), - }; + let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME).inspect_err(|err| { + eprintln!("{} file open error {:?}", TEST_OUTPUT_FILE_NAME, err); + })?; let mut buffered_file_writer = BufWriter::new(output_file); let mut line_number = 1; for line in buffered_input_file.lines() { - if let Ok(line) = line { - let write_result = - buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); - if write_result.is_err() { - eprintln!("Line write error: {}", write_result.unwrap_err()); - break; - } - line_number += 1; - } else { - panic!("Line read error"); - } + 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); + })?; + + line_number += 1; } println!("{} : lines processed", line_number - 1); - file_cleanup(); + file_cleanup() } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_INPUT_FILE_NAME); if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - let file_write_result = fs::write(file_path, text); - - if file_write_result.is_ok() { - println!("Multi line file created successfully!"); - } else { - eprintln!( - "Error creating file : {} , error : {:?}", - file_path.display(), - file_write_result.err() - ); - } + fs::write(file_path, text).inspect_err(|err| { + eprintln!("Couldn't create the test file : {}", err); + })?; } + + Ok(()) } -fn file_cleanup() { +fn file_cleanup() -> Result<(), std::io::Error> { let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME]; for file_name in file_names { let file_path = Path::new(file_name); if file_path.exists() { - let remove_status = fs::remove_file(file_path); - if remove_status.is_ok() { - println!("Successfully deleted file {}", file_name); - } else { - eprintln!( - "Error deleting file {}, err : {:?}", - file_name, - remove_status.err() - ); - } + fs::remove_file(file_path).inspect(|_| { + println!("Test file {} removed", file_name); + })?; } else { - println!("No cleanup necassary since {} not exist.", file_name); + println!("No cleanup necessary since {} not exist.", file_name); } } + + Ok(()) } diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index dc1b4e9b4c..ac65ca975f 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -1,8 +1,8 @@ use std::fs; use std::path::PathBuf; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; let mut path_buffer = PathBuf::new(); path_buffer.push("SampleFilesFolder"); @@ -24,62 +24,58 @@ fn main() { } } - file_cleanup(); + file_cleanup() } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); - let dir_path = file_path.parent().unwrap(); + let dir_path = match file_path.parent() { + Some(parent) => parent, + None => { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Could not get parent path", + )); + } + }; if !dir_path.exists() { - let dir_create_result = fs::create_dir(dir_path); - if dir_create_result.is_ok() { - println!("{:?} created", dir_path); - } + fs::create_dir(dir_path).inspect_err(|x| { + eprintln!("Could not create directory: {:?}", x); + })?; } if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - let file_write_result = fs::write(&file_path, text); - - if file_write_result.is_ok() { - println!("Multi line file created successfully!"); - } else { - eprintln!( - "Error creating file : {} , error : {:?}", - file_path.display(), - file_write_result.err() - ); - } + fs::write(&file_path, text).inspect_err(|err| { + eprintln!("Couldn't create test file: {:?}", err); + })?; } + + Ok(()) } -fn file_cleanup() { +fn file_cleanup() -> Result<(), std::io::Error> { let mut path_buffer = PathBuf::new(); path_buffer.push("SampleFilesFolder"); path_buffer.push("MultiLineTextFile.txt"); if path_buffer.exists() { - let remove_status = fs::remove_file(&path_buffer); - if remove_status.is_ok() { - println!("Test file deleted."); - } else { - panic!("Error deleting file."); - } + fs::remove_file(&path_buffer).inspect(|_| { + println!("Test file removed"); + })?; } - path_buffer.pop(); if path_buffer.exists() { - let remove_status = fs::remove_dir(&path_buffer); - if remove_status.is_ok() { - println!("Test directory deleted."); - } else { - panic!("Error deleting directory."); - } + fs::remove_dir(&path_buffer).inspect(|_| { + println!("Test dir removed"); + })?; } + + Ok(()) } From 2b799273006754ea89bf3fb19eabf1328bd1f8dc Mon Sep 17 00:00:00 2001 From: Kivanc Date: Fri, 12 Dec 2025 15:41:31 +0300 Subject: [PATCH 15/16] Clippy suggestions implemented --- exercises/24_file_io/file_io3.rs | 3 ++- solutions/24_file_io/file_io3.rs | 8 ++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/exercises/24_file_io/file_io3.rs b/exercises/24_file_io/file_io3.rs index 6ac3be9ba0..7966bfcca1 100644 --- a/exercises/24_file_io/file_io3.rs +++ b/exercises/24_file_io/file_io3.rs @@ -1,4 +1,5 @@ use std::fs; +use std::io::Error; use std::path::PathBuf; fn main() -> Result<(), std::io::Error> { @@ -33,7 +34,7 @@ fn create_required_files() -> Result<(), std::io::Error> { let dir_path = match file_path.parent(){ Some(parent) => parent, - None => return Err(std::io::Error::new(std::io::ErrorKind::Other, "Could not get parent path")), + None => return Err(Error::other("Could not get parent path")), }; if !dir_path.exists() { diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index ac65ca975f..47a943252d 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -1,4 +1,5 @@ use std::fs; +use std::io::Error; use std::path::PathBuf; fn main() -> Result<(), std::io::Error> { @@ -32,12 +33,7 @@ fn create_required_files() -> Result<(), std::io::Error> { let dir_path = match file_path.parent() { Some(parent) => parent, - None => { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Could not get parent path", - )); - } + None => return Err(Error::other("Could not get parent path")) }; if !dir_path.exists() { From 9c7884995225456d52a7b31f86470333c3adc053 Mon Sep 17 00:00:00 2001 From: Kivanc Date: Fri, 12 Dec 2025 15:45:09 +0300 Subject: [PATCH 16/16] Rust fmt applied --- solutions/24_file_io/file_io3.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index 47a943252d..a68dad6efb 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -33,7 +33,7 @@ fn create_required_files() -> Result<(), std::io::Error> { let dir_path = match file_path.parent() { Some(parent) => parent, - None => return Err(Error::other("Could not get parent path")) + None => return Err(Error::other("Could not get parent path")), }; if !dir_path.exists() {