When we talk about memory, we as programmers like two things.
We want our memory to be given back to us at a time of our choosing, and we never want to reference
memory once we have given it back to the system.
Because if we do this, then it causes undefined behavior and can lead to crashes and security holes.
In 2019, Microsoft Security Response Center reported that roughly 70% of the vulnerabilities they assigned
to a CVE each year continue to be memory safety issues.
So memory safety is a big deal.
Typically, programming languages have done two options.
The first one is Garbage Collection, which Java, C-sharp, Python, JavaScript, and so on all have
with a garbage collector.
Programmers give up having control over when objects are free to the collector.
The second is through manual freeing of the memory, such as what is done in C and C++, which is great
if you never make a mistake.
But history says programmers are likely to make a mistake.
So what is rescues?
Well, technically, neither rescues is an ownership module and ownership is Russ most unique feature.
Russ Compiler is going to verify that your program is free of memory safety errors, such as dangling
pointers, double freeze using initialized memory and so on.
This concept is what drives the rust language, so it is important to understand the reason behind this
design choice.
So let's start looking at what ownership is, because it is a new concept for most programmers.
So it does take time to get used to.
But the more you use rust and become familiar with the rules of ownership, the more natural you will
begin to feel with the concept.
Let's quickly talk about the stack and the heap, which are parts of memory.
The stack is going to store values in the order it gets them and removes the values in the opposite
order.
So it is a last in, first out.
A great example of this is a stack of plates you add to the top, but then you also remove from the
top.
All data stored on the stack must have a known fixed size data without a known size at compile time
or a size that may change will be stored on the heap.
The heap is less organized and when you put something onto the heap you request, you request, request
the space, and that will then return a pointer, which is the address of that location.
Pushing onto the stack is faster than allocating on the heap.
So searching for data on the stack is also faster than searching the heap.
When you pass values into a function or have local variables inside the function, they are all pushed
to the stack and then when the function is over, they're popped off the stack, which is the same thing
as removing them from the stack.
So I mentioned earlier the rules of ownership.
What are they?
Well, the rules are each value in rest has a variable called its owner.
There can only be one owner at a time.
And then when the owner goes out of scope, the value will be dropped, also known as free.
So let's start off with a simple example and just start trying to understand what these concepts mean.
So for the first one, we'll say let var equals one.
So in this case, Var is now valid for the rest of the main function.
And since we know that the value one is of IE 32 or u 32, in this case it's ie 32.
It has a fixed size.
So we know that we're getting pushed onto the stack, but now say we're down, we're down here.
Now bar is dropped because the main function is over.
So we are freed from memory.
So same thing with strings.
Strings can change in size.
So we're going to do a mutable string called S and we will do Hello to string.
So now we have.
The S variable, which is a string called with the data.
Hello.
And this is going to be created on the heap.
Created on the heap.
On the stack.
So just like down here, now that we are no longer in the main function.
S is also dropped and the resources are given back to the operating system.
So now we can push a string on here and just say hello world and what this is going to do.
It's going to append world at the end of Hello, which we can do because we are allocated on the heap
and that means we are able to grow.
So in the following lectures we are going to dive into more into these mechanisms that drive ownership.