Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding file IO exericses #2191

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added exercises/24_fileIO/README.md
Empty file.
57 changes: 57 additions & 0 deletions exercises/24_fileIO/fileIO1.rs
Original file line number Diff line number Diff line change
@@ -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();
}

}
34 changes: 34 additions & 0 deletions exercises/24_fileIO/fileIO2.rs
Original file line number Diff line number Diff line change
@@ -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!");
}

}
14 changes: 14 additions & 0 deletions rustlings-macros/info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1209,3 +1209,17 @@ name = "as_ref_mut"
dir = "23_conversions"
hint = """
Add `AsRef<str>` or `AsMut<u32>` 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
"""
56 changes: 56 additions & 0 deletions solutions/24_fileIO/fileIO1.rs
Original file line number Diff line number Diff line change
@@ -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();
}

}
33 changes: 33 additions & 0 deletions solutions/24_fileIO/fileIO2.rs
Original file line number Diff line number Diff line change
@@ -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!");
}

}
Loading