Getting started with Rust on Raspberry Pi

Introduction

Rust is a modern programming language that is blazingly fast, memory-efficient, and type-safe. It is a great language for systems programming, and it is used by companies like Mozilla, Dropbox, and Cloudflare.

Raspberry Pi is a low-cost, credit-card-sized computer that plugs into a computer monitor or TV and uses a standard keyboard and mouse. It is a capable little device that enables people of all ages to explore computing and to learn how to program in the language of their choice.

In this blog post, we will learn how to get started with Rust on Raspberry Pi. Let's get started.

Prerequisites

In terms of hardware, you will need the following:

  • Raspberry Pi 4 (4GB RAM) (already setup with Raspberry Pi OS)
  • Keyboard and Mouse
  • Monitor or TV

If you haven't already set up your Raspberry Pi, you can follow the instructions in the earlier post-set up Raspberry Pi 4 without an external monitor. If you don't have a Raspberry Pi, you can still follow along using a virtual machine. 

Let's ensure that our Raspberry Pi is up-to-date.

gaurav@RPi4:~ $ uname -a
Linux RPi4 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr  3 17:24:16 BST 2023 aarch64 GNU/Linux

gaurav@RPi4:~ $ sudo apt update && sudo apt upgrade -y
[sudo] password for gaurav:
Hit:1 http://archive.raspberrypi.org/debian bullseye InRelease
Hit:2 http://raspbian.raspberrypi.org/raspbian bullseye InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following package was automatically installed and is no longer required:
  libfuse2
Use 'sudo apt autoremove' to remove it.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Installing Rust

The Rust documentation suggests using a script available for us to install Rust with Rustup and Cargo on Raspberry Pi.

gaurav@RPi4:~ $ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | 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.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/gaurav/.rustup

This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:

  /home/gaurav/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /home/gaurav/.cargo/bin

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

  /home/gaurav/.profile
  /home/gaurav/.bashrc

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

Current installation options:

   default host triple: aarch64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

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

info: profile set to 'default'
info: default host triple is aarch64-unknown-linux-gnu
info: syncing channel updates for 'stable-aarch64-unknown-linux-gnu'
info: latest update on 2023-06-01, rust version 1.70.0 (90c541806 2023-05-31)
info: downloading component 'cargo'
  6.7 MiB /   6.7 MiB (100 %)   1.3 MiB/s in  4s ETA:  0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 13.6 MiB /  13.6 MiB (100 %)   3.7 MiB/s in  4s ETA:  0s
info: downloading component 'rust-std'
 32.8 MiB /  32.8 MiB (100 %)   2.9 MiB/s in 11s ETA:  0s
info: downloading component 'rustc'
 75.5 MiB /  75.5 MiB (100 %)   3.4 MiB/s in 24s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 13.6 MiB /  13.6 MiB (100 %)   1.6 MiB/s in  7s ETA:  0s
info: installing component 'rust-std'
 32.8 MiB /  32.8 MiB (100 %)   5.8 MiB/s in  5s ETA:  0s
info: installing component 'rustc'
 75.5 MiB /  75.5 MiB (100 %)   6.3 MiB/s in 11s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-aarch64-unknown-linux-gnu'

  stable-aarch64-unknown-linux-gnu installed - rustc 1.70.0 (90c541806 2023-05-31)

Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

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

Note that you will be prompted to choose an installation option. You can press the return key or enter 1 to continue with the defaults, as I did. Of course, you can choose other options as needed.

Notice the information at the end of the command execution.

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

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

You may choose to restart, but I will source the env file to continue.

gaurav@RPi4:~ $ source "$HOME/.cargo/env"

Once done, we can check the version of the components installed.

gaurav@RPi4:~ $ rustup -V
rustup 1.26.0 (5af9b9484 2023-04-05)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.70.0 (90c541806 2023-05-31)`

gaurav@RPi4:~ $ rustc -V
rustc 1.70.0 (90c541806 2023-05-31)

gaurav@RPi4:~ $ cargo -V
cargo 1.70.0 (ec8a8a0ca 2023-04-25)

Our First Rust Program

I prefer to keep my projects in a directory `workspace` in my home directory.

mkdir ~/workspace
cd ~/workspace

Let's create a simple Rust program to print " Hello World!" to the console.

gaurav@RPi4:~/workspace $ cargo new --bin hello-world
     Created binary (application) `hello-world` package

gaurav@RPi4:~/workspace $ cd hello-world/
gaurav@RPi4:~/workspace/hello-world $ tree .
.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files

Also, take a look at the contents of the `main.rs` file.

gaurav@RPi4:~/workspace/hello-world $ cat src/main.rs
───────┬────────────────────────────────────────────────────────────────────────────────────
       │ File: src/main.rs
───────┼────────────────────────────────────────────────────────────────────────────────────
   1   │ fn main() {
   2   │     println!("Hello, world!");
   3   │ }
───────┴────────────────────────────────────────────────────────────────────────────────────

Now, let's run our program.

gaurav@RPi4:~/workspace/hello-world $ cargo run
   Compiling hello-world v0.1.0 (/home/gaurav/workspace/hello-world)
    Finished dev [unoptimized + debuginfo] target(s) in 1.33s
     Running `target/debug/hello-world`
Hello, world!

Congratulations! You have successfully run your first Rust program on the Raspberry Pi. 🥳

Conclusion

In this article, we learned how to install Rust on the Raspberry Pi. We also created a simple Rust program and ran it on the Raspberry Pi. In the next article in this series, we will learn how to set up a sophisticated Rust development environment on the Raspberry Pi. So, stay tuned!


Similar Articles