Why endless iterator when using chars().next()?

⚓ Rust    📅 2026-03-18    👤 surdeus    👁️ 1      

surdeus

Why does this produce an endless iterator over just the first char in the str?

fn main() {
    let name = "string".to_string();
    let n = name.as_str();
    while let Some(item) = n.chars().next() {
        println!("{}", item)
    }
}

(Playground)

I would have expected next() to actually move onto the next char, but it never does.

replacing with a simple 'for c in n.chars()" solves the issue

4 posts - 3 participants

Read full topic

🏷️ Rust_feed