Skip to content
You Start with one site
Amounts in Start with one site
Embedded Systems · Concept

Ownership in Rust

One idea Intermediate 5 min read
Found this useful?

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.

A reviewer, perhapsReads both modules and notices the same pin number.Depends on a person
A technician, on siteThe lamp does not light, and there is no message.Late and costly
The compiler, at onceThe pin has one owner, so the second use is an error.This is ownership

See it

Predict first

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?

See it: one pin, and two functions that want it

Compiler
Owner of the pin after these lines
Loaded into the chip

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

The question

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
  1. 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.

  2. 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

Ownership is not a check that runs in the device. The compiler does all of the work while the program is built, and the chip receives the same instructions as it would from C. The cost is paid by the programmer, as errors to solve before the first test.

Check yourself

Question 1

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?

Question 2

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?

Question 3

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.
Key points
  • 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.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.