Is there a good reason why String has no `into_chars`

You can write one like this:

fn into_chars(s: String) -> impl Iterator<Item = char> {
    let mut i = 0;
    std::iter::from_fn(move ||
        if i < s.len() {
            let c = s[i..].chars().next().unwrap();
            i += c.len_utf8();
            Some(c)
        } else {
            None
        }
    )
}

This doesn't suffer too much from the typical lifetime troubles because the closure can capture the String by value, and doesn't need to return references to anything.

2 Likes