[Beginner] Function without enumerate

⚓ Rust    📅 2026-06-29    👤 surdeus    👁️ 2      

surdeus

Hello, can anyone help me write this function without using enumerate / join?

fn draw(list: &[(String, bool)], height: usize, y: usize, offset: usize) {
    eprint!("\x1b[2J\x1b[H");
    for (i, (name, is_dir)) in list.iter().skip(offset).take(height).enumerate() {
        let newline = if i > 0 { "\n" } else { "" };
        let slash = if *is_dir { "/" } else { "" };
        eprint!("{newline}{slash}{name}");
    }
    eprint!("\x1b[{};1H", y - offset + 1);
}

That's the closest I got:

fn draw(list: &[(String, bool)], height: usize, y: usize, offset: usize) {
    eprint!("\x1b[2J\x1b[H");
    let mut first = true;
    for (name, is_dir) in list.iter().skip(offset).take(height) {
        let newline = if first { "" } else { "\n" };
        let slash = if *is_dir { "/" } else { "" };
        eprint!("{newline}{slash}{name}");
        first = false;
    }
    eprint!("\x1b[{};1H", y - offset + 1);
}

But using this first variable seems wrong.
Full code available here

6 posts - 5 participants

Read full topic

🏷️ Rust_feed