1. 23
    Rustlings strings2: String structs can be turned into &str slices via referencing
    52s

Rustlings strings2: String structs can be turned into &str slices via referencing

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

README for this exercise.

Chris Biscardi: [0:00] 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.

[0:13] 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.

[0:32] 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.

[0:44] 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.