1 00:00:06,010 --> 00:00:10,910 Strings are very similar to vectors since they are stored as a vector of bytes. 2 00:00:10,930 --> 00:00:17,980 The difference is that strings are guaranteed to always be a valid UTF eight sequence. 3 00:00:18,250 --> 00:00:24,130 A string will be allocated on the heap is global and is not null terminated. 4 00:00:24,460 --> 00:00:27,920 There are a couple of different ways you can allocate a string. 5 00:00:27,940 --> 00:00:40,360 The first one we will create a variable called name and say string from and then we will provide the 6 00:00:40,360 --> 00:00:40,960 string. 7 00:00:41,200 --> 00:00:47,800 And then if you just hover over string, it will tell you it is a UTF eight encoded global string. 8 00:00:49,210 --> 00:00:51,820 Another way that we can create. 9 00:00:52,630 --> 00:01:01,180 A string is say let course equals thrust 10 00:01:04,810 --> 00:01:07,490 to string. 11 00:01:07,960 --> 00:01:09,750 So both of these are the same thing. 12 00:01:09,760 --> 00:01:11,200 They're both strings. 13 00:01:11,350 --> 00:01:12,760 They're both on the heap. 14 00:01:13,330 --> 00:01:14,260 The global. 15 00:01:14,260 --> 00:01:16,240 And they're not null terminated. 16 00:01:17,730 --> 00:01:26,250 So what we can do, much like vectors, is we can modify strings. 17 00:01:26,850 --> 00:01:37,700 So let's change my name and we'll call this variable new name and say we want to name replace. 18 00:01:37,710 --> 00:01:41,190 We want to go from Tyler. 19 00:01:43,290 --> 00:01:46,500 And then we want to change it to Thai. 20 00:01:47,910 --> 00:01:50,640 So let's just go ahead and print all these out. 21 00:01:52,900 --> 00:01:54,900 And see what we get. 22 00:01:54,910 --> 00:02:00,700 So we have name and then we will copy and paste it two more times. 23 00:02:04,150 --> 00:02:05,920 We have course. 24 00:02:06,940 --> 00:02:08,740 Can we have new name? 25 00:02:09,910 --> 00:02:12,100 So let's go ahead and build it. 26 00:02:14,470 --> 00:02:18,520 We have Tyler, Rust and Ty, which is what we were all expecting. 27 00:02:19,180 --> 00:02:26,790 So now we also have what is known as a string, slice or stir. 28 00:02:26,800 --> 00:02:36,090 So we'll put in here and str equals string, slice or stir. 29 00:02:36,100 --> 00:02:38,770 So that's the how you can pronounce that. 30 00:02:39,610 --> 00:02:48,760 And much like a slice and a vector, it is going to reference and borrow the text from the variable. 31 00:02:48,970 --> 00:02:56,170 So like other slice references, it is a fat pointer containing both the address and the actual data 32 00:02:56,170 --> 00:02:57,370 and its length. 33 00:02:58,360 --> 00:03:03,340 You cannot modify a string slice. 34 00:03:03,340 --> 00:03:06,850 So let's see how you can create a string slice. 35 00:03:06,850 --> 00:03:13,330 So we'll just call this string one and set it to Hello. 36 00:03:13,660 --> 00:03:15,580 And that's all you have to do. 37 00:03:15,610 --> 00:03:18,410 This is in string slice. 38 00:03:18,430 --> 00:03:24,490 So just to prove it to you, in case you don't believe me, we're going to do a nifty little trick on 39 00:03:24,490 --> 00:03:26,170 how to get it to point out. 40 00:03:26,380 --> 00:03:32,260 So we'll just come in here and type in a bogus method. 41 00:03:32,260 --> 00:03:35,530 So this will obviously produce an error in the compiler. 42 00:03:36,100 --> 00:03:38,050 So let's go ahead and build it. 43 00:03:38,050 --> 00:03:43,360 And as you can see, it produced the expected error and you can see that. 44 00:03:44,210 --> 00:03:47,210 Our type is a string slice. 45 00:03:48,590 --> 00:03:54,440 So you might be wondering when to use a string or a string slice. 46 00:03:54,890 --> 00:04:02,420 And for now, it will suffice to say that a string slice can refer to any slice of a string, whether 47 00:04:02,420 --> 00:04:04,820 it is a string literal or a string. 48 00:04:04,820 --> 00:04:08,720 And I will show you what a string literal is in the very next lecture. 49 00:04:10,670 --> 00:04:16,010 So what it means is that a string slice is more appropriate for function arguments when the caller should 50 00:04:16,010 --> 00:04:18,650 be allowed to pass either kind of string. 51 00:04:18,650 --> 00:04:21,110 So either a string or a string literal. 52 00:04:21,380 --> 00:04:28,520 And remember, a string slice does not allocate memory on the heat where a string does. 53 00:04:29,590 --> 00:04:34,980 But we can also flip flop between string slices and strings. 54 00:04:34,990 --> 00:04:36,790 So we have our string slice here. 55 00:04:36,790 --> 00:04:42,310 So let's create and say string one dot. 56 00:04:44,510 --> 00:04:45,370 To string. 57 00:04:45,380 --> 00:04:46,550 So now we just. 58 00:04:47,670 --> 00:04:54,720 Converted string one into a string and stored it inside of string two and now let's turn string two 59 00:04:54,720 --> 00:05:01,350 back into a string slice so we will go and str two. 60 00:05:01,470 --> 00:05:04,290 So now if we print all these out. 61 00:05:08,320 --> 00:05:12,280 Every single one of them is going to print out. 62 00:05:13,800 --> 00:05:14,370 Hello. 63 00:05:16,430 --> 00:05:23,030 And we all we did was bounce back and forth between a string slice and a regular string, which is exactly 64 00:05:23,030 --> 00:05:23,870 what happened. 65 00:05:23,870 --> 00:05:30,260 So much like Vector's strings have a ton of methods already associated with them. 66 00:05:31,190 --> 00:05:38,630 We can compare strings using the double equal signs to. 67 00:05:39,260 --> 00:05:48,140 Validate if the strings are equal to one another or exclamation point equal signs to mean does not equal. 68 00:05:49,520 --> 00:05:53,720 So we're trying to make sure two strings do not equal each other. 69 00:05:53,720 --> 00:06:02,990 So just to show what this might look like, let's go ahead and print it out as a simple sample. 70 00:06:02,990 --> 00:06:12,500 So we're going to check if one dot to lowercase equals one. 71 00:06:13,280 --> 00:06:19,340 So we're going to take all Capital One, change it to lowercase, and then compare it and see if it 72 00:06:19,340 --> 00:06:21,140 equals to all lowercase ones. 73 00:06:21,140 --> 00:06:23,330 And so we would expect this to return. 74 00:06:23,330 --> 00:06:23,900 True. 75 00:06:24,780 --> 00:06:26,790 Which is exactly what we get down here. 76 00:06:26,790 --> 00:06:28,500 So everything returns true. 77 00:06:29,550 --> 00:06:33,100 So those are the basic fundamentals of strings and string slices. 78 00:06:33,120 --> 00:06:35,580 We will continuously use these throughout the course. 79 00:06:35,580 --> 00:06:42,750 So you will be getting more than enough experience using them to gain familiarity and just get more 80 00:06:42,750 --> 00:06:45,570 comfortable with when to use what and where. 81 00:06:46,320 --> 00:06:52,710 But I do encourage you, prior to going on to string literals, play around with string some, and look 82 00:06:52,710 --> 00:06:54,840 at all the methods that are associated with them. 83 00:06:54,840 --> 00:06:59,220 And when you're comfortable with it, continue on to the next lecture of String Literals.