diff --git a/exercises/24_file_io/file_io1.rs b/exercises/24_file_io/file_io1.rs index 7b0895e0..a6142dbc 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 f73b2a84..e08dfdcb 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 c4ed30ba..d5b27783 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 39e5aa6d..1ddb3153 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 +}