ju_tcs_tbop_24_clappy/
lib.rs1use std::{fs::read_to_string, path::Path};
2
3pub fn head(path: &Path, n: usize) -> Vec<String> {
4 read_to_string(path)
5 .unwrap()
6 .lines()
7 .take(n)
8 .map(String::from)
9 .collect()
10}
11
12pub fn tail(path: &Path, n: usize) -> Vec<String> {
13 read_to_string(path)
14 .unwrap()
15 .lines()
16 .rev()
17 .take(n)
18 .map(String::from)
19 .collect::<Vec<String>>()
20 .iter()
21 .rev()
22 .map(String::from)
23 .collect()
24}
25
26#[cfg(test)]
27mod tests {
28 use std::path::Path;
29
30 #[test]
31 fn test_head() {
32 let head = crate::head(Path::new("basic.txt"), 3);
33 let mut expected = Vec::new();
34 expected.push("a");
35 expected.push("b");
36 expected.push("c");
37 assert_eq!(head, expected);
38 }
39
40 #[test]
41 fn test_tail() {
42 let tail = crate::tail(Path::new("basic.txt"), 3);
43 let mut expected = Vec::new();
44 expected.push("d");
45 expected.push("e");
46 expected.push("f");
47 assert_eq!(tail, expected);
48 }
49}