Ownership and Functions

This lesson discusses how ownership works by using functions.

As discussed in the previous lesson, the assignment of a variable to another variable will copy or move it. In case of passing variables to the functions, similar can happen.

When a variable whose memory is allocated on heap goes out of scope, the value will be cleaned up by drop unless the data has been moved such that it is now being owned by another variable.

Passing Values to a Function

The ownership of the variable is

  • Copied if the value is a primitive data type so the variable can be reused after the function call
  • Moved if the value is a non-primitive data type so the value becomes inaccessible after the function call

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy