I understand that a &str is a slice reference of type &[u8].
If that is the case then why I am not able to iterate it, like this:
let noodles = "noodles".to_string();
let oodles = &noodles[1..];
for elem in oodles {
println!("{}", elem)
}
I get error:
`&str` is not an iterator; try calling `.chars()` or `.bytes()`
Same code works fine for other types of slice, like a slice reference &[i32]
let myArr = [1,2,3,4];
let mySlice: &[i32] = &myArr;
for elem in mySlice {
println!("{}", elem);
}
&stris not&[u8], it is&str. It is indeed a byte slice under the hood and you can get a reference to that slice with.as_bytes()—but that's rarely what you actually want, as characters can be multibyte.