How to Get Started With Rust on Raspberry Pi
Whatsapp Pinterest
Advertisement

If you are interested in programming, you’ve probably heard of Rust 7 Reasons Rust Is the Most Exciting New Programming Language 7 Reasons Rust Is the Most Exciting New Programming Language Want to get started with programming? Here's why Rust is the most exciting and accessible new programming language. Read More . The language, designed by Mozilla, is widely loved by developers and continues to grow in devotees.

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

Setting Up Your Raspberry Pi

For this project you will need:

  • Raspberry Pi.
  • LED.
  • 220-1k Ohm resistor.
  • Breadboard and hookup wires.

A circuit diagram showing an LED and resistor attached to the GPIO 18 and GND pins of a Raspberry Pi

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

This tutorial was made using a Raspberry Pi 3B+ with Raspbian Stretch in desktop mode. It would work perfectly fine through a remote SSH connection too, though different models of Pi and different operating systems might have varying results.

How to Install Rust on Raspberry Pi

The Terminal command required to install Rust

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

The three choices presented by the Rust installer.

The installer will notify you when it is complete, though installation can take some time depending on your connection.

After Installation

Rustc and Cargo are installed, but not currently in PATH

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

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

Testing Rust and Cargo in PATH with their version numbers.

You’ll be compiling and building all of your scripts from the terminal, but you’ll also need a code editor. In this project I’ll be using Code-OSS, a community build of VS Code which you can install on the Pi, but it’s not essential. Any code editor will do.

Creating a Rust Project

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

mkdir YourFolder
cd YourFolder

Use Cargo to create a new Rust project.

cargo new YourProject

You’ll get a confirmation that the new project has been created.
Making a new directory in the terminal and putting a blank rust project in it

Enter the new project folder and list its contents.

cd YourProject
ls

You’ll see a folder named src and a file called Cargo.toml. These two elements make up the basis of every Rust project.

A Simple Rust Project, Explained

A sample hello-world rust script

First, lets open up the src directory, and open main.rs in a code editor. You’ll see that the new project comes complete with a “Hello World” script to get you started.

Rust syntax will be familiar to those who have used C languages or Java before. This differs from Python which uses whitespace, semi-colons and braces to denote code blocks. Rust code must compile and build before it runs.

A blank Cargo.toml file

Back in the project’s parent folder, open up Cargo.toml in a code editor. Anyone who has coded in JavaScript or Ruby will likely find this familiar. Project information, build instructions, and dependencies are all listed in this file. Packages are called Crates in Rust, and we’ll be using one later to access the Raspberry Pi’s GPIO pins.

Building the 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 build the sample project

This creates another folder within your project called target. You will also notice a new file called Cargo.lock. When working with a team or coding something to deploy to a server, this file locks the project to a version that has previously compiled and built successfully. While learning, you can safely ignore this file.

Within the target folder is a subfolder called debug, and this is where your executable file will be. On Mac and Linux, run your project by typing:

./YourProject

On Windows, you will have a new EXE file which you can run by double-clicking.

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

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

Setting Up GPIO Pins

Adding the Rust GPIO Zero crate to the Cargo.toml file

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

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

[dependencies]
rust_gpiozero = "0.2.0"

Save it, and open your terminal. At this stage, there is no point in rebuilding the project as no code has changed. Cargo provides a function which will check that the code will compile and that all dependencies are present.

cargo check

Cargo check will check code for errors and install dependencies

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

Hello Blink

A Raspberry Pi with an LED attached

Now you will change your hello world script into a blinking light script. Begin 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 let the compiler know you are using the rust_gpiozero library, so at the very top of the script add a reference to the library.

use rust_gpiozero::*;

Much like the regular Python based blink sketch, we need a way to add a delay between turning the LED on and off. In Rust, we use two elements of the standard library to do this:

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

Now in your main function, add a variable for your LED pin, and a loop to contain the 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));
}

That’s it! Save your script, and return to the terminal.

Test It Out

An image showing the LED lighting up

Build the project again to update the executable. Alternatively, the run command builds and runs the script in a single step:

cargo run

You should see a blinking LED. Well done! You’ve just made your first hardware program with Rust. Press Ctrl-C to exit back to the terminal. If you have any errors, check over your code thoroughly for any missed colons, semi-colons or brackets.

An Exciting Future With Rust on Raspberry Pi

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

That said, Rust has quite a buzz around it, and there are many reasons why you should learn the language 7 Reasons Rust Is the Most Exciting New Programming Language 7 Reasons Rust Is the Most Exciting New Programming Language Want to get started with programming? Here's why Rust is the most exciting and accessible new programming language. Read More !

Explore more about: Raspberry Pi, Rust.

Enjoyed this article? Stay informed by joining our newsletter!

Enter your Email

Leave a Reply

Your email address will not be published. Required fields are marked *