Exercise 13 / 24: DNS Candidates

// Iterators of Code 2026 — Day 13: DNS Candidates
// Challenge: Build a resolver candidate list: preferred servers first, then
// fallbacks. Preserve order and duplicates; this only builds a list, not queries.
// Refactor the two loops into one expression.

fn dns_candidates<'a>(preferred: &[&'a str], fallbacks: &[&'a str]) -> Vec<&'a str> {
    let mut candidates = Vec::new();

    for server in preferred {
        candidates.push(*server);
    }
    for server in fallbacks {
        candidates.push(*server);
    }

    candidates
}

fn main() {
    assert_eq!(
        dns_candidates(&["10.0.0.2", "10.0.0.3"], &["1.1.1.1", "8.8.8.8"]),
        vec!["10.0.0.2", "10.0.0.3", "1.1.1.1", "8.8.8.8"]
    );
    assert_eq!(
        dns_candidates(&["10.0.0.2", "10.0.0.2"], &["10.0.0.2"]),
        vec!["10.0.0.2", "10.0.0.2", "10.0.0.2"]
    );
    assert_eq!(dns_candidates(&[], &["1.1.1.1"]), vec!["1.1.1.1"]);
    assert_eq!(dns_candidates(&["10.0.0.2"], &[]), vec!["10.0.0.2"]);
    assert!(dns_candidates(&[], &[]).is_empty());
}

Your solution

Standalone .rs / std only. Stored, not executed.

60 characters max. Not a login.

File overrides pasted code.

Maximum solution size: 64 KiB.

Editing access uses this browser's cookie. Clearing cookies loses access.