Course lesson
Rustlings strings2: String structs can be turned into &str slices via referencing
README for this exercise.
- Duration
- 1 min
- Access
- Free
- Transcript
- Available
README for this exercise.
Course lesson
README for this exercise.
README for this exercise.
Chris Biscardi: In strings2, we once again have to make it compile without changing the function signature, specifically line 8. In our main function body, we let word = String, then we have an if statement that calls a function defined on line 16.
Is_a_color_word takes a word and uses the println! Macro to print out "That is a color word I know" or "That is not a color word I know," depending on the value. Is_a_color_word takes a reference to a string as the attempt and returns a Boolean. We use an implicit return to do a number of checks and fall back to the last value.
The error message that the Rust compiler is giving us tells us that on line 9 the parameter expected a reference to a string and found a struct string instead. It's also telling us that we could consider borrowing here.
If we follow the Rust compiler's instructions and we use a reference to the word as an argument to is_a_color_word, which is what the type signature is asking for, the test will pass.
Course lesson
README for this exercise.