Welcoming RUST to the world of IoT

Reading Time: 3 minutes

Breaking away the stereotypes of a blog, let’s start with a question. What’s our future? If I was asked this question, it has to be IoT.

Internet of Things has already taken the technology world by a storm and is continuing to do so. While learning about IoT, its uses and all that stuff, a desire to make my own IoT framework arose.

I started to search for a language which can be programmed into an IoT device and Rust knocked me down like a bolt of lightning(metaphorically!)

Image result for raspberry pi rust
Raspberry Pi: A well known IoT device.

Rusting of Raspberry Pi

Raspberry Pi is the most known IoT device to the beginners. Some more complex IoT devices include Google Home, Amazon Alexa and the new Nescafe E for making your coffee experience smarter.

Now coming back to the Rust, let’s see(actually read!) how can we make our Rust code to work with an IoT device. In this blog, I will be sharing about Raspberry Pi-Rust integration for a Linux platform.

Step[0]: Installing toolchain

Cross-compiling the rust code into the Raspberry Pi requires a toolchain. The toolchain can be downloaded by typing the following command on your Linux terminal.

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

This command downloads a shell script file from the sh.rustup.rs and the file is run using the pipe symbol ( | ) which is used to install the rust toolchain.

If Rust is already installed on your system, you would get the following error:

info: downloading installer
error: it looks like you have an existing installation of Rust at:
error: /usr/bin
error: rustup cannot be installed alongside Rust. Please uninstall first
error: if this is what you want, restart the installation with `-y'
error: cannot install while Rust is installed

If this error occurs, run this command first and then the previous command.

sudo apt remove rustc

And finally the main toolchain command

rustup target add armv7-unknown-linux-gnueabihf

This will download a 53.0 MB file to your system which is the toolchain for cross compilation and you will get the following output:

info: downloading component 'rust-std' for 'armv7-unknown-linux-gnueabihf'
 53.0 MiB /  53.0 MiB (100 %) 567.3 KiB/s ETA:   0 s                
info: installing component 'rust-std' for 'armv7-unknown-linux-gnueabihf'

Step[1]: Getting the C compiler

In order to run you code on Raspberry Pi, you need a C compiler to compile the program. You can install the C Compiler using the command:

sudo apt-get install gcc-4.7-multilib-arm-linux-gnueabihf

Step[2]: Adding the right Linker

In this step, we would add a linker for the ARM architecture. For that you need to go to Home directory and type the following command.

cd .cargo
touch config
gedit config

This will make a new file named config in the .cargo directory inside the Home directory. Now, add the target to the file by concatenating the file with this:

linker = "arm-linux-gnueabihf-gcc-4.7"

Step[3]: A Sample Code

Create a new project and enter into the newly made directory.

cargo new factorial
cd factorial
fn factorial(num: u64) -> u64
    let mut i:u64 = num;
    let mut response:u64 = 1;
    while i>1 {
                  response = response*i;
                  i = i-1;

fn main() {

Step[4]: Editing Cargo.toml

Cargo.toml file needs to be added with openssl dependency with a vendored feature and will look like this:

name = "factorial"
version = "0.1.0"
authors = ["mudit <mudit.chhabra@knoldus.in>"]
edition = "2018"

openssl = { version = "0.10", features = ["vendored"] }

The Final Step: Building the project for Raspberry Pi

The project can be build using –target option with the cargo build command.

cargo build --target=armv7-unknown-linux-gnueabihf

You can check if the project is compiled according to the Raspberry Pi Architecture by checking this directory:


Now you are ready to run this file on Raspberry Pi.

This would give the following output on Raspberry Pi

 Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/fibonacci`

Although the target folder is bulky and is not pushed to GitHub but in this project the Raspberry Pi binary is present in the target folder. So I have pushed the target folder as well.

The example project is here for reference.