Exercise 23 / 24: Log Tail
// Iterators of Code 2026 — Day 23: Log Tail
// Challenge: Replace the backwards indexing loop with an iterator pipeline.
// Input lines are oldest-first. Return at most the last three, newest-first,
// without changing the input. Empty lines still count as lines.
fn recent_logs<'a>(lines: &[&'a str]) -> Vec<&'a str> {
let mut recent = Vec::new();
let mut index = lines.len();
while index > 0 && recent.len() < 3 {
index -= 1;
recent.push(lines[index]);
}
recent
}
fn main() {
let lines = ["boot", "connected", "retry", "recovered", "ready"];
assert_eq!(recent_logs(&lines), vec!["ready", "recovered", "retry"]);
assert_eq!(lines[0], "boot");
assert_eq!(recent_logs(&[]), Vec::<&str>::new());
assert_eq!(recent_logs(&["boot"]), vec!["boot"]);
assert_eq!(recent_logs(&["boot", "ready"]), vec!["ready", "boot"]);
assert_eq!(
recent_logs(&["boot", "connected", "ready"]),
vec!["ready", "connected", "boot"]
);
assert_eq!(
recent_logs(&["retry", "", "retry"]),
vec!["retry", "", "retry"]
);
}
Your solution
Standalone .rs / std only. Stored, not executed.
Editing access uses this browser's cookie. Clearing cookies loses access.