ju_tcs_rust_23_11/
lib.rs

1use std::fs::File;
2use std::io::{self, BufRead};
3use std::path::PathBuf;
4
5pub fn head(path: PathBuf, n: usize) -> Vec<String> {
6    let file = File::open(&path).unwrap();
7
8    let mut lines: Vec<String> = io::BufReader::new(file)
9        .lines()
10        .map(|line| line.unwrap())
11        .collect();
12
13    while lines.len() > n {
14        lines.remove(lines.len() - 1);
15    }
16
17    lines
18}
19
20
21pub fn tail(path: PathBuf, n: usize) -> Vec<String> {
22    let file = File::open(&path).unwrap();
23
24    let mut lines: Vec<String> = io::BufReader::new(file)
25        .lines()
26        .map(|line| line.unwrap())
27        .collect();
28    while lines.len() > n {
29        lines.remove(0);
30    }
31    lines
32}