# Crafting a 'Hello World' in Rust for the ESP-32: A Step-by-Step Guide

Jumping into embedded programming with Rust is a daunting challenge. I’ve written this blog post to help get you started. I’ll be showing you how to get the most basic of code, the ‘Hello world!’ running on the ESP-32, in Rust.

To write this post I’ve distilled information from [The Rust on ESP Book](https://docs.esp-rs.org/book/) (licensed under [CC-BY-SA v4.0](https://creativecommons.org/licenses/by-sa/4.0/legalcode))[, and I highl](https://creativecommons.org/licenses/by-sa/4.0/legalcode)y recommend giving it a read, especially if this topic interests you, as it trumps this blog post in detail and thoroughness.

## Setup

Plug your ESP-32 in to a USB port on your machine. A light should turn on and you should now be able to see the serial port in your terminal.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736092715471/7a72af36-9ef0-4aa3-9558-65bc2473ee26.jpeg align="center")

```bash
ls /dev/ttyUSB0
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736092622768/bceefe46-880c-4ddf-af9f-b4db39ad3d6a.png align="center")

your serial port name *may* be different here, this just happens to be mine.

## Configure Your Host System

add yourself to the tty and dialout group. You’ll need this to be able to interact with the board over its serial connection.

```bash
sudo usermod -aG dialout <youruser>
sudo usermod -aG tty <youruser>
```

After this, logout and log back in to your machine, check that you are a member of these groups with `groups`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736092606530/083a2207-af62-4b2c-a413-ffe4b4d71118.png align="center")

On your host system install cargo generate. We’ll be using this to generate a template, meaning all the extra code and configuration we need for the ESP-32.

```bash
cargo install cargo-generate
```

Note: on my system, I had to install `openssl-dev` on my host to compile past errors.

## Generate Your Code

Now generate the required template for our system.

```bash
cargo generate esp-rs/esp-idf-template cargo
```

you'll see a TUI menu, set the project name to `hello_world`, the MCU to `esp32`, and don’t configure advanced options.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736092585902/d871e544-fda6-4976-a7a7-bf5b4dbc0dd7.png align="center")

You can read this section of [the tutorial](https://docs.esp-rs.org/book/writing-your-own-application/generate-project/esp-idf-template.html) I mentioned above if you want to know more about the generator.

You should now see some new files! The most important of which is `src/`[`main.rs`](http://main.rs).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736092577180/95783472-41a4-404d-864f-5859fbfddd6d.png align="center")

## Create Build Environment

Now we need to create the build environment. We’ll be using the container provided by [espressif](https://hub.docker.com/r/espressif/idf-rust/), since it already has all the configurations and packages requires.

Change directories into your generated project, pull the container, and start it:

```bash
cd hello_world
docker pull espressif/idf-rust:all_1.83.0.1
docker run -it -v $PWD:/home/esp/hello_world  espressif/idf-rust:all_1.83.0.1 bash
```

the above docker command will start an interactive shell inside the container, and mount a volume of your generated project inside of it.

You should have now have a shell inside the container:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736092566806/91f005e0-2e79-4deb-a1f1-b49fe4db63a7.png align="center")

## Build, Flash and Run, Code

In the *container shell*, cd into the `hello_world` directory and build the code as you would any other Rust project:

```bash
cd hello_world
cargo build
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736092557888/08d58820-09d3-42d3-a080-b638cd29cda4.png align="center")

After some time, your build will finish.

Back on the hosts shell, install espflash. This is a neat little tool that makes flashing the ESP32 as easy as 01,10,11 :)

```bash
cargo install espflash
```

For install errors, I had to install `systemd-devel` for the this to work.

Now that we're installed, let's flash our code to the board!

```bash
espflash flash target/xtensa-esp32-espidf/debug/hello_world --monitor
```

This command will both flash to the board, as well as monitor on the serial port for any logging.

The ESP-32 will automatically run your code on boot, so you wont need to do anything else here.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736092538038/9ff9c8b1-21d8-4423-97f3-f6bec65ab922.png align="center")

Success! Now you can play around on your own here. I personally just like to set up an infinite loop for fun :)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1736092525722/24b0203a-3fe6-45c4-8bc2-5adefc943f61.png align="center")

## Next Steps

Now that you have a system set up to run Rust code on the ESP-32, the world is your oyster! I urge you to find more examples to broaden your horizon, or just experiment a little! Take a look at the [**esp-rs**](https://docs.esp-rs.org/) website for some other reccomendations, and good luck! 🫡

## Footnote

This post could not have been written without help from [The Rust on ESP Book](https://docs.esp-rs.org/book/). I’ve distilled information from that book, as well as included some of my own. That book is licensed under [CC-BY-SA v4.0](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
