mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 06:49:19 +00:00
57 lines
1.1 KiB
Rust
57 lines
1.1 KiB
Rust
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();
|
|
}
|
|
|
|
} |