Ownership in Rust
On this page
A workshop has one key for the forklift. Whoever holds it drives, and nobody else can. The key can be given away for good, or lent for one job and returned. Ownership is this rule in the Rust language: every value, such as a pin, has exactly one owner, and the compiler rejects a program in which two parts could use it at once.
See it
A Rust program holds the pin GP15 in a variable named led. It passes the pin to one function with blink(led). On the next line it passes the same pin to a second function with alarm(led). What happens?
The first line of the program shows how blink takes the pin, and alarm is written in the same way. LedPin stands for the long type name of GP15 set as an output. The token shows who owns the pin when the lines have run. A dashed arrow is a loan that has been returned.
The idea
In C a pin is a number that any part of a program may use. One module sets pin 15 as an output for a lamp, and another sets it as an input. The program is built with no warning, and the lamp stops working.
In Rust the pin is a value with one owner. To pass it to a function is a move: the function becomes the owner, and the old name is empty. To use the old name again is an error, and the compiler, the tool that turns the program into code for the chip, finds it before anything is loaded.
A function that needs the pin for a moment takes a borrow, written &mut: the pin is lent for the length of the call, and the owner has it again afterwards. Only one such borrow can exist at a time. The lesson on ownership, borrowing and hardware builds a program from both.
Ownership has a limit. A value shared between the main program and an interrupt needs a lock as well: see the mutex.
One worked number
Three functions each need the pin GP15 for a moment: blink, alarm and self_test. The main program owns the pin and calls them in turn. How many of the 3 calls does the compiler accept if each function takes the pin by a move, and how many if each takes a borrow?
Show the worked solution, 2 steps
- Follow the moves
blink(led) moves the pin into blink, and this call is accepted. The name led is now empty, so alarm(led) and self_test(led) each use a moved value: 2 errors. With moves, 1 of 3 calls is accepted, and no program is built.
- Change the three functions to borrow
Each call lends the pin and has returned it before the next call starts: 0 errors, and 3 of 3 calls are accepted. The figure shows both cases with two functions.
The mistake to avoid
Check yourself
One line of a program sets pins.gpio15 as an output and stores the result in a. The next line sets pins.gpio15 as an input and stores the result in b. How many of the 2 lines does the compiler accept?
A function switches a relay on, and the caller must be able to switch the same relay off later. How should the function take the pin of the relay?
A program passes every ownership check and is loaded into a controller. How much time and memory does the controller spend on those checks while it runs?
Where you meet it
- A relay that starts a pumpOnly one part of the firmware can hold the output, so a test routine cannot switch the pump while the controller runs it.
- A serial port to a modem or a meterTwo parts that both write to it would mix their messages. With one owner, that program cannot be built.
- Firmware written by several peopleA new module cannot quietly take a pin that an old module uses. The build fails on the day the mistake is made.
- Every value, such as a pin, has exactly one owner.
- A move gives the value away, and the old name cannot be used again.
- A borrow lends the value for one call. Only one borrow that can change it exists at a time.
- The compiler checks all of this. The chip pays nothing.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.