Contents

Getting started with Rust on Raspberry Pi

In this blog post, we will learn how to get 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 language of their choice.

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 setup your Raspberry Pi, you can follow the instructions in earlier post - Setup Raspberry Pi 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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 to use a script available for us to install Rust with Rustup and Cargo on Raspberry Pi.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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"
Choosing installation option
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:

1
2
3
4
5
6
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:

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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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 Program

I prefer to keep my projects in a directory workspace in my home directory:

1
2
mkdir ~/workspace
cd ~/workspace

Let’s create a simple Rust program to print “Hello World!” to the console.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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:

1
2
3
4
5
6
7
8
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:

1
2
3
4
5
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 setup a sophisticated Rust development environment on the Raspberry Pi. So, stay tuned!