book icon indicating copy to clipboard operation
book copied to clipboard

Alert about non-ASCII characters should be further up

Open iampi31415 opened this issue 5 months ago • 6 comments

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 ...

iampi31415 avatar May 20 '25 15:05 iampi31415