mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-01-28 00:00:03 +03:00
adding file IO exericses
This commit is contained in:
parent
1aec7c1152
commit
b4b5fd90fd
0
exercises/24_fileIO/README.md
Normal file
0
exercises/24_fileIO/README.md
Normal file
57
exercises/24_fileIO/fileIO1.rs
Normal file
57
exercises/24_fileIO/fileIO1.rs
Normal file
|
@ -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
exercises/24_fileIO/fileIO2.rs
Normal file
34
exercises/24_fileIO/fileIO2.rs
Normal file
|
@ -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!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1209,3 +1209,17 @@ name = "as_ref_mut"
|
||||||
dir = "23_conversions"
|
dir = "23_conversions"
|
||||||
hint = """
|
hint = """
|
||||||
Add `AsRef<str>` or `AsMut<u32>` as a trait bound to the functions."""
|
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
solutions/24_fileIO/fileIO1.rs
Normal file
56
solutions/24_fileIO/fileIO1.rs
Normal file
|
@ -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
solutions/24_fileIO/fileIO2.rs
Normal file
33
solutions/24_fileIO/fileIO2.rs
Normal file
|
@ -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…
Reference in a new issue