-
Notifications
You must be signed in to change notification settings - Fork 10.6k
/
Copy pathfileIO1.rs
56 lines (37 loc) · 1.08 KB
/
fileIO1.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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();
}
}