Build Rust Apps With Dev Containers | by Andy Watt | Feb, 2022

Have you been thinking about trying Rust? Get started in seconds with VSCode and Devcontainers!

Andy Watt
The rusty remains of a ship
Photo by Michael on Unsplash

Rust is the new kid on the block, and it is currently — and quite rightly — generating a lot of hype. There are a lot of reasons to like Rust, but I think that they are well covered in many other articles. Instead of waxing lyrical about Rust itself, I’ll use Rust to further show that Dev Containers with VS Code are very powerful tools that I think all developers should have in their toolbox!

I’m guessing that while many of you readers of technical blog posts will have heard of Rust. But I would also guess that not that many have installed the tooling on your machines and started to have a play with Rust?

This is not going to be a Rust language tutorial — that are many of those out there! This article will instead show how to set up a developer environment for Rust, using Visual Studio Code, and Docker. I’ll demonstrate how to build a fully-featured Rust development environment in a Docker container so you can get started without having to install anything Rust-specific on your machine.

To illustrate the point, I’ll build up a simple API example using Acitx. Of course, it wouldn’t be any fun if I didn’t also add in some testing, so I’ll get to that as well!

As per my previous tutorials on the topic, the prerequisites on your machine are the following:

The code for this can be found here. Clone the repo if you want to skip to the end!

Photo by Marie P on Unsplash

Start by creating an empty folder, cd into it, and then open VSCode. Copy and paste this into your terminal, or do it the old-fashioned way through explorer.

mkdir rust-devcontainer; 
cd rust-devcontainer;
code .;

When VSCode opens, hit F1, and search for Add Development Container. Note: you need to have installed the extension.

Search for ‘Rust’ and add the container config files.

Choose the default version (buster at the time of writing).

Skip any additional features, and hit ‘OK.’

When the config files are automatically added, VSCode will show a pop-up that lets you open the container. Click on ‘Reopen in Container’ to open the environment in Docker.

Note: if the pop up does not appear, hit F1 and search for ‘reopen in container’

Prove to yourself that you are now running in a container — with all of the Rust tooling installed — by opening a terminal and checking that the Rust tools are available:

Once you are in the Rust environment, setting up a simple API is pretty straightforward. Start by initialising a new Rust project by opening the terminal in VSCode and typing the following command:

cargo init

To show that we really are up and running with a rust environment already, go to the srcmain.rs file, and add the following:

fn main() {
println!("Hello World!");
}

Then, in the VSCode terminal, type this:

cargo run

That’s it! We have a fully-featured Rust development environment up and running, and we’re ready to build any number of apps.

Next, let’s build a demo API using Actix.

Add a dependency for Actix in the cargo.toml file.

Note: I’m using a beta version for now.

[dependencies]
actix-web = "4.0.0-beta.21"

Then, delete the ‘hello world’ example and add this to the srcmain.rs file:

To make sure it works, open the terminal, type

cargo run

This will start the API running on http://127.0.0.1:8080

And if you go to that URL in a browser, you’ll see that it is behaving as expected.

All good so far, and we probably could call it a day at this. But, I’m never satisfied until there is at least one unit test running, so let’s do that.

Photo by JESHOOTS.COM on Unsplash

Back in the srcmain.rs file, add in the following code underneath the logic:

And run the test by going into the terminal in VSCode and typing the following:

cargo test

You should see the test passing!

This is a usable test setup, but I do like a test explorer so that VSCode feels more like a ‘real IDE’. As ever, the community has provided one.

In the file .devcontainerdevcontainer.json add two additional lines to the extensions property

"swellaby.vscode-rust-test-adapter",
"ms-vscode.test-adapter-converter"

You will need to exit and reenter the remote container to see the changes (F1, back to WSL, then back to the devcontainer). When you have reloaded the environment, you’ll see the familiar VSCode test explorer. You can run the tests directly from explorer!

Assuming that all is well, you can navigate and run the tests from the VSCode test explorer.

I think this is a good place to stop for now. We have set up a fairly comprehensive Rust development environment with a basic API, a unit test example, and some of the basic Rust extensions for VSCode installed.

Because this is all configured using devcontainers, this setup can be shared among a team very easily to get everyone up and running with a consistent Rust dev environment in literally seconds — just as long as it takes to clone a repo!

I hope this was a useful read for those of you who made it to the end! I hope that I have converted at least a few new ‘Rustaceans’!

Want to Connect?You can get in touch on Linkedin.

Leave a Comment