1. 24
    Rustlings test2: Determining when a value is a String struct vs a string slice
    3m 16s

Rustlings test2: Determining when a value is a String struct vs a string slice

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

Chris Biscardi: [0:00] Test2 is a test for the Strings section that we went through previously. In this file, there are two functions. One is a string_slice() function, and one is a string() function.

[0:09] The only difference between these two functions is that they take a different type argument. String() takes the String struct, and string_slice() takes what's called a &str. That is a reference to a specific point in memory.

[0:21] In our main function, we have a bunch of parentheses where we need to introduce one of these functions to the beginning of each of these calls depending on whether we think the value is a string_slice(), and thus accepts a &str as an argument or accepts the String.

[0:35] In the first case, we have a string literal. I'll use a string_slice() for that. Note that the Rust errors are not going to be super helpful until we have working code here. On line 21, we have a string_slice that we run to_string on.

[0:47] On the right, we have the string documentation in the Rust docs. I happen to know that ToString is an implementation of the ToString trait on the string struct. If we go down to the implementation and we expand it, we can see that the function to_string on string returns us a String struct in which case we put a string on the start of this function call.

[1:06] The String module includes a function called from() which is an implementation of the From trait. The From trait is used to do value-to-value conversion and consumes the input value. It has a relationship with Into which we'll talk about in a second. This will also be a string.

[1:21] The next example we have is a string slice with to_owned called on it. ToOwned is an implementation of the ToOwned trait which is a generalization of Clone. What this means is that we have the string_slice and that to_owned under the hood will do a clone of this value. This will also be a string because we'll gain ownership of it.

"[1:40] nice weather".into, there's a string_slice that we call into on. This is similar to what we did before with From from the other side. With From, we're saying that we're going to create a string from this value. With Into, we're going to take the value and say that we're going to put it into something. In this case, a string.

[1:59] Note that when using into because we're taking the value and we're putting it into another value, as opposed to saying we're creating a string from a value. If we use string_slice as opposed to string into, we'll put this value into a string_slice instead of a string.

[2:15] The format! Macro can be found on the Macro std::format docs page. The only indication of what this macro returns is contained in the first paragraph. It creates a string using interpolation of runtime expressions. This will also be a string.

[2:30] On line 26, we have a reference to a string, as we've seen on line 22. That takes a slice. This will be a string slice. On line 27, we have a string with some extra whitespace that we call trim on. If we go to the String Struct docs, and we look for trim, we can see that there's a public function trim on string that returns the string slice.

[2:54] On line 28, we have a string slice we call to_string on which gives us a string that we call replace on. If we look at the replace definition for the function on string, we can see that returns a string, so this will also be string.

[3:07] Finally, we have to do to_lowercase. Look for to_lowercase, we can see that it also returns a string. If we save, we can see that we have correctly defined all of these

egghead
egghead
~ 36 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today