Why endless iterator when using chars().next()?
⚓ Rust 📅 2026-03-18 👤 surdeus 👁️ 1Why 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)
}
}
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
🏷️ Rust_feed