diff --git a/exercises/24_fileIO/README.md b/exercises/24_fileIO/README.md new file mode 100644 index 00000000..e69de29b diff --git a/exercises/24_fileIO/fileIO1.rs b/exercises/24_fileIO/fileIO1.rs new file mode 100644 index 00000000..645cde70 --- /dev/null +++ b/exercises/24_fileIO/fileIO1.rs @@ -0,0 +1,57 @@ +use std::fs::File; +use std::io::Read; + +fn my_file_reader() -> String { + + // TODO: Open the file in read-only mode + let mut file = File::???("ferris.txt").unwrap(); + let mut content = String::new(); + + file.read_to_string(&mut content).unwrap(); + + content +} + + +fn main() {} + +#[cfg(test)] +mod test { + + use std::io::prelude::*; + use std::fs::File; + use my_file_reader; + + fn ensure_file_exists() { + let path = "ferris.txt"; + let msg = "hello from rustling!"; + + let mut file = File::create(path).unwrap(); + file.write_all(msg.as_bytes()).unwrap(); + } + + #[test] + fn can_you_read_in_rust() { + + ensure_file_exists(); + + let file_contents = my_file_reader(); + + assert_eq!(file_contents.len(), 20); + } + + #[test] + fn can_you_write_in_rust() { + let msg = "hello world!"; + + // here the user will enter the path of the file + let path = "ferris.txt"; + + // the user will fill the create method + let mut file = File::create(path).unwrap(); + + // the unwrap will be missing here + file.write_all(msg.as_bytes()).unwrap(); + } + +} \ No newline at end of file diff --git a/exercises/24_fileIO/fileIO2.rs b/exercises/24_fileIO/fileIO2.rs new file mode 100644 index 00000000..59c997b5 --- /dev/null +++ b/exercises/24_fileIO/fileIO2.rs @@ -0,0 +1,34 @@ +use std::fs::File; +use std::io::Write; + +fn my_file_writer() { + let path = "ferris.txt"; + let msg = "hello from rustling!"; + + // TODO: Open the file in write-only mode creating it if it doesn't exist + let mut file = File::???(path).unwrap(); + file.write_all(msg.as_bytes()).unwrap(); +} + + +fn main() {} + +#[cfg(test)] +mod test { + + use std::io::prelude::*; + use std::fs::File; + use my_file_writer; + + #[test] + fn can_you_write_in_rust() { + my_file_writer(); + + let mut file = File::open("ferris.txt").unwrap(); + let mut content = String::new(); + file.read_to_string(&mut content).unwrap(); + + assert_eq!(content, "hello from rustling!"); + } + +} \ No newline at end of file diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index e7055981..58f55ffd 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -1209,3 +1209,17 @@ name = "as_ref_mut" dir = "23_conversions" hint = """ Add `AsRef` or `AsMut` as a trait bound to the functions.""" + +[[exercises]] +name = "fileIO1" +dir = "24_fileIO" +hint = """ +The `read` function should return a `Result` with the `String` as the success +""" + +[[exercises]] +name = "fileIO2" +dir = "24_fileIO" +hint = """ +The `create` function will create a file with the given name +""" diff --git a/solutions/24_fileIO/fileIO1.rs b/solutions/24_fileIO/fileIO1.rs new file mode 100644 index 00000000..211b2135 --- /dev/null +++ b/solutions/24_fileIO/fileIO1.rs @@ -0,0 +1,56 @@ +use std::fs::File; +use std::io::Read; + +fn my_file_reader() -> String { + + let mut file = File::open("ferris.txt").unwrap(); + let mut content = String::new(); + + file.read_to_string(&mut content).unwrap(); + + content +} + + +fn main() {} + +#[cfg(test)] +mod test { + + use std::io::prelude::*; + use std::fs::File; + use my_file_reader; + + fn ensure_file_exists() { + let path = "ferris.txt"; + let msg = "hello from rustling!"; + + let mut file = File::create(path).unwrap(); + file.write_all(msg.as_bytes()).unwrap(); + } + + #[test] + fn can_you_read_in_rust() { + + ensure_file_exists(); + + let file_contents = my_file_reader(); + + assert_eq!(file_contents.len(), 20); + } + + #[test] + fn can_you_write_in_rust() { + let msg = "hello world!"; + + // here the user will enter the path of the file + let path = "ferris.txt"; + + // the user will fill the create method + let mut file = File::create(path).unwrap(); + + // the unwrap will be missing here + file.write_all(msg.as_bytes()).unwrap(); + } + +} \ No newline at end of file diff --git a/solutions/24_fileIO/fileIO2.rs b/solutions/24_fileIO/fileIO2.rs new file mode 100644 index 00000000..b8d4a832 --- /dev/null +++ b/solutions/24_fileIO/fileIO2.rs @@ -0,0 +1,33 @@ +use std::fs::File; +use std::io::Write; + +fn my_file_writer() { + let path = "ferris.txt"; + let msg = "hello from rustling!"; + + let mut file = File::create(path).unwrap(); + file.write_all(msg.as_bytes()).unwrap(); +} + + +fn main() {} + +#[cfg(test)] +mod test { + + use std::io::prelude::*; + use std::fs::File; + use my_file_writer; + + #[test] + fn can_you_write_in_rust() { + my_file_writer(); + + let mut file = File::open("ferris.txt").unwrap(); + let mut content = String::new(); + file.read_to_string(&mut content).unwrap(); + + assert_eq!(content, "hello from rustling!"); + } + +} \ No newline at end of file