ju_tcs_rust_23_20/
lib.rs

1use std::fs::File;
2use std::io::{self, BufRead};
3use std::path::Path;
4
5#[cfg(test)]
6mod tests;
7
8pub fn head(path: &Path, count: usize) -> io::Result<Vec<String>> {
9    let lines = read_lines(path)?;
10    lines
11        .into_iter()
12        .take(count)
13        .collect::<Vec<_>>()
14        .into_iter()
15        .collect()
16}
17
18pub fn tail(path: &Path, count: usize) -> io::Result<Vec<String>> {
19    let lines = read_lines(path)?;
20    Ok(lines
21        .into_iter()
22        .collect::<Vec<_>>()
23        .into_iter()
24        .collect::<io::Result<Vec<_>>>()?
25        .into_iter()
26        .rev()
27        .take(count)
28        .rev()
29        .collect())
30}
31
32fn read_lines(filename: impl AsRef<Path>) -> io::Result<io::Lines<io::BufReader<File>>> {
33    let file = File::open(filename)?;
34    Ok(io::BufReader::new(file).lines())
35}