Strings are very similar to vectors since they are stored as a vector of bytes.
The difference is that strings are guaranteed to always be a valid UTF eight sequence.
A string will be allocated on the heap is global and is not null terminated.
There are a couple of different ways you can allocate a string.
The first one we will create a variable called name and say string from and then we will provide the
string.
And then if you just hover over string, it will tell you it is a UTF eight encoded global string.
Another way that we can create.
A string is say let course equals thrust
to string.
So both of these are the same thing.
They're both strings.
They're both on the heap.
The global.
And they're not null terminated.
So what we can do, much like vectors, is we can modify strings.
So let's change my name and we'll call this variable new name and say we want to name replace.
We want to go from Tyler.
And then we want to change it to Thai.
So let's just go ahead and print all these out.
And see what we get.
So we have name and then we will copy and paste it two more times.
We have course.
Can we have new name?
So let's go ahead and build it.
We have Tyler, Rust and Ty, which is what we were all expecting.
So now we also have what is known as a string, slice or stir.
So we'll put in here and str equals string, slice or stir.
So that's the how you can pronounce that.
And much like a slice and a vector, it is going to reference and borrow the text from the variable.
So like other slice references, it is a fat pointer containing both the address and the actual data
and its length.
You cannot modify a string slice.
So let's see how you can create a string slice.
So we'll just call this string one and set it to Hello.
And that's all you have to do.
This is in string slice.
So just to prove it to you, in case you don't believe me, we're going to do a nifty little trick on
how to get it to point out.
So we'll just come in here and type in a bogus method.
So this will obviously produce an error in the compiler.
So let's go ahead and build it.
And as you can see, it produced the expected error and you can see that.
Our type is a string slice.
So you might be wondering when to use a string or a string slice.
And for now, it will suffice to say that a string slice can refer to any slice of a string, whether
it is a string literal or a string.
And I will show you what a string literal is in the very next lecture.
So what it means is that a string slice is more appropriate for function arguments when the caller should
be allowed to pass either kind of string.
So either a string or a string literal.
And remember, a string slice does not allocate memory on the heat where a string does.
But we can also flip flop between string slices and strings.
So we have our string slice here.
So let's create and say string one dot.
To string.
So now we just.
Converted string one into a string and stored it inside of string two and now let's turn string two
back into a string slice so we will go and str two.
So now if we print all these out.
Every single one of them is going to print out.
Hello.
And we all we did was bounce back and forth between a string slice and a regular string, which is exactly
what happened.
So much like Vector's strings have a ton of methods already associated with them.
We can compare strings using the double equal signs to.
Validate if the strings are equal to one another or exclamation point equal signs to mean does not equal.
So we're trying to make sure two strings do not equal each other.
So just to show what this might look like, let's go ahead and print it out as a simple sample.
So we're going to check if one dot to lowercase equals one.
So we're going to take all Capital One, change it to lowercase, and then compare it and see if it
equals to all lowercase ones.
And so we would expect this to return.
True.
Which is exactly what we get down here.
So everything returns true.
So those are the basic fundamentals of strings and string slices.
We will continuously use these throughout the course.
So you will be getting more than enough experience using them to gain familiarity and just get more
comfortable with when to use what and where.
But I do encourage you, prior to going on to string literals, play around with string some, and look
at all the methods that are associated with them.
And when you're comfortable with it, continue on to the next lecture of String Literals.