1 00:00:06,660 --> 00:00:10,380 - Some simple types in Rust support copy semantics, 2 00:00:10,380 --> 00:00:13,560 rather than move semantics, as we know. 3 00:00:13,560 --> 00:00:15,720 So for example, numbers, 4 00:00:15,720 --> 00:00:18,330 there's a structure called SystemTime. 5 00:00:18,330 --> 00:00:20,250 The option enum. 6 00:00:20,250 --> 00:00:23,160 When you assign one of these to another one, 7 00:00:23,160 --> 00:00:25,080 it does a bitwise copy, okay? 8 00:00:25,080 --> 00:00:27,270 It is always a bitwise copy 9 00:00:27,270 --> 00:00:30,243 for types which implement the copy mechanism. 10 00:00:32,250 --> 00:00:34,920 So in a nutshell, as we're going to see in a moment, 11 00:00:34,920 --> 00:00:37,590 there is a trait called copy 12 00:00:37,590 --> 00:00:39,090 which you can implement yourself 13 00:00:39,090 --> 00:00:42,900 if you want your types to also support copy semantics. 14 00:00:42,900 --> 00:00:45,060 So in this example here, 15 00:00:45,060 --> 00:00:48,603 I've started off Num1 is a 32 bit integer. 16 00:00:49,470 --> 00:00:53,580 So i32, one of these kind of primitive types, 17 00:00:53,580 --> 00:00:55,263 implements the copy trait. 18 00:00:56,160 --> 00:00:58,980 So Num1 is 42. 19 00:00:58,980 --> 00:01:01,080 So for types like integers, 20 00:01:01,080 --> 00:01:02,820 which implement the copy trait, 21 00:01:02,820 --> 00:01:05,370 when you assign a variable to another, 22 00:01:05,370 --> 00:01:06,870 it does a bitwise copy. 23 00:01:06,870 --> 00:01:08,430 It doesn't kind of move ownership 24 00:01:08,430 --> 00:01:11,310 or anything hideous or insidious. 25 00:01:11,310 --> 00:01:13,530 It just does a straightforward bit copy 26 00:01:13,530 --> 00:01:16,342 from Num1 into Num2. 27 00:01:16,342 --> 00:01:20,830 So Num2 becomes a bitwise copy like so, 28 00:01:23,311 --> 00:01:26,100 and Num1 and Num2 are completely independent of each other. 29 00:01:26,100 --> 00:01:27,810 They kind of exist side by side. 30 00:01:27,810 --> 00:01:30,990 It just copied the bits from Num1 into Num2. 31 00:01:30,990 --> 00:01:33,120 They're separate variables, obviously. 32 00:01:33,120 --> 00:01:35,640 So I could change Num2, 33 00:01:35,640 --> 00:01:37,200 I could increment Num2. 34 00:01:37,200 --> 00:01:39,560 So Num2 was 42, 35 00:01:39,560 --> 00:01:42,510 it becomes 43. 36 00:01:42,510 --> 00:01:45,300 And then when I output Num1, it's 42. 37 00:01:45,300 --> 00:01:48,510 When I open Num2, it's 43. 38 00:01:48,510 --> 00:01:49,700 So for built-in types, 39 00:01:49,700 --> 00:01:53,550 or for some simple types like i32 an option, 40 00:01:53,550 --> 00:01:57,000 it does copying out of the box. 41 00:01:57,000 --> 00:01:59,650 In other words, those types implement the copy trait. 42 00:02:00,810 --> 00:02:04,200 So if you want to support copy semantics in your structures 43 00:02:04,200 --> 00:02:06,600 so that your structure here is more like a value type 44 00:02:06,600 --> 00:02:08,037 as you'd call it in C#. 45 00:02:09,150 --> 00:02:11,850 What you do is you implement the copy trait 46 00:02:11,850 --> 00:02:14,550 and the copy trait is what we call a marker trait. 47 00:02:14,550 --> 00:02:16,290 It doesn't have any methods, 48 00:02:16,290 --> 00:02:17,460 it's just like a stamp 49 00:02:17,460 --> 00:02:20,130 like a Boolean flag on that data type. 50 00:02:20,130 --> 00:02:21,450 It tells the compiler, 51 00:02:21,450 --> 00:02:24,810 this type supports the concept of copying. 52 00:02:24,810 --> 00:02:27,690 So if you assign A to B, 53 00:02:27,690 --> 00:02:29,550 do a bitwise copy 54 00:02:29,550 --> 00:02:32,013 rather than doing a move. 55 00:02:33,150 --> 00:02:37,530 Now copy inherits from clone. 56 00:02:37,530 --> 00:02:41,040 So if one of your structures implements the copy trait, 57 00:02:41,040 --> 00:02:43,650 it must also implement the super trait clone. 58 00:02:43,650 --> 00:02:45,150 So it looks like this, 59 00:02:45,150 --> 00:02:45,983 the clone trait, 60 00:02:45,983 --> 00:02:48,480 we looked at this in the previous demo, 61 00:02:48,480 --> 00:02:49,860 it has a clone method 62 00:02:49,860 --> 00:02:51,330 that you can implement 63 00:02:51,330 --> 00:02:52,890 plus a clone from method 64 00:02:52,890 --> 00:02:54,720 that you typically don't implement. 65 00:02:54,720 --> 00:02:56,400 It's already kind of implemented 66 00:02:56,400 --> 00:02:58,650 with a default behavior. 67 00:02:58,650 --> 00:03:02,760 So cloning is where you basically do a carbon copy 68 00:03:02,760 --> 00:03:03,960 of an object. 69 00:03:03,960 --> 00:03:07,140 The copy trait inherits from clone. 70 00:03:07,140 --> 00:03:10,200 So a structure of yours which implements copy 71 00:03:10,200 --> 00:03:12,840 must also implement clone. 72 00:03:12,840 --> 00:03:15,570 So you can find your structure type here. 73 00:03:15,570 --> 00:03:18,090 I've got a structure type called currency 74 00:03:18,090 --> 00:03:21,900 and the currency will implement the copy trait 75 00:03:21,900 --> 00:03:23,730 but there aren't any methods there to implement. 76 00:03:23,730 --> 00:03:25,320 It's just a flag really 77 00:03:25,320 --> 00:03:29,130 telling the compiler implement copy semantics for this type, 78 00:03:29,130 --> 00:03:30,930 not move semantics. 79 00:03:30,930 --> 00:03:33,180 Don't move the data away on an assignment 80 00:03:33,180 --> 00:03:34,560 just do a bitwise copy. 81 00:03:34,560 --> 00:03:36,173 That's what you're saying, right? 82 00:03:37,710 --> 00:03:39,570 But if you implement the copy trait 83 00:03:39,570 --> 00:03:42,060 then also you have to implement the clone trait 84 00:03:42,060 --> 00:03:45,660 because that's what inheritance means in Rust. 85 00:03:45,660 --> 00:03:47,490 If you implement a trait 86 00:03:47,490 --> 00:03:50,193 you must also implement all of the methods above. 87 00:03:51,060 --> 00:03:53,790 So currency would have to implement the clone method 88 00:03:53,790 --> 00:03:55,593 and the clone from, 89 00:03:55,593 --> 00:03:56,460 or to be more precise 90 00:03:56,460 --> 00:03:58,440 it would implement the clone method 91 00:03:58,440 --> 00:04:01,140 and it would probably inherit the default implementation 92 00:04:01,140 --> 00:04:02,550 of clone from 93 00:04:02,550 --> 00:04:04,380 because you typically don't need to override that. 94 00:04:04,380 --> 00:04:07,830 It already has a sufficient implementation already. 95 00:04:07,830 --> 00:04:10,620 So currency implements copy, 96 00:04:10,620 --> 00:04:12,213 and it also implements clone. 97 00:04:13,410 --> 00:04:15,090 Here is an example. 98 00:04:15,090 --> 00:04:17,100 I've done this manually initially. 99 00:04:17,100 --> 00:04:20,223 Here's my Currency structure dollars and cents. 100 00:04:21,345 --> 00:04:22,980 Okay so interestingly 101 00:04:22,980 --> 00:04:26,310 that the individual fields here support copying 102 00:04:26,310 --> 00:04:29,970 but the structure itself doesn't natively support copying 103 00:04:29,970 --> 00:04:33,060 unless you actually tell the compiler so. 104 00:04:33,060 --> 00:04:36,330 So unless and until you implement the copy trait 105 00:04:36,330 --> 00:04:37,350 for currency 106 00:04:37,350 --> 00:04:38,550 when you do that, 107 00:04:38,550 --> 00:04:40,210 it means that currency assignment 108 00:04:41,640 --> 00:04:45,720 will be a bitwise copy rather than a move. 109 00:04:45,720 --> 00:04:50,333 So that means if you said currency one was $10.50 110 00:04:52,050 --> 00:04:53,643 and then you had currency two, 111 00:04:54,480 --> 00:04:57,930 when you say currency two equals currency one 112 00:04:57,930 --> 00:05:02,260 it'll do a bitwise copy of the values in here 113 00:05:03,256 --> 00:05:04,860 10.50 like that, 114 00:05:04,860 --> 00:05:07,590 rather than moving the data from C1 115 00:05:07,590 --> 00:05:10,920 it'll just do a bitwise copy like so, okay? 116 00:05:10,920 --> 00:05:12,990 So you just have to implement the copy trait. 117 00:05:12,990 --> 00:05:13,860 There are no methods, 118 00:05:13,860 --> 00:05:15,720 it's just like a statement of intent 119 00:05:15,720 --> 00:05:18,420 but you do have to implement the super trait as well. 120 00:05:18,420 --> 00:05:21,000 So if you implement copy for currency 121 00:05:21,000 --> 00:05:23,043 you must also implement clone. 122 00:05:24,210 --> 00:05:25,860 And in here it's interesting, 123 00:05:25,860 --> 00:05:26,830 the clone method 124 00:05:29,370 --> 00:05:30,690 it says custom cloning, 125 00:05:30,690 --> 00:05:32,130 it just returns. 126 00:05:32,130 --> 00:05:34,230 Remember the point of the clone method 127 00:05:34,230 --> 00:05:38,823 is to basically create an object that has the same data. 128 00:05:40,819 --> 00:05:44,490 So if I said if C1 contain the values 10.50 129 00:05:48,170 --> 00:05:51,310 and I said C2 equals C1.clone 130 00:05:53,370 --> 00:05:57,090 if I did that, okay? 131 00:05:57,090 --> 00:05:58,863 Then it'll call the clone method. 132 00:06:00,980 --> 00:06:03,540 And the clone method does some printing maybe 133 00:06:03,540 --> 00:06:08,540 and then it returns the contents of itself. 134 00:06:08,550 --> 00:06:11,560 Remember the self reference 135 00:06:12,420 --> 00:06:14,850 is a reference to the object 136 00:06:14,850 --> 00:06:16,470 upon which you call the clone method. 137 00:06:16,470 --> 00:06:21,030 When you say C1.clone self will be C1. 138 00:06:21,030 --> 00:06:24,090 So what you do is you return the contents 139 00:06:24,090 --> 00:06:25,380 of that reference 140 00:06:25,380 --> 00:06:28,020 it'll return the contents of this object, 141 00:06:28,020 --> 00:06:29,520 will be returned. 142 00:06:29,520 --> 00:06:31,660 So C1.clone will return 143 00:06:34,066 --> 00:06:38,187 a copy of 10.50 like so 144 00:06:38,187 --> 00:06:42,030 and C2 becomes that, okay? 145 00:06:42,030 --> 00:06:43,680 So that's how it works. 146 00:06:43,680 --> 00:06:46,290 So in this syntax here, 147 00:06:46,290 --> 00:06:47,820 we can think of references 148 00:06:47,820 --> 00:06:50,790 as being a little bit like pointers. 149 00:06:50,790 --> 00:06:52,470 If ever pointed to an object, 150 00:06:52,470 --> 00:06:56,190 then star returns the contents of that object, 151 00:06:56,190 --> 00:06:58,890 it returns a copy of C1 152 00:06:58,890 --> 00:07:01,200 and that's what would then get assigned 153 00:07:01,200 --> 00:07:03,360 into the variable on the left hand side 154 00:07:03,360 --> 00:07:06,180 of the assignment, right? 155 00:07:06,180 --> 00:07:09,550 Okay so this is how to implement the copy trait 156 00:07:10,620 --> 00:07:13,293 and the clone trait manually. 157 00:07:14,340 --> 00:07:17,820 But often you can rely on default implementation. 158 00:07:17,820 --> 00:07:19,260 You can just ask a compiler 159 00:07:19,260 --> 00:07:22,600 to automatically implement the copy trait 160 00:07:23,760 --> 00:07:25,343 and the clone trait, okay? 161 00:07:26,460 --> 00:07:27,570 And you can do that 162 00:07:27,570 --> 00:07:31,680 if every field and your structure implements copy. 163 00:07:31,680 --> 00:07:35,200 So this field here implements the copy trait 164 00:07:36,150 --> 00:07:38,460 integers know how to copy themselves. 165 00:07:38,460 --> 00:07:43,080 A bitwise copy i32 does implement copy. 166 00:07:43,080 --> 00:07:46,110 If all the fields inside the structure 167 00:07:46,110 --> 00:07:47,410 implement the copy trait 168 00:07:48,286 --> 00:07:49,230 then you can ask the compiler 169 00:07:49,230 --> 00:07:53,040 to automatically generate a copy implementation 170 00:07:53,040 --> 00:07:55,560 and automatically implement the clone implementation. 171 00:07:55,560 --> 00:07:57,960 So you don't have to write any implementation yourself 172 00:07:57,960 --> 00:07:59,940 it'll be generated for you. 173 00:07:59,940 --> 00:08:03,047 The default copy will be bitwise. 174 00:08:03,047 --> 00:08:05,130 So if you said C1 equals C2, 175 00:08:05,130 --> 00:08:07,860 it'll do a bitwise copy of each field 176 00:08:07,860 --> 00:08:09,810 and give you back the resulting object. 177 00:08:10,650 --> 00:08:12,330 And in a similar way 178 00:08:12,330 --> 00:08:16,080 the default cloning is also field by field, okay? 179 00:08:16,080 --> 00:08:19,620 So if you said C1 equals C2.clone, 180 00:08:19,620 --> 00:08:21,600 then the clone implementation, 181 00:08:21,600 --> 00:08:23,670 it'll call the clone method on that 182 00:08:23,670 --> 00:08:26,220 and the clone method on that, okay? 183 00:08:26,220 --> 00:08:28,590 And for most types, this is sufficient. 184 00:08:28,590 --> 00:08:30,840 It's just a matter of expressing the fact that you want it. 185 00:08:30,840 --> 00:08:34,500 You want your structure type to support copying 186 00:08:34,500 --> 00:08:36,090 and therefore cloning as well, 187 00:08:36,090 --> 00:08:38,880 because this trait inherits from there. 188 00:08:38,880 --> 00:08:40,560 So if you implement that trait, 189 00:08:40,560 --> 00:08:42,000 you've also got to specify 190 00:08:42,000 --> 00:08:44,253 that you implement that trait as well. 191 00:08:45,330 --> 00:08:46,830 So I feel an example coming on, 192 00:08:46,830 --> 00:08:48,690 lesson 12 traits is the project, 193 00:08:48,690 --> 00:08:50,670 we'll kick off with the main code. 194 00:08:50,670 --> 00:08:52,110 The structure we're going to look at 195 00:08:52,110 --> 00:08:54,660 is in the copyable module. 196 00:08:54,660 --> 00:08:56,700 That's where we're going to have our structure. 197 00:08:56,700 --> 00:08:59,220 And the code that's going to use that structure 198 00:08:59,220 --> 00:09:02,370 is going to be in demo_copyable.rs 199 00:09:02,370 --> 00:09:05,520 and then we'll run the project once we've discussed. 200 00:09:05,520 --> 00:09:09,060 Okay, so then here's main, 201 00:09:09,060 --> 00:09:12,750 in main let me un-comment demo copyable. 202 00:09:12,750 --> 00:09:15,000 This is the last demo of the lesson. 203 00:09:15,000 --> 00:09:16,800 It's been quite a journey. 204 00:09:16,800 --> 00:09:19,050 So let's un-comment that statement. 205 00:09:19,050 --> 00:09:21,030 And then in the structures 206 00:09:21,030 --> 00:09:22,500 I want to look at the example 207 00:09:22,500 --> 00:09:25,570 that illustrates copyable. 208 00:09:25,570 --> 00:09:27,760 Okay, so I have a currency structure 209 00:09:28,620 --> 00:09:30,420 with a dollars and cents 210 00:09:30,420 --> 00:09:31,860 and I've asked the compiler 211 00:09:31,860 --> 00:09:34,710 to automatically generate the debug output. 212 00:09:34,710 --> 00:09:36,732 Remember the default debug 213 00:09:36,732 --> 00:09:40,080 will output the name of the structure currency 214 00:09:40,080 --> 00:09:41,310 and it'll output the dollars 215 00:09:41,310 --> 00:09:43,560 and the cents field one by one. 216 00:09:43,560 --> 00:09:46,920 I've also requested automatic implementation 217 00:09:46,920 --> 00:09:48,540 of the copy trait, 218 00:09:48,540 --> 00:09:51,330 which will do a bitwise copy of each field 219 00:09:51,330 --> 00:09:52,410 effectively to give me back 220 00:09:52,410 --> 00:09:54,810 a bitwise copy of the composite. 221 00:09:54,810 --> 00:09:57,660 And I've asked for it to implement the clone 222 00:09:57,660 --> 00:09:59,820 so it'll clone that field 223 00:09:59,820 --> 00:10:01,953 and clone that field as well. 224 00:10:02,910 --> 00:10:05,490 I've got a new method kind of usual 225 00:10:05,490 --> 00:10:07,830 really a new associated function I should say, 226 00:10:07,830 --> 00:10:10,980 which creates and returns a currency object 227 00:10:10,980 --> 00:10:12,530 with the values that I specify. 228 00:10:13,890 --> 00:10:16,890 Down here, I've got the manual implementation 229 00:10:16,890 --> 00:10:18,270 of copy and clone. 230 00:10:18,270 --> 00:10:19,920 Those are commented out. 231 00:10:19,920 --> 00:10:22,860 We'll come back and un-comment those later 232 00:10:22,860 --> 00:10:24,690 in a few minutes. 233 00:10:24,690 --> 00:10:25,523 First of all, 234 00:10:25,523 --> 00:10:27,360 let's just run with the structure as it stands 235 00:10:27,360 --> 00:10:29,820 with its default trade implementation. 236 00:10:29,820 --> 00:10:31,680 Let's see how that looks. 237 00:10:31,680 --> 00:10:33,003 So demo copyable, 238 00:10:34,976 --> 00:10:36,713 I create a currency $10.99 239 00:10:41,250 --> 00:10:42,753 and then I assign C1 to C2. 240 00:10:44,040 --> 00:10:44,873 At this point, 241 00:10:44,873 --> 00:10:47,760 the compiler will say does it have to do a copy 242 00:10:47,760 --> 00:10:49,200 or does it do a move? 243 00:10:49,200 --> 00:10:52,443 It depends on whether currency implements copy. 244 00:10:53,490 --> 00:10:57,540 Well, currency does implement copy. 245 00:10:57,540 --> 00:10:58,923 We did request that. 246 00:11:00,150 --> 00:11:02,370 Therefore this will be a copy. 247 00:11:02,370 --> 00:11:04,980 It'll do a bitwise copy of each field, 248 00:11:04,980 --> 00:11:06,690 dollars and cents into here. 249 00:11:06,690 --> 00:11:11,133 So C2 will be a copy of $10.99, 250 00:11:11,970 --> 00:11:13,500 no movement has occurred. 251 00:11:13,500 --> 00:11:15,947 We haven't moved data out of C1 into C2 252 00:11:17,395 --> 00:11:19,140 like what would happen with other types 253 00:11:19,140 --> 00:11:20,610 that don't implement copy. 254 00:11:20,610 --> 00:11:22,170 It's just done a bitwise copy 255 00:11:22,170 --> 00:11:23,760 from that data into that object. 256 00:11:23,760 --> 00:11:27,810 So I can continue to use C1 and C2. 257 00:11:27,810 --> 00:11:30,960 I can change C1 to be $11. 258 00:11:30,960 --> 00:11:31,803 It was 10. 259 00:11:32,640 --> 00:11:35,880 I can change C2 to be $22. 260 00:11:35,880 --> 00:11:37,680 That was also 10 initially. 261 00:11:37,680 --> 00:11:39,870 And then I can output using the debug formatter 262 00:11:39,870 --> 00:11:41,010 which is quite cool. 263 00:11:41,010 --> 00:11:42,270 I can output C1 264 00:11:42,270 --> 00:11:44,597 that would be $11.99 265 00:11:44,597 --> 00:11:47,377 and C2 will be $22.99, 266 00:11:47,377 --> 00:11:50,627 $22,99, right? 267 00:11:54,480 --> 00:11:56,403 So let's just run this to confirm. 268 00:11:59,670 --> 00:12:01,500 So using the debug formatter 269 00:12:01,500 --> 00:12:04,020 the currency one $11 99. 270 00:12:04,020 --> 00:12:06,488 That's correct, isn't it, $11.99? 271 00:12:06,488 --> 00:12:07,321 And C2, 272 00:12:07,321 --> 00:12:09,450 again, using the debug formatter 273 00:12:09,450 --> 00:12:11,820 using the debug formatter here, 274 00:12:11,820 --> 00:12:13,143 it outputs all the fields. 275 00:12:14,446 --> 00:12:17,520 C2 is $22 99. 276 00:12:17,520 --> 00:12:18,780 Okay so that's correct. 277 00:12:18,780 --> 00:12:22,784 That's using the the default implementation 278 00:12:22,784 --> 00:12:25,680 of the copy trait. 279 00:12:25,680 --> 00:12:26,580 So what I could do 280 00:12:26,580 --> 00:12:28,576 is I could rather than relying 281 00:12:28,576 --> 00:12:29,850 on the default implementation 282 00:12:30,870 --> 00:12:32,790 I could rely on custom implementation. 283 00:12:32,790 --> 00:12:34,710 I could implement the copy trait, 284 00:12:34,710 --> 00:12:39,120 a market trait you just say as a statement of fact 285 00:12:39,120 --> 00:12:41,100 I want copy behavior, 286 00:12:41,100 --> 00:12:42,870 not move behavior, 287 00:12:42,870 --> 00:12:46,030 and then a custom implementation of clone as well 288 00:12:47,460 --> 00:12:49,090 which just basically returns 289 00:12:50,940 --> 00:12:53,133 a copy of my own data effectively, 290 00:12:54,210 --> 00:12:55,233 the contents of, 291 00:12:58,984 --> 00:13:00,570 so this will be custom behavior 292 00:13:00,570 --> 00:13:03,333 that's going to be implemented achieved. 293 00:13:05,304 --> 00:13:06,804 So let's do a cargo run again. 294 00:13:08,970 --> 00:13:12,180 Okay, so currency, those values, 295 00:13:12,180 --> 00:13:14,850 the default copying did work. 296 00:13:14,850 --> 00:13:16,980 I haven't actually called the clone method. 297 00:13:16,980 --> 00:13:18,630 I have to provide it. 298 00:13:18,630 --> 00:13:20,700 If you implement a substrate, 299 00:13:20,700 --> 00:13:23,190 you've got to implement the super trait above it, 300 00:13:23,190 --> 00:13:25,260 even if you don't actually use it yet. 301 00:13:25,260 --> 00:13:26,190 I could have done, of course, 302 00:13:26,190 --> 00:13:28,950 I could have said in my main code here, 303 00:13:28,950 --> 00:13:31,860 I could have said C1.clone 304 00:13:31,860 --> 00:13:35,010 but I didn't need to in my demo. 305 00:13:35,010 --> 00:13:36,750 One last thing I want to discuss 306 00:13:36,750 --> 00:13:40,470 is what if you don't implement copy at all? 307 00:13:40,470 --> 00:13:42,993 So back in my structure, 308 00:13:43,890 --> 00:13:46,470 I've commented out the copy 309 00:13:46,470 --> 00:13:48,960 and clone implementation there 310 00:13:48,960 --> 00:13:51,240 and I'm going to also comment out 311 00:13:51,240 --> 00:13:54,153 the manual implementation here. 312 00:13:55,590 --> 00:13:59,190 Okay, so what that means, 313 00:13:59,190 --> 00:14:02,160 what that means is that my currency structure 314 00:14:02,160 --> 00:14:04,710 doesn't support copy and behavior. 315 00:14:04,710 --> 00:14:06,810 So remember what happens 316 00:14:06,810 --> 00:14:09,453 when you assign things that don't implement copy? 317 00:14:11,040 --> 00:14:13,083 It doesn't move, okay? 318 00:14:14,461 --> 00:14:17,700 So C1 was $10.99, 319 00:14:17,700 --> 00:14:21,210 and because currency doesn't implement copy anymore, 320 00:14:21,210 --> 00:14:23,550 when I say C2 equals C1 321 00:14:23,550 --> 00:14:28,020 it'll move ownership of the data from C1 into C2. 322 00:14:28,020 --> 00:14:29,670 If you don't implement copy, 323 00:14:29,670 --> 00:14:31,170 then a move occurs 324 00:14:31,170 --> 00:14:34,240 the data will be moved into C2 325 00:14:35,638 --> 00:14:38,580 and C1 will be vacated, okay? 326 00:14:38,580 --> 00:14:40,110 We've looked at this a lot. 327 00:14:40,110 --> 00:14:43,590 This is the whole idea of ownership in Rust. 328 00:14:43,590 --> 00:14:45,180 When you assign with types 329 00:14:45,180 --> 00:14:46,590 that don't implement copy 330 00:14:46,590 --> 00:14:48,090 it moves ownership of the data 331 00:14:48,090 --> 00:14:50,430 from here into here, 332 00:14:50,430 --> 00:14:52,860 it means this object can no longer be used. 333 00:14:52,860 --> 00:14:54,490 Once you've moved data out of C1 334 00:14:55,380 --> 00:14:56,883 you can't use C1 anymore. 335 00:14:58,440 --> 00:15:01,170 Okay, so I'm going to get an error now, 336 00:15:01,170 --> 00:15:03,270 all I wanted to do was to copy the data 337 00:15:03,270 --> 00:15:04,620 but it didn't copy. 338 00:15:04,620 --> 00:15:06,753 It moved away from C1 into C2. 339 00:15:08,009 --> 00:15:10,830 C1 is no longer usable afterwards 340 00:15:10,830 --> 00:15:12,210 which is a terrible shame. 341 00:15:12,210 --> 00:15:13,323 It's a crying shame. 342 00:15:15,780 --> 00:15:17,796 Well, we knew that, didn't we? 343 00:15:17,796 --> 00:15:19,773 It said C1 currency. 344 00:15:20,790 --> 00:15:24,480 The currency structure doesn't implement the copy trait. 345 00:15:24,480 --> 00:15:27,900 Therefore a move occurs here 346 00:15:27,900 --> 00:15:29,010 because we didn't tell it 347 00:15:29,010 --> 00:15:30,180 we wanted copy behavior. 348 00:15:30,180 --> 00:15:32,343 So it moved ownership from C1 into C2, 349 00:15:33,720 --> 00:15:36,780 which means that C1 can't be used anymore 350 00:15:36,780 --> 00:15:38,880 because it's been moved. 351 00:15:38,880 --> 00:15:41,640 C1 is effectively like a dead object now. 352 00:15:41,640 --> 00:15:43,470 The data doesn't live here anymore. 353 00:15:43,470 --> 00:15:44,883 It's been moved into C2. 354 00:15:45,720 --> 00:15:46,960 So the recommendation 355 00:15:48,413 --> 00:15:49,440 is for simple types, 356 00:15:49,440 --> 00:15:52,950 for structures that are effectively just like values 357 00:15:52,950 --> 00:15:56,400 that want to behave as if they just be copied. 358 00:15:56,400 --> 00:15:58,602 I can just copy the data from here to here 359 00:15:58,602 --> 00:16:00,540 as a bitwise copy that actually makes sense. 360 00:16:00,540 --> 00:16:03,930 Then in that case you do want to implement copy 361 00:16:03,930 --> 00:16:05,610 and clone. 362 00:16:05,610 --> 00:16:07,080 And in most cases 363 00:16:07,080 --> 00:16:09,540 the default implementation will be fine 364 00:16:09,540 --> 00:16:13,350 as long as your fields implement copying themselves. 365 00:16:13,350 --> 00:16:14,700 And if they don't 366 00:16:14,700 --> 00:16:16,380 and you want something more exotic 367 00:16:16,380 --> 00:16:20,220 then you can always implement copy and clone manually 368 00:16:20,220 --> 00:16:22,650 as we've done down here if you need to. 369 00:16:22,650 --> 00:16:24,210 Okay, so one way or the other 370 00:16:24,210 --> 00:16:26,040 you can implement copy behavior 371 00:16:26,040 --> 00:16:28,230 and that means when you assign one variable to another 372 00:16:28,230 --> 00:16:30,420 it just does a bitwise copy now 373 00:16:30,420 --> 00:16:32,970 instead of trying to move data away. 374 00:16:32,970 --> 00:16:34,770 Okay so the application works again.