Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

In Rust, the first basic principle of memory is ownership: a value belongs to its variable. In this section we will look at what happens when:

19.1.1Cloning

The Clone trait (see Traits chapter), when implemented by a type, allows the explicit copy of an object through the use of the clone() method.

Depending on the implementation of the clone() method, the copy may be done:

Here is an example in which we clone a String object (the String type allocates memory on the heap for storing a character string):

Couldn't automatically determine type of variable `s2`.
Please give it an explicit type.

19.1.2Passing a value

When a value is passed from a variable to another variable or a function, one of two things happen:

Both behaviours are implicitly selected by the compiler, and the choice between them depends on the implementation or not of the Copy trait by the value’s type. If the value’s type implements the Copy trait, the value is copied, otherwise it is moved. With either copy or move the destination gets the ownership of the data:

19.1.2.1Copying

The Copy trait specializes the Clone trait (i.e.: it implements the clone() method) by making a raw copy of the memory (memory bytes copy). The copy has thus no knowledge of the data structure.

To implement the Copy trait, a type must NOT:

The built-in types that implement Copy are:

In the following example, the table a is implicitly copied into b:

19.1.2.2Moving

When its value is moved, a variable loses the ownership and cannot be used anymore.

In the following example, s cannot be used anymore once its value is passed to t:

19.1.3Passing to a function

The same copy/move mechanism applies when passing values to functions.

Here is an example with a String value that is passed to a function:

What happens is the following:

If s1 is needed after the call to foo(), one solution that we have seen above is to clone the value:

19.1.4Getting a variable from a function

The return value of a function is moved or copied in the same way we have seen.

In the following example the value returned by foo() is moved into s1:

19.1.5Modifying a value with a function

Using what we have seen above, we are able to write a function that computes a new value and returns it. We can use this function to overwrite (i.e.: we use the variable shadowing feature, see Shadowing a variable) our variable with a new value:

What we have done in the previous example is:

It looks like we have modified a, while all we have done is redefining a new variable of the same name. Both version of the variable a are immutable.

Making the variable mutable, we can reset its value:

We can do the same with a type that allocates memory on the heap:

Since the ownership of the value is passed to foo(), it is also possible to declare the foo’s parameter as mutable so we can modify it in-place: