hi there serdar yagalov here for infoworld in this episode of smart rust we're going to
look at some of the fundamental concepts in rust that set it apart from other languages and one
of those concepts is ownership in memory managed languages like java or python or
languages from the dot net family like c sharp a garbage collector handles memory at runtime
so you can focus on writing code that solves your problems but this can come at the cost of speed
garbage collected and managed languages are always a little slower because of the overhead involved
in tracking and managing memory sometimes in ways you don't have direct control over
rust is like c and c plus plus in that the programmer is responsible for handling memory
this allows it to be faster at the cost of it being a little more elaborate to program
and in cnc plus plus this case at the cost of memory management errors potentially creeping in
but rust has unique mechanisms for handling memory that allow you to reason about how memory works so
that any code that isn't memory safe simply won't compile the first and most key of these mechanisms
is a concept called ownership it's the idea that a value can only belong to one given variable at
a time if you want to move or copy its value to another variable you have to be explicit about
this operation in this simple example i have a function that declares a string variable and
prints its contents and the variable's named data data is used directly there's no copying
or moving of the value all we do is run it and it prints the results with no problems there's
no ambiguity about what happens to this variable now consider this very slightly different version
we're creating a new variable data2 and setting it equal to data but if we try to run this if we're
trying to reuse data by printing you'll notice that it refuses to compile so what happened one
wonderful thing about rust by the way is it's very detailed and insightful compiler errors and the
error trace tells us that the value we are trying to use has been moved if we say data2 equals data
that means take the value with data move it to data2 and make data obsolete this is a
very different behavior from many other languages in something like c the same value would be copied
into data2 but rust is very strict about what values exist where and at what times
the better to keep them from being misreferenced now this behavior probably seems pretty nitpicky
at first but it's at the heart of how rust is able to let you write memory save code if something
violates the rules of rust's memory management system it won't compile so it's better to have
that happen at compile time than to be bushwhacked by it later when your code's actually running
now here's the third version of the same code and with this one we're using the clone method
on the string to make an explicit copy of data so now we have two independent variables
each with a copy and note that each of these variables in turn is also subject to the same
restrictions we just outlined here so if we run this code it will compile and run with no issues
now one exception to how ownership and copying works is when you're dealing with types that
can be handled entirely on the stack if you've worked with c or other system languages you ought
to be familiar with the stack versus the heap two different ways of allocating memory stack memory
is for small things like single integers or floating point values heap memory is for larger
things like strings and the string value we've been dealing with here in this example is a heap
allocated value but if we deal with just a plain old integer the rules for borrowing are a little
more relaxed in this example i'm doing what i did before i create a second variable that uses the
value of the first but because this is an integer a stack allocated value stack allocated values
don't need to be tracked the way heap values do their behavior in memory is much more predictable
that's the key thing the predictability of the behavior means that rust doesn't have to be as
explicit or detailed about how to handle it so with a with a variable like this we can
make copies of stack values and if we run this code it will compile and run without an issue
now when you learn how to create your own types in rust one of the things you'll learn is that
you can apply a number of behaviors or traits to these types one of them is the copy trait
which allows you to do exactly what i've described here it makes copies automatically by just making
assignments another is a trait called drop which deallocates memory for an object when
it's no longer in use when it goes out of scope as they say the key thing is you can
only either pick copy or drop for a given type because their behaviors are mutually exclusive
rust's memory management system requires this otherwise it won't be able to predict what
values would be assigned where and how long they live so to sum up rust memory management model
revolves around the idea of ownership a heap allocated value can only belong to one variable
at a time and you have to be explicit about where it's passed to who it currently owns it
stack allocated values can be copied freely though but you can't mix these behaviors in the same type
and finally any program that violates rust's memory management rules which tend to be fairly
strict will simply not compile the better to keep this from blowing up in your face at run time
now in future videos i'm going to delve into more detail about how rust ownership rules work
such as how it handles things like passing values across function boundaries which can often be
a little tricky to work with but absolutely necessary for anything beyond a trivial program
that's it for this video if you liked it please leave a comment below don't forget
to subscribe to the tech talk channel on youtube and for more smartrust and
more dev with serdar in general be sure to follow me on facebook youtube and infowl.com
you