Writing Unsafe Code in Rust is actually really easy to do.
All we have to do is place our code inside of an unsafe block to just to let the compiler know we are
about to execute unsafe code.
So let's just see a quick example of this.
So we're going to create an immutable variable called NUM and now we are going to create a raw pointer.
To these to that variable which is not unsafe to do.
We are very.
Capable of doing that.
And we can even print out the address.
Of these variables.
Let me copy and paste.
One or two.
So now we can run this and see that our address is point is printed out and it points back to the num
of five.
But if we try to d reference this, then it becomes an unsafe operation.
And if we try to run it, we already know it's not going to run.
It says DX reference of raw pointer is unsafe and requires unsafe function or block.
So all we have to do in order to run this is just say unsafe and then print it and then put the unsafe
code block into.
That code block.
So now if we run it, we see that we were able to successfully reference our raw pointer and print out
the value of five.
And just because you might use unsafe code in a function, it does not mean that the entire function
has to be unsafe.
So notice that all we did here was wrap the code that was considered unsafe and the unsafe code block.
So you can simply just use unsafe blocks explicitly where appropriate.
There is no need to wrap everything and an unsafe block if only one or two or however many lines is
executing the unsafe code.
That way you can continue to maximize the borrow checker and ownership model that Rust provides for
us.