1 00:00:06,570 --> 00:00:10,800 - Okay, the other type of string is the String class 2 00:00:10,800 --> 00:00:12,840 and that's much more dynamic. 3 00:00:12,840 --> 00:00:16,093 This is how you create an instance of String. 4 00:00:16,093 --> 00:00:20,070 The String structure has a static method if you like. 5 00:00:20,070 --> 00:00:22,710 It's called an associated function from. 6 00:00:22,710 --> 00:00:24,930 String::from hello. 7 00:00:24,930 --> 00:00:29,590 So this here is a literal piece of text 8 00:00:31,710 --> 00:00:32,850 and what we're doing here 9 00:00:32,850 --> 00:00:34,680 is we're creating a String object, 10 00:00:34,680 --> 00:00:39,540 s is a String object, which basically owns its own memory. 11 00:00:39,540 --> 00:00:43,500 When you say String::from hello, the String object s 12 00:00:43,500 --> 00:00:46,920 will basically copy hello into here 13 00:00:46,920 --> 00:00:48,783 and it now owns that memory. 14 00:00:49,950 --> 00:00:52,110 So I'm gonna forget about the String literal now. 15 00:00:52,110 --> 00:00:54,570 S has its own copy of that text 16 00:00:54,570 --> 00:00:56,720 and that's what a String object looks like. 17 00:00:58,080 --> 00:01:00,030 If you wanted to, no particular reason, 18 00:01:00,030 --> 00:01:02,820 rather than using implicit typeing for that string, 19 00:01:02,820 --> 00:01:07,260 you can use explicit typing to say s is a String object. 20 00:01:07,260 --> 00:01:09,233 And that would give you the same effect. 21 00:01:10,410 --> 00:01:14,370 So internally, a String object holds text 22 00:01:14,370 --> 00:01:16,740 as a potentially growable vector of bytes. 23 00:01:16,740 --> 00:01:19,980 That's actually specified in the documentation. 24 00:01:19,980 --> 00:01:24,885 Internally, a string holds a vector of bytes 25 00:01:24,885 --> 00:01:27,363 to hold the text that you've assigned. 26 00:01:30,150 --> 00:01:31,980 So consider this example. 27 00:01:31,980 --> 00:01:33,783 I talk about memory allocation. 28 00:01:34,710 --> 00:01:37,110 This is how memory is allocated. 29 00:01:37,110 --> 00:01:40,890 The String variable s lives on the stack. 30 00:01:40,890 --> 00:01:44,010 So that's just a local variable on the stack. 31 00:01:44,010 --> 00:01:45,903 We have this local variable s. 32 00:01:48,780 --> 00:01:52,050 And so I'm gonna draw it as a String object. 33 00:01:52,050 --> 00:01:53,943 That's my String object s. 34 00:01:55,680 --> 00:01:59,610 The text that the String holds is allocated on the heap. 35 00:01:59,610 --> 00:02:02,970 So the text that it holds, hello, 36 00:02:02,970 --> 00:02:05,253 that text is allocated on the heap. 37 00:02:07,633 --> 00:02:08,624 Hello. 38 00:02:08,624 --> 00:02:12,453 The String object has a pointer onto the heap like so. 39 00:02:14,610 --> 00:02:17,070 So continuing our example 40 00:02:17,070 --> 00:02:20,400 where I've got my stack-based variable s, 41 00:02:20,400 --> 00:02:22,590 which has a pointer into the heap, 42 00:02:22,590 --> 00:02:26,610 and on the heap, it holds hello like so, 43 00:02:26,610 --> 00:02:30,450 let's consider what happens at the end of the function. 44 00:02:30,450 --> 00:02:31,680 At the end of the function, 45 00:02:31,680 --> 00:02:34,110 this is how memory gets deallocated. 46 00:02:34,110 --> 00:02:39,110 So first of all, my String object s is just a local variable 47 00:02:39,120 --> 00:02:42,810 and it goes out of scope at the curly bracket. 48 00:02:42,810 --> 00:02:44,070 And when it goes out of scope, 49 00:02:44,070 --> 00:02:46,090 this object is going to be destroyed 50 00:02:47,310 --> 00:02:49,140 just because that's what always happens 51 00:02:49,140 --> 00:02:52,653 when you have objects allocated locally on the stack. 52 00:02:54,840 --> 00:02:58,290 Just before the String object dies, 53 00:02:58,290 --> 00:03:01,440 it has a destructor effectively. 54 00:03:01,440 --> 00:03:04,410 There's a function we'll have look at called drop, 55 00:03:04,410 --> 00:03:06,420 which is effectively a destructor. 56 00:03:06,420 --> 00:03:09,120 The drop function is automatically called 57 00:03:09,120 --> 00:03:13,350 on an object when it goes our of scope here. 58 00:03:13,350 --> 00:03:16,320 And for the String structure, 59 00:03:16,320 --> 00:03:20,100 the drop function deallocates its buffer. 60 00:03:20,100 --> 00:03:22,170 So it'll deallocate the drop function, 61 00:03:22,170 --> 00:03:24,540 will automatically deallocate the buffer 62 00:03:24,540 --> 00:03:27,420 and then the String object goes out of scope 63 00:03:27,420 --> 00:03:29,910 and the String object itself disappears. 64 00:03:29,910 --> 00:03:30,930 So it's very similar 65 00:03:30,930 --> 00:03:34,080 to the idea of destructors in C++. 66 00:03:34,080 --> 00:03:36,240 There's no garbage collection in Rust. 67 00:03:36,240 --> 00:03:37,800 That would be far too slow. 68 00:03:37,800 --> 00:03:39,660 It does it deterministically. 69 00:03:39,660 --> 00:03:42,990 An object cleans up its own allocation 70 00:03:42,990 --> 00:03:44,703 just as it's about to be dropped. 71 00:03:45,540 --> 00:03:48,900 So like I said, there is a Drop mechanism. 72 00:03:48,900 --> 00:03:53,460 There's actually a trait on interface called Drop in Rust. 73 00:03:53,460 --> 00:03:58,460 And the trait called Drop has a function called drop, 74 00:03:59,430 --> 00:04:01,959 which is similar to a destructor. 75 00:04:01,959 --> 00:04:04,230 It so happens that this happens, 76 00:04:04,230 --> 00:04:07,440 this drop function will be called automatically 77 00:04:07,440 --> 00:04:09,000 at the end of an object's lifetime, 78 00:04:09,000 --> 00:04:12,057 very much like destructor in C++. 79 00:04:12,057 --> 00:04:13,470 And it gives the object a chance 80 00:04:13,470 --> 00:04:15,870 to deallocate its resources. 81 00:04:15,870 --> 00:04:17,100 So luckily for us, 82 00:04:17,100 --> 00:04:20,970 the String structure implements the Drop trait. 83 00:04:20,970 --> 00:04:23,970 It has a drop function automatically called 84 00:04:23,970 --> 00:04:27,120 when the String is about to be destroyed. 85 00:04:27,120 --> 00:04:31,533 The drop function deallocates the buffer on the heap. 86 00:04:33,900 --> 00:04:37,140 So I'm gonna have a look at some mutable String objects. 87 00:04:37,140 --> 00:04:38,670 Then we'll have a look at the demo. 88 00:04:38,670 --> 00:04:41,760 You can modify text in a String as long 89 00:04:41,760 --> 00:04:43,810 as you've declared the String as mutable. 90 00:04:44,730 --> 00:04:46,140 So there are various methods 91 00:04:46,140 --> 00:04:48,573 that'll actually change the value of the String. 92 00:04:49,410 --> 00:04:52,020 You can invoke methods to modify contents. 93 00:04:52,020 --> 00:04:53,940 There are also methods to query contents 94 00:04:53,940 --> 00:04:56,490 as well obviously, like .length. 95 00:04:56,490 --> 00:04:57,900 Let's have a look at the documentation 96 00:04:57,900 --> 00:05:01,560 of the String class or the String structure I should say 97 00:05:01,560 --> 00:05:02,850 to see what's available. 98 00:05:02,850 --> 00:05:04,613 And then we'll have a look at the demo. 99 00:05:05,580 --> 00:05:09,300 This is the documentation for these String structure 100 00:05:09,300 --> 00:05:13,683 in the String module, in the std crate. 101 00:05:14,580 --> 00:05:18,540 Lots of functions here, which allow you to query 102 00:05:18,540 --> 00:05:20,940 and modify the contents of a String. 103 00:05:20,940 --> 00:05:24,630 Like push_str, for example, is like an append. 104 00:05:24,630 --> 00:05:28,080 There's a push function, which appends a single character 105 00:05:28,080 --> 00:05:30,963 and a push_str, which depends a string. 106 00:05:32,133 --> 00:05:36,030 There are other methods down here which are listed 107 00:05:36,030 --> 00:05:40,710 under a different section, as_ptr, for example. 108 00:05:40,710 --> 00:05:42,690 You can get back the address 109 00:05:42,690 --> 00:05:45,093 of the text that the string points to. 110 00:05:46,290 --> 00:05:50,130 So you'll probably find yourself looking in here quite a lot 111 00:05:50,130 --> 00:05:52,410 just to see what methods you have 112 00:05:52,410 --> 00:05:55,083 and how they work and what parameters they take. 113 00:05:57,060 --> 00:05:59,850 So let's have a look at our example again to see how to work 114 00:05:59,850 --> 00:06:01,563 with the String structure. 115 00:06:02,460 --> 00:06:04,350 Okay, so I'm back in the example. 116 00:06:04,350 --> 00:06:07,530 I'm just going to reset the clock. 117 00:06:07,530 --> 00:06:11,760 Just get rid of all my little windows here. 118 00:06:11,760 --> 00:06:13,620 So we're back into a clean state. 119 00:06:13,620 --> 00:06:17,730 This is my demo_string_handling in the demo. 120 00:06:17,730 --> 00:06:21,120 My do_it function, we've looked at string_literals. 121 00:06:21,120 --> 00:06:22,200 So now we're going to look 122 00:06:22,200 --> 00:06:25,050 at string_objects and how to have mutable_string_objects. 123 00:06:27,240 --> 00:06:28,650 So here's my example here. 124 00:06:28,650 --> 00:06:33,180 I've got a string_object, s3, wales. 125 00:06:33,180 --> 00:06:34,920 I've got a string_object, s4. 126 00:06:34,920 --> 00:06:37,350 I've typed this explicitly. 127 00:06:37,350 --> 00:06:40,230 No particular benefit of doing that really 128 00:06:40,230 --> 00:06:42,270 but just for demonstration purposes. 129 00:06:42,270 --> 00:06:44,970 And that String is cymru, which is, as you know, 130 00:06:44,970 --> 00:06:47,340 is Welsh for Wales. 131 00:06:47,340 --> 00:06:49,233 I can print out a string, 132 00:06:51,150 --> 00:06:54,870 I can get the address of the first byte in the text 133 00:06:54,870 --> 00:06:56,820 and I can get the length of the string. 134 00:06:56,820 --> 00:06:58,740 And I've done that for string three 135 00:06:58,740 --> 00:07:00,990 and I've done it for string four. 136 00:07:00,990 --> 00:07:02,790 At the end of the function, 137 00:07:02,790 --> 00:07:05,643 these two string objects go out of scope. 138 00:07:06,840 --> 00:07:09,480 The drop function is called on the String objects 139 00:07:09,480 --> 00:07:13,470 because String, the String structure, 140 00:07:13,470 --> 00:07:15,660 implements the Drop trait. 141 00:07:15,660 --> 00:07:18,030 In other words, it has a drop function, 142 00:07:18,030 --> 00:07:22,320 which will dynamically effectively delete the memory 143 00:07:22,320 --> 00:07:23,153 on the heap. 144 00:07:25,140 --> 00:07:28,260 And then I've got a mutable_string example. 145 00:07:28,260 --> 00:07:31,500 I've got a mutable_string, s5, 146 00:07:31,500 --> 00:07:33,240 which has got some white space 147 00:07:33,240 --> 00:07:35,610 at the beginning deliberately, super. 148 00:07:35,610 --> 00:07:37,863 When you call the push_str function, 149 00:07:39,330 --> 00:07:41,790 the String object kind of expands 150 00:07:41,790 --> 00:07:43,950 its dynamic memory allocation. 151 00:07:43,950 --> 00:07:46,200 So it's the same String object, 152 00:07:46,200 --> 00:07:48,840 it's just that the buffer gets expanded. 153 00:07:48,840 --> 00:07:51,150 You can convert the string to_uppercase, 154 00:07:51,150 --> 00:07:55,770 trim the string to get rid of leading and trailing space 155 00:07:55,770 --> 00:07:57,780 and then convert the String to_uppercase. 156 00:07:57,780 --> 00:08:02,760 So s6 will be an uppercase version of super swans. 157 00:08:02,760 --> 00:08:03,993 Let's run it and see. 158 00:08:10,380 --> 00:08:15,380 So we have the details for s3 and s4. 159 00:08:15,870 --> 00:08:19,440 So s3 is wales, the address of the first byte 160 00:08:19,440 --> 00:08:20,940 and the number of bytes. 161 00:08:20,940 --> 00:08:23,520 Likewise for s4. 162 00:08:23,520 --> 00:08:25,620 And then we have our mutable_string. 163 00:08:25,620 --> 00:08:28,500 S5, well numbered. 164 00:08:28,500 --> 00:08:33,500 So initially, that string contains super swans. 165 00:08:34,050 --> 00:08:38,760 So s5 contains super swans with spaces. 166 00:08:38,760 --> 00:08:42,807 That's s5 and I've got the address of it there. 167 00:08:42,807 --> 00:08:45,870 S6 is the kind of converted version. 168 00:08:45,870 --> 00:08:48,990 It trimmed it and then converted it it to uppercase. 169 00:08:48,990 --> 00:08:53,670 So s6 is the trimmed uppercased version of super swans. 170 00:08:53,670 --> 00:08:54,503 Super.