Hi guys,
I am reading slices section in "the book". It has an example of getting first word as following,
fn first_word(s : &String) -> usize{
let mut bytes = s.as_bytes(); // is this mutable.
bytes[1] = bytes[1] + 200;
for (i, &item) in bytes.iter().enumerate(){
if item == b' '{
return i;
}
}
s.len()
}
fn main() {
let mut s = String::from("hello world");
let word = first_word(&s);
}
In the first line of first_word I get an array of bytes as per the string characters. I see that
s.as_bytes()
returns an immutable reference array. But I am saving it in mutable variable.
So, when I try to change the array, it is not allowing.
Why is this?