Lyricdev
GitHub

LYR-SEM0232

a loop over a string

error ยท spec 07-statements

A for loop walks a string, which is no Iterable: s.chars() gives its characters, s.lines() its lines.

Explanation

A string holds text, and text is walked in more than one way: by its characters, s.chars(), or by its lines, s.lines(). Neither is the string's own, so a string is no Iterable and for (c in s) is refused.

lyric fix writes .chars(), the walk most loops mean.

Wrong

fn main(): void {
    let line = "ab";
    for (c in line) {
        let _ = c;
    }
}
fn main(): void {
    let line = "ab";
    for (c in line.chars()) {
        let _ = c;
    }
}