ju_tcs_rust_23_22/
lib.rs

1use std::fs::{File};
2use std::io::{BufRead, BufReader};
3use std::path::Path;
4
5pub fn head(path: &Path, n: usize) -> Vec<String> {
6    let mut output_buf = Vec::new();
7    let input_file = match File::open(path) {
8        Ok(file) => file,
9        Err(err) => panic!("couldn't open the file, err: {}", err),
10    };
11    let reader = BufReader::new(input_file);
12
13    let mut line_num = 1;
14    for line in reader.lines() {
15        if line_num >= n{
16            break;
17        }
18        output_buf.push(line.unwrap());
19        line_num += 1;
20    }
21
22    output_buf
23}
24
25pub fn tail(path: &Path, n: usize) -> Vec<String> {
26    let mut output_buf = Vec::new();
27    let input_file = match File::open(path) {
28        Ok(file) => file,
29        Err(err) => panic!("couldn't open the file, err: {}", err),
30    };
31    let reader = BufReader::new(input_file);
32
33    let mut line_num = 1;
34    for line in reader.lines() {
35        if line_num >= n{
36            output_buf.push(line.unwrap());
37        }
38        line_num += 1;
39    }
40
41    output_buf
42}