If you are interested in programming, you must have heard of Rust. The language developed by Mozilla is widely loved by developers and continues to grow among devotees.

The Raspberry Pi is the swiss army knife of small computers and is perfect for learning to code. Let’s combine them and install Rust on Raspberry Pi.

Setting up your Raspberry Pi

For this project you will need:

  • Raspberry Pi.
  • Resistor 220-1k Ohm.
  • Breadboard and connecting wires.

Circuit diagram showing LED and resistor connected to GPIO pins 18 and GND of Raspberry Pi

Set up your circuit with GPIO 18 connected to the positive leg of the LEDs and the negative leg of the LEDs to the resistor and back to the GND pin on the Pi.

This tutorial was made using a Raspberry Pi 3B+ with Raspbian Stretch in desktop mode. It also works fine over a remote SSH connection, although different Pi models and different operating systems may have different results.

How to install Rust on Raspberry Pi

Requires a terminal command to install Rust

To install rust, go to the rust-lang installation page and copy the installation command into your terminal. When prompted, select the default installation.

Three options provided by the Rust installer.

The installer will notify you of completion, although the installation may take some time depending on your connection.

After installation

Rustc and Cargo installed but not currently in PATH

The installation was successful, but you can’t start using it yet. If you try to check Rust and Cargo by version, you will get an error. Usually, you must add the language to your PATH variable in order to use it on the command line.

Luckily, Rust will do this for you, and all you have to do is reboot your Pi or log out and log back in. Now checking for Rust and Cargo should work.

Testing Rust and Cargo in PATH with their version numbers.

You will be compiling and building all your scripts from the terminal, but you will also need a code editor. For this project, I’ll be using Code-OSS, a community build of Code VS that you can install on the Pi, but it doesn’t have to. Any code editor will do.

Creating a Rust Project

To create a Rust project, create a new directory and enter it by typing

mkdir YourFolder cd YourFolder 

Use Cargo to create a new Rust project.

 cargo new YourProject 

You will receive confirmation that a new project has been created.
Creating a new directory in the terminal and adding an empty rust project to it

Enter the new project folder and list its contents.

 cd YourProject ls 

You will see a folder named src and a file named Cargo.toml . These two elements form the core of every Rust project.

A simple Rust project, explained

Hello World Script Sample

First, let’s open the src directory and open main.rs in the code editor. You will see that the new project comes bundled with a «Hello World» script to get you started.

Rust’s syntax will be familiar to those who have used C or Java before. This is different from Python, which uses spaces, semicolons, and curly braces to denote blocks of code. Rust code must be compiled and compiled before running.

Empty Cargo.toml file

Back in the project’s parent folder, open Cargo.toml in the code editor. Anyone who has coded in JavaScript or Ruby will surely find this familiar. Project information, build instructions, and dependencies are listed in this file. The packages are called Crates in Rust and later we will use one of them to access the GPIO pins of the Raspberry Pi.

Create a sample project

Back in the terminal window, make sure you are in your project directory and build the project.

 cargo build 

Using the Cargo Build command to create a sample project

This will create another folder in your project called target . You will also notice a new file named Cargo.lock . When working with a team or writing code to be deployed to a server, this file locks the project to a version that was previously compiled and successfully built. During training, you can safely ignore this file.

The target folder contains a subfolder debug and that’s where your executable will be located. On Mac and Linux, run your project by typing:


On Windows you will have a new EXE- file that you can double-click to run.

The file structure of the built rust project along with the running hello world program.

Success! Let’s turn this project into something that uses GPIO pins.

Setting up GPIO pins

Adding a null Rust GPIO crate to the Cargo.toml file

For this project, we will be using the rust_gpiozero crate by Rahul Thakdoor. While not the only way to access GPIO pins, this box is designed to be similar to the Python GPIO Zero library.

Instead of manually loading the crate, paste its name under the dependencies in the Cargo.toml file.

 [dependencies] rust_gpiozero = "0.2.0" 

Save it and open your terminal. At this stage, it makes no sense to rebuild the project, since the code has not changed. Cargo provides a function that checks that the code will compile and that all dependencies are present.

 cargo check 

Load validation will check the code for errors and install dependencies

Depending on your connection, this may take several minutes, but you only need to do this once, when you add or change items in the Cargo.toml file.

hello blink

Raspberry Pi with LED

Now you will change your hello world script to a blinking light script. Start by opening main.rs in your editor. If you want to skip coding, you can find the finished script on Github Gist.

You need to tell the compiler that you are using the rust_gpiozero library, so add a link to the library at the very top of the script.

 use rust_gpiozero::*; 

Similar to the normal Python-based flashing sketch, we need a way to add a delay between the LED turning on and off. In Rust, we use two elements of the standard library for this:

 use std::thread::sleep; use std::time::Duration; // note the capital D! 

Now in your basic function, add a variable for your LED pin and a loop containing blinking instructions.

 let led = LED::new(18); // sets a variable for the led pin loop{ // starts a loop led.on(); sleep(Duration::from_secs(1)); // creates a 1 second pause led.off(); sleep(Duration::from_secs(1)); } 

This is it! Save your script and return to the terminal.

check it

Image showing how an LED glows

Build the project again to update the executable. Alternatively, the run command creates and runs the script in one step:

 cargo run 

You should see a blinking LED. Well done! You have just made your first hardware program with Rust. Click ctrl-c, to exit back to the terminal. If you have any errors, carefully check your code for missing colons, semicolons, or parentheses.

An exciting future with Rust on Raspberry Pi

Currently, Python is not in danger of being replaced by Rust. It’s easy to learn and Python will have many applications for years to come.

However, there is quite a lot of noise in Rust and there are many reasons why you should learn the language. !

Похожие записи