book
book copied to clipboard
Alert about non-ASCII characters should be further up
trafficstars
URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch04-03-slices.html#the-slice#type
Description of the problem:
The function below is confusing:
fn first_word(s: &String) -> usize {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return i;
}
}
s.len()
}
~~Because s.len() isn't semantically the same than a byte index.~~
Is only for ASCII characters. That is only said many paragraphs afterwards.
Suggested fix:
Move this further up:
For the purposes of introducing string slices, we are assuming ASCII only ...