ju_tcs_rust_23_29/
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
use std::fs::read_to_string;
use std::path::Path;

pub fn head(path: &Path, n: usize) -> Vec<String> {
    let mut lines: Vec<String> = Vec::new();
    lines = read_file(path);
    let size_file = lines.len();
    if size_file >= n {
        lines.drain(n.. size_file);
    } else {
        panic!("too few lines");
    }
    lines
}

pub fn tail(path: &Path, n: usize) -> Vec<String> {
    let mut lines: Vec<String> = Vec::new();
    lines = read_file(path);
    let size_file = lines.len();
    if size_file >= n {
        lines.drain(0.. size_file - n);
    } else {
        panic!("too few lines");
    }
    lines
}

fn read_file(path: &Path) -> Vec<String>{
    let mut lines: Vec<String> = Vec::new();
    for line in read_to_string(path).unwrap().lines(){
        lines.push(line.to_string());
    }
    lines
}