The main issue here is that identifiers cannot start with numbers. So let 1word is not accepted.
Your usage of first_word is correct and you are effectively storing the result in the variable 1word (if that would have compiled).
You have then to implement the second_word. But regarding the Book section you are in, you probably want to print using Debug trait as second_word will return you a tuple and you won't be able to just print it using {} as tuple is not implementing std::fmt::Display.
You should use {:?} in your print then.
Here is how you could rewrite this:
fn main() {
let s: String = String::from("Hello, word!");
let word1 = first_word(&s);
let word2 = second_word(&s);
println! ("1 {} 2 {:?}", word1, word2);
}
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()
}
fn second_word(s: &String) -> (usize, usize) {
// TODO
(0, 0)
}
But, I wouldn't spend much time implementing second_word. The purpose of this section of the book is to introduce &str and slices. This exemple is more to realise that using index with usize tends to become unsafe as you reference something with unrelated data and you have the chance that the String is changed by the time you want to access the string at this index, and it may happen that this index is outside the string then.
I like to solve different interesting problems, such as understanding why the program does not work and how to make it work. In this case, the function is not quite like in the Book, but just must return the index of the last symbol in the 2nd word before the space, believing that there is only one space between the words, or return index of the end.