1use std::{path::Path, fs::File};
2use std::io::{self,BufRead};
3
4pub fn head(path: &Path, n: usize) -> Vec<String> {
5 let file = File::open(path);
6 let lines = io::BufReader::new(file.unwrap()).lines();
7
8 let mut res: Vec<String> = vec![];
9 let mut i = n;
10 for line in lines {
11 if i == 0 { break; }
12
13 res.push(line.unwrap());
14 i -= 1;
15 }
16
17 res
18}
19
20pub fn tail(path: &Path, n: usize) -> Vec<String> {
21 let file = File::open(path);
22 let mut lines: Vec<Result<String,std::io::Error>> = io::BufReader::new(file.unwrap()).lines().collect();
23 lines.reverse();
24
25 let mut res: Vec<String> = vec![];
26 let mut i = n;
27 for line in lines {
28 if i == 0 { break; }
29
30 res.push( line.unwrap() );
31
32 i-=1;
33 }
34
35 res.reverse();
36 res
37}
38
39
40#[cfg(test)]
41mod tests {
42 #[test]
43 fn test_math() {
44 assert_eq!(1 + 2, 3);
45 }
46}