Exercise 07 / 24: Read the envelope

// Iterators of Code 2026 — Day 07: Read the envelope
// Refactor headers using iterators. Collect lines before the first empty line,
// excluding the separator. Nonempty lines after the separator are body text.
// A whitespace-only line is NOT empty for this simplified message format.

fn headers<'a>(lines: &[&'a str]) -> Vec<&'a str> {
    let mut result = Vec::new();
    for &line in lines {
        if line.is_empty() {
            break;
        }
        result.push(line);
    }
    result
}

fn main() {
    assert_eq!(
        headers(&["From: Ada", "Subject: Lunch", "", "Bring a sandwich"]),
        ["From: Ada", "Subject: Lunch"]
    );
    assert_eq!(headers(&["From: Ada", " "]), ["From: Ada", " "]);
    assert!(headers(&["", "Body only"]).is_empty());
    assert!(headers(&[]).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.