Raspberry Rust

I'm a big fan of Rust, the new systems programming language from Mozilla, and I'm a big fan of the Raspberry Pi, so I thought I'd write a few notes on running Rust on the Raspberry Pi!

Once upon a time installing Rust was a little but messy, but now there's rustup-rs - a Rust install tool written in Rust!

Step 1. Get a Raspberry Pi, boot it up, and get yourself a terminal window.

Step 2. Run this command:

curl https://sh.rustup.rs -sSf | sh

The installer will check out your architecture (armv7l for a Pi 2 or Pi 3, or armv6 for an original Pi) and download a pre-compiled copy of Rust, ready to run.

pi@boron:~ $ curl https://sh.rustup.rs -sSf | sh

info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust programming 
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to Cargo's bin 
directory, located at:


This path will then be added to your PATH environment variable by modifying the
profile file located at:


You can uninstall at any time with rustup self uninstall and these changes will
be reverted.

WARNING: This is beta software.

Current installation options:

    default host triple: armv7-unknown-linux-gnueabihf
      default toolchain: stable
   modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation

<press 1 followed by enter here>

info: syncing channel updates for 'stable-armv7-unknown-linux-gnueabihf'
info: downloading component 'rustc'
 44.7 MiB /  44.7 MiB (100 %)   3.7 MiB/s ETA:   0 s                
info: downloading component 'rust-std'
 61.4 MiB /  61.4 MiB (100 %)   4.0 MiB/s ETA:   0 s                 
info: downloading component 'rust-docs'
  7.5 MiB /   7.5 MiB (100 %)   1.2 MiB/s ETA:   0 s                
info: downloading component 'cargo'
  4.1 MiB /   4.1 MiB (100 %) 933.3 KiB/s ETA:   0 s                
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'rust-docs'
info: installing component 'cargo'
info: default toolchain set to 'stable'

   stable installed - rustc 1.12.1 (d4f39402a 2016-10-19)

Rust is installed now. Great!

To get started you need Cargo's bin directory in your PATH environment 
variable. Next time you log in this will be done automatically.

To configure your current shell run source $HOME/.cargo/env.

Step 3. Write some Rust! First, use Cargo (the Rust package manager) to create you a new 'crate'. A 'crate' is what Rust calls packages, and cargo is how you download crates and build them. Yes, it's all very pun-tastic. Anyway, we use the 'cargo new --bin' command to create a new binary crate - that is, it's a crate that produces a program we can run, as opposed to a library crate we can use in other crates later on.

pi@boron:~ $ source $HOME/.cargo/env
pi@boron:~ $ cargo new --bin my_first_project
     Created binary (application) `my_first_project` project
pi@boron:~ $ cd my_first_project/
pi@boron:~/my_first_project $ cargo run
   Compiling my_first_project v0.1.0 (file:///home/pi/my_first_project)
    Finished debug [unoptimized + debuginfo] target(s) in 8.79 secs
     Running `target/debug/my_first_project`
Hello, world!
pi@boron:~/my_first_project $

Step 4. So what did we just do? Well 'cargo new' create a Hello World example for us, and then 'cargo run' compiled our source code into an executable and then executed it. Let's have a look at the example:
pi@boron:~/my_first_project $ cat src/main.rs
fn main() {
    println!("Hello, world!");
If we'd created a library crate instead of a binary create, it would have created a 'lib.rs' instead of a 'main.rs'.

Step 5. Go learn Rust! Hopefully you've seen that Rust on the Raspberry Pi is very easy to get started with, and there's nothing Pi specific about it. Go and look at the Book, and the API guide, the User Forum, the /r/rust Reddit. If you've got 30 minutes, I've got a brief presentation on Rust from the point of view of a C programmer, and there are many more on YouTube. Stop wasting precious CPU cycles on the Python interpreter and stop writing buggy C programs full of hard-to-find race hazards and buffer overflows - Rust ftw!


  1. Awesome to see you using some rust on an RPi! I used this recently: https://github.com/jamesmunns/pigpio-i2c-rs to do some sniffing of I2C data using Rust, and its been working flawlessly so far.


Post a Comment

Popular posts from this blog

Where next for the Monotron

Embedded Rust in 2018

I decided to make an 1980's Home Computer in Rust - Part 1