1use std::fs::read_to_string;
2use std::path::Path;
3
4pub fn head(path: &Path, n: usize) -> Vec<String> {
5 let mut lines: Vec<String> = Vec::new();
6 lines = read_file(path);
7 let size_file = lines.len();
8 if size_file >= n {
9 lines.drain(n.. size_file);
10 } else {
11 panic!("too few lines");
12 }
13 lines
14}
15
16pub fn tail(path: &Path, n: usize) -> Vec<String> {
17 let mut lines: Vec<String> = Vec::new();
18 lines = read_file(path);
19 let size_file = lines.len();
20 if size_file >= n {
21 lines.drain(0.. size_file - n);
22 } else {
23 panic!("too few lines");
24 }
25 lines
26}
27
28fn read_file(path: &Path) -> Vec<String>{
29 let mut lines: Vec<String> = Vec::new();
30 for line in read_to_string(path).unwrap().lines(){
31 lines.push(line.to_string());
32 }
33 lines
34}