Exercise 20 / 24: Backup Mirrors
// Iterators of Code 2026 — Day 20: Backup Mirrors
// Challenge: Remove the loop and the manual handling of missing mirrors.
// Consume the list, retaining every present address in order, even duplicates
// or empty strings. Borrow the original text rather than copying it.
fn backup_mirrors(mirrors: Vec<Option<&str>>) -> Vec<&str> {
let mut available = Vec::new();
for mirror in mirrors {
if let Some(address) = mirror {
available.push(address);
}
}
available
}
fn main() {
let local = String::from("https://backup.internal");
assert_eq!(
backup_mirrors(vec![
None,
Some(local.as_str()),
None,
Some("https://backup.example"),
]),
vec![local.as_str(), "https://backup.example"]
);
assert!(backup_mirrors(vec![]).is_empty());
assert!(backup_mirrors(vec![None, None]).is_empty());
assert_eq!(
backup_mirrors(vec![Some(""), Some("mirror"), Some("mirror")]),
vec!["", "mirror", "mirror"]
);
}
Your solution
Standalone .rs / std only. Stored, not executed.
Editing access uses this browser's cookie. Clearing cookies loses access.