ju_tcs_rust_23_23/
lib.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
use std::{path::Path, fs::File};
use std::io::{self,BufRead};

pub fn head(path: &Path, n: usize) -> Vec<String> {
    let file = File::open(path);
    let lines = io::BufReader::new(file.unwrap()).lines();
    
    let mut res: Vec<String> = vec![];
    let mut i = n;
    for line in lines {
        if i == 0 { break; }
        
        res.push(line.unwrap());
        i -= 1;
    }

    res
}

pub fn tail(path: &Path, n: usize) -> Vec<String> {
    let file = File::open(path);
    let mut lines: Vec<Result<String,std::io::Error>> = io::BufReader::new(file.unwrap()).lines().collect();
    lines.reverse();

    let mut res: Vec<String> = vec![];
    let mut i = n;
    for line in lines {
        if i == 0 { break; }
        
        res.push( line.unwrap() );

        i-=1;
    }

    res.reverse();
    res
}


#[cfg(test)]
mod tests {
    #[test]
    fn test_math() {
        assert_eq!(1 + 2, 3);
    }
}