1 00:00:06,570 --> 00:00:09,810 - In the previous section we talked about the Copy trait 2 00:00:09,810 --> 00:00:12,360 and I also explained that most types in Rust 3 00:00:12,360 --> 00:00:14,700 don't actually implement the Copy trait. 4 00:00:14,700 --> 00:00:18,360 For example, String doesn't implement Copy. 5 00:00:18,360 --> 00:00:21,390 Therefore for types that don't implement copy, 6 00:00:21,390 --> 00:00:23,700 when you assign one value to another, 7 00:00:23,700 --> 00:00:26,610 the value is moved from the first variable 8 00:00:26,610 --> 00:00:28,470 into the second variable 9 00:00:28,470 --> 00:00:31,440 and the original variable becomes invalidated 10 00:00:31,440 --> 00:00:32,490 and you can't use it 11 00:00:32,490 --> 00:00:35,400 and the compiler would complain if you try to. 12 00:00:35,400 --> 00:00:39,600 So here's an example, just to recap what we already know. 13 00:00:39,600 --> 00:00:40,500 In this example, 14 00:00:40,500 --> 00:00:43,647 S1 is a String, let me draw it here. 15 00:00:43,647 --> 00:00:45,510 S1 is a String. 16 00:00:45,510 --> 00:00:47,760 It has a pointer effectively, 17 00:00:47,760 --> 00:00:51,303 to a block of memory on the heap, hello. 18 00:00:52,770 --> 00:00:57,000 And then we assign S1 into S2. 19 00:00:57,000 --> 00:01:01,350 And because S1, sorry because String doesn't implement Copy, 20 00:01:01,350 --> 00:01:05,070 then this will move the text from S1 into S2. 21 00:01:05,070 --> 00:01:06,770 Well, actually what really happens 22 00:01:08,365 --> 00:01:09,198 when it doesn't move, 23 00:01:09,198 --> 00:01:12,630 it just basically does a bit copy of this into here. 24 00:01:12,630 --> 00:01:15,780 So basically the pointer that was in there 25 00:01:15,780 --> 00:01:17,550 gets copied into here. 26 00:01:17,550 --> 00:01:21,180 So S2 points there. 27 00:01:21,180 --> 00:01:25,920 But not only that, the compiler also then invalidates S1 28 00:01:25,920 --> 00:01:29,220 as if S1 never existed, you can't use it anymore. 29 00:01:29,220 --> 00:01:31,350 Its value has been moved. 30 00:01:31,350 --> 00:01:35,850 So if you print S2, it'll print hello and that's fine. 31 00:01:35,850 --> 00:01:38,730 But if you try to print S1, you'll get a compiler error 32 00:01:38,730 --> 00:01:41,583 because S1 has lost its value. 33 00:01:42,720 --> 00:01:46,710 Right, well what if you did want to actually copy the text 34 00:01:46,710 --> 00:01:50,640 from S1 into S2 instead of just copying the pointer? 35 00:01:50,640 --> 00:01:54,300 In situations like that, you can rely on the Clone Trait. 36 00:01:54,300 --> 00:01:56,730 This is another important trait in Rust, 37 00:01:56,730 --> 00:01:58,770 predefined in the Rust library. 38 00:01:58,770 --> 00:02:03,770 The Clone Trait has a clone function which does a deep copy. 39 00:02:03,990 --> 00:02:07,320 It returns a clone or a copy of the actual value, 40 00:02:07,320 --> 00:02:10,260 like the text, not just the pointer. 41 00:02:10,260 --> 00:02:13,230 So String implements Clone. 42 00:02:13,230 --> 00:02:15,660 So you can call the clone method on a String. 43 00:02:15,660 --> 00:02:18,300 You do actually have to call the clone method 44 00:02:18,300 --> 00:02:22,380 to perform the copy and then you'll get back 45 00:02:22,380 --> 00:02:24,813 a duplicate of the original text. 46 00:02:26,730 --> 00:02:30,410 Right, you can also use the original text as well, okay? 47 00:02:30,410 --> 00:02:32,100 So you'll end up with two Strings, 48 00:02:32,100 --> 00:02:36,120 the original plus the copy, both of which are still alive. 49 00:02:36,120 --> 00:02:38,283 The original String doesn't lose its value. 50 00:02:39,240 --> 00:02:42,420 Here's an example of Cloning a String. 51 00:02:42,420 --> 00:02:43,320 I've got S1. 52 00:02:43,320 --> 00:02:46,590 Notice that S1 is mutable for the purposes of my example. 53 00:02:46,590 --> 00:02:47,550 So let's draw that here. 54 00:02:47,550 --> 00:02:50,730 S1 is a String object. 55 00:02:50,730 --> 00:02:55,730 It has a point there to hello, like so. 56 00:02:56,400 --> 00:02:59,280 And then when I call S1.clone, 57 00:02:59,280 --> 00:03:00,960 the Clone function will basically 58 00:03:00,960 --> 00:03:02,700 give me back another String, 59 00:03:02,700 --> 00:03:05,523 and it'll kind of copy the text into here. 60 00:03:07,950 --> 00:03:12,200 Okay, so a bit like a Copy Constructor in C++ 61 00:03:13,140 --> 00:03:14,593 There we go and that's S2. 62 00:03:16,710 --> 00:03:18,690 So in my example, 63 00:03:18,690 --> 00:03:23,550 for S1, I append world, okay, very exciting. 64 00:03:23,550 --> 00:03:28,113 So hello world. S1 is now hello world. 65 00:03:29,910 --> 00:03:32,583 But S2 is still just hello. 66 00:03:33,420 --> 00:03:35,370 Yes, that's correct. 67 00:03:35,370 --> 00:03:37,320 Right, so that was quite straightforward. 68 00:03:37,320 --> 00:03:39,930 Most of the times, hopefully you don't call clone 69 00:03:39,930 --> 00:03:41,610 because it's obviously, you know, 70 00:03:41,610 --> 00:03:44,250 quite time consuming cloning the text. 71 00:03:44,250 --> 00:03:47,040 But if you need to clone it, then you can. 72 00:03:47,040 --> 00:03:50,370 Right, quick example to show how that all works in practice. 73 00:03:50,370 --> 00:03:54,060 Let's go to the demo project lesson 6, Scope Ownership. 74 00:03:54,060 --> 00:03:57,723 Have a look at demo cloning and then run it as usual. 75 00:03:58,620 --> 00:04:01,440 Okay, so first of all, in my main code 76 00:04:01,440 --> 00:04:06,270 let's un-comment the call to the demo cloning example, 77 00:04:06,270 --> 00:04:08,190 demo_cloning::do_it. 78 00:04:08,190 --> 00:04:11,040 Obviously I've imported, I've declared that module there. 79 00:04:11,880 --> 00:04:14,253 And let's dive into it here. 80 00:04:15,180 --> 00:04:18,450 Right, well, I'll run it and then we can discuss the output. 81 00:04:18,450 --> 00:04:20,160 It's basically the same as we just saw 82 00:04:20,160 --> 00:04:21,660 in the PowerPoint deck. 83 00:04:21,660 --> 00:04:22,683 So cargo, run. 84 00:04:29,550 --> 00:04:30,510 So first of all, 85 00:04:30,510 --> 00:04:35,490 I've got some integer implements copy. 86 00:04:35,490 --> 00:04:38,640 So there's no need to clone the value of a 87 00:04:38,640 --> 00:04:41,100 when you have a type which implements copy. 88 00:04:41,100 --> 00:04:44,190 When you assign, it is effectively doing a bit-wise copy. 89 00:04:44,190 --> 00:04:49,023 So a and b are both 42, which is correct. 90 00:04:49,980 --> 00:04:54,360 And then I've got a mutable String here at S1. 91 00:04:54,360 --> 00:04:55,770 I clone its content. 92 00:04:55,770 --> 00:04:58,560 So that S2 is also hello. 93 00:04:58,560 --> 00:05:02,610 And then in S1 I append. Oh yes, I remember this example. 94 00:05:02,610 --> 00:05:05,220 When I first taught Rust, it was in Norway 95 00:05:05,220 --> 00:05:08,010 in a very nice conference center 96 00:05:08,010 --> 00:05:11,340 where they served ice cream and popcorn and coffee. 97 00:05:11,340 --> 00:05:16,260 So I append S1 was originally hello, hello world. 98 00:05:16,260 --> 00:05:19,770 det er istid eller kaffetid snart 99 00:05:19,770 --> 00:05:21,477 If you speak (speaks in foreign language) 100 00:05:21,477 --> 00:05:22,627 if you speak Norwegian, 101 00:05:23,520 --> 00:05:28,290 it is ice cream time or coffee time soon. 102 00:05:28,290 --> 00:05:29,370 Okay, there we go. 103 00:05:29,370 --> 00:05:33,300 So S1 has been appended, but of course S2, 104 00:05:33,300 --> 00:05:35,160 S2 is just the original text. 105 00:05:35,160 --> 00:05:36,933 S2 just says hello. 106 00:05:38,310 --> 00:05:42,480 Right. So that concludes this lesson on Ownership. 107 00:05:42,480 --> 00:05:44,370 What we'll have a look at in the next lesson 108 00:05:44,370 --> 00:05:46,740 is a related concept of borrowing, 109 00:05:46,740 --> 00:05:49,590 where you can effectively take a reference to an object 110 00:05:49,590 --> 00:05:51,390 without claiming ownership, okay? 111 00:05:51,390 --> 00:05:52,950 So it kind of fits in well 112 00:05:52,950 --> 00:05:55,150 with what we've just covered in this lesson.