1 00:00:05,960 --> 00:00:10,310 When we talk about memory, we as programmers like two things. 2 00:00:10,430 --> 00:00:15,260 We want our memory to be given back to us at a time of our choosing, and we never want to reference 3 00:00:15,260 --> 00:00:17,660 memory once we have given it back to the system. 4 00:00:17,660 --> 00:00:23,570 Because if we do this, then it causes undefined behavior and can lead to crashes and security holes. 5 00:00:24,450 --> 00:00:30,960 In 2019, Microsoft Security Response Center reported that roughly 70% of the vulnerabilities they assigned 6 00:00:30,960 --> 00:00:35,490 to a CVE each year continue to be memory safety issues. 7 00:00:35,490 --> 00:00:37,410 So memory safety is a big deal. 8 00:00:37,860 --> 00:00:40,770 Typically, programming languages have done two options. 9 00:00:40,770 --> 00:00:47,490 The first one is Garbage Collection, which Java, C-sharp, Python, JavaScript, and so on all have 10 00:00:47,760 --> 00:00:49,140 with a garbage collector. 11 00:00:49,140 --> 00:00:53,910 Programmers give up having control over when objects are free to the collector. 12 00:00:54,030 --> 00:01:01,590 The second is through manual freeing of the memory, such as what is done in C and C++, which is great 13 00:01:01,590 --> 00:01:03,120 if you never make a mistake. 14 00:01:03,120 --> 00:01:05,940 But history says programmers are likely to make a mistake. 15 00:01:06,740 --> 00:01:07,940 So what is rescues? 16 00:01:07,970 --> 00:01:16,880 Well, technically, neither rescues is an ownership module and ownership is Russ most unique feature. 17 00:01:17,030 --> 00:01:23,000 Russ Compiler is going to verify that your program is free of memory safety errors, such as dangling 18 00:01:23,000 --> 00:01:27,620 pointers, double freeze using initialized memory and so on. 19 00:01:27,860 --> 00:01:33,230 This concept is what drives the rust language, so it is important to understand the reason behind this 20 00:01:33,230 --> 00:01:34,340 design choice. 21 00:01:34,760 --> 00:01:42,350 So let's start looking at what ownership is, because it is a new concept for most programmers. 22 00:01:42,350 --> 00:01:44,500 So it does take time to get used to. 23 00:01:44,510 --> 00:01:49,070 But the more you use rust and become familiar with the rules of ownership, the more natural you will 24 00:01:49,070 --> 00:01:50,750 begin to feel with the concept. 25 00:01:51,170 --> 00:01:54,830 Let's quickly talk about the stack and the heap, which are parts of memory. 26 00:01:55,720 --> 00:02:01,000 The stack is going to store values in the order it gets them and removes the values in the opposite 27 00:02:01,000 --> 00:02:01,450 order. 28 00:02:01,450 --> 00:02:03,850 So it is a last in, first out. 29 00:02:04,300 --> 00:02:09,790 A great example of this is a stack of plates you add to the top, but then you also remove from the 30 00:02:09,790 --> 00:02:10,450 top. 31 00:02:10,450 --> 00:02:18,310 All data stored on the stack must have a known fixed size data without a known size at compile time 32 00:02:18,310 --> 00:02:22,030 or a size that may change will be stored on the heap. 33 00:02:22,210 --> 00:02:28,750 The heap is less organized and when you put something onto the heap you request, you request, request 34 00:02:28,750 --> 00:02:34,000 the space, and that will then return a pointer, which is the address of that location. 35 00:02:34,860 --> 00:02:38,280 Pushing onto the stack is faster than allocating on the heap. 36 00:02:38,580 --> 00:02:42,540 So searching for data on the stack is also faster than searching the heap. 37 00:02:42,840 --> 00:02:48,960 When you pass values into a function or have local variables inside the function, they are all pushed 38 00:02:48,960 --> 00:02:54,570 to the stack and then when the function is over, they're popped off the stack, which is the same thing 39 00:02:54,570 --> 00:02:56,190 as removing them from the stack. 40 00:02:56,340 --> 00:02:58,620 So I mentioned earlier the rules of ownership. 41 00:02:59,190 --> 00:03:00,000 What are they? 42 00:03:00,210 --> 00:03:04,830 Well, the rules are each value in rest has a variable called its owner. 43 00:03:05,070 --> 00:03:07,980 There can only be one owner at a time. 44 00:03:08,370 --> 00:03:13,650 And then when the owner goes out of scope, the value will be dropped, also known as free. 45 00:03:13,680 --> 00:03:19,650 So let's start off with a simple example and just start trying to understand what these concepts mean. 46 00:03:19,860 --> 00:03:24,420 So for the first one, we'll say let var equals one. 47 00:03:25,110 --> 00:03:30,420 So in this case, Var is now valid for the rest of the main function. 48 00:03:30,720 --> 00:03:40,110 And since we know that the value one is of IE 32 or u 32, in this case it's ie 32. 49 00:03:41,720 --> 00:03:43,130 It has a fixed size. 50 00:03:43,130 --> 00:03:48,620 So we know that we're getting pushed onto the stack, but now say we're down, we're down here. 51 00:03:48,620 --> 00:03:55,130 Now bar is dropped because the main function is over. 52 00:03:55,130 --> 00:03:56,840 So we are freed from memory. 53 00:03:57,770 --> 00:03:59,450 So same thing with strings. 54 00:03:59,480 --> 00:04:02,480 Strings can change in size. 55 00:04:02,480 --> 00:04:13,400 So we're going to do a mutable string called S and we will do Hello to string. 56 00:04:14,180 --> 00:04:15,650 So now we have. 57 00:04:17,490 --> 00:04:20,970 The S variable, which is a string called with the data. 58 00:04:20,970 --> 00:04:21,540 Hello. 59 00:04:21,540 --> 00:04:24,270 And this is going to be created on the heap. 60 00:04:25,770 --> 00:04:27,510 Created on the heap. 61 00:04:29,960 --> 00:04:31,580 On the stack. 62 00:04:33,140 --> 00:04:37,520 So just like down here, now that we are no longer in the main function. 63 00:04:37,910 --> 00:04:43,580 S is also dropped and the resources are given back to the operating system. 64 00:04:45,080 --> 00:04:54,860 So now we can push a string on here and just say hello world and what this is going to do. 65 00:04:54,860 --> 00:05:01,580 It's going to append world at the end of Hello, which we can do because we are allocated on the heap 66 00:05:01,580 --> 00:05:03,770 and that means we are able to grow. 67 00:05:04,340 --> 00:05:11,720 So in the following lectures we are going to dive into more into these mechanisms that drive ownership.