for loops over a list, a map, a range (0..n) or a Set. This value is none of those. A
str isn't iterated directly: say whether you want its chars(), bytes() or lines().
Wrong#
fn main() {
for c in "abc" { print(c) }
}Fixed#
fn main() {
for c in "abc".chars() { print(c) }
}Common causes#
- Looping over a string: use
.chars(),.lines()or.split(...). - Looping over a number: write
for i in 0..n. - Looping over a
T?orT!: unwrap it first with?orelse.