From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Transferring ownership

Transferring ownership - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Transferring ownership

- The distinction between stack and heap data and whether values are copied or moved also comes into play when passing variables as input arguments to functions. In the main function shown here, we declare an integer variable named rocket_fuel on line two. Pass it as an input argument to the process_fuel function and then print its value afterwards on line four. The process_fuel function accepts that input as a parameter named propellant and then simply prints a message with it. When I run this program, we see the print statement from the process_fuel function first, followed by the print statement on line four. Pretty straightforward. What's not obvious here, is how the value of rocket_fuel is being passed to the process_fuel function. Since rocket_fuel is an integer, which lives on the stack, when it gets passed as an input argument, it passes a copy of its value. The propellant variable takes ownership of that…

Contents