How to Develop Rust Applications in Linux Containers

Discover practical tips, installation processes, and development techniques to enhance your coding journey in the Rust ecosystem with out guide.

Subscribe

Subscribe

How to Install Rust on Linux and Develop Applications
6:47

Modernize Your Cloud: Developing Rust Applications in Linux Containers

Cloud modernization is a crucial step for businesses aiming to enhance efficiency, scalability, and performance. In this post, we'll explore how developing Rust applications in Linux containers can be a game-changer in this process.

By the end of this article, you should understand how to:

  • Create a Linux container with Rust pre-installed
  • Attach Visual Studio code to the running container
  • Build and run your Rust application inside the container

 

Why Rust and Linux for Cloud Modernization?

Using Linux containers to develop software can save you time and effort. Developing software in containers gives you near-instant startup times, faster iteration, standardized tooling for local and remote deployment. Containers help you to bring organization to the chaos that is software development.

Rust, known for its performance and safety, is an excellent choice for cloud-based applications. When combined with the power of Linux containers, it provides an isolated, efficient environment for application development.

This combination is particularly potent and offers a robust platform for building and deploying modern applications.

stratusphere by stratusgrid 1

How to Set Up Your Development Environment

There are a few different software tools that we’ll need to install locally, in order to set up a containerized development environment. Let’s briefly list those, and then dive further into each of them.

As you can see, we don’t need all that many tools. We just need to know how to use them! After installing the tools above, move onto the next section where we’ll launch a Rust container, using Docker CLI.

Developing Rust Applications in Linux Containers

1. Launch Rust container

One of the benefits of using containers to develop and deploy software, is that pre-built container images exist with the necessary components for your language or framework. Rust is no exception. On the Docker Hub, a registry that hosts a vast, searchable array of container images, there is an official Rust container image with the toolchain pre-installed.

By simply running a container, using the Rust container image, you can skip the step of “installing” the Rust toolchain locally. Even better, the container has Rust installed on its own, isolated filesystem, which means your host filesystem is protected from dependency conflicts.

Fire up a terminal session and run the following command.

docker run --interactive --tty --restart=always rust

This command will spawn a Bash shell inside a new container, based on the Rust image. You can leave the interactive shell running, or you can detach from it using the standard Docker keyboard shortcut: CTRL + P, followed by CTRL + Q.

By setting the restart policy to always, you ensure that the Rust container automatically starts back up when you restart your development workstation.

2. Connect to Rust container from VSCode

Now that you have a Rust container running locally, you can connect to it from Microsoft Visual Studio Code.  Open VSCode and then open the Command Palette, typically using F1 or CTRL+SHIFT+P keyboard shortcuts. Search for the command “Dev Containers: Attach to Running Container,” and hit ENTER.

Now you can create a project directory with the Cargo CLI tool. Notice that we don’t have to “install” the Rust toolchain, because the Rust container image already includes it. You can open the Integrated Terminal in VSCode by using the CTRL+` keyboard shortcut.

cargo new sample-app

Now you can open VSCode into that directory by using the Open Folder command. By default, the Open Folder command is mapped to the CTRL+K, CTRL+O keyboard sequence. You’ll notice the VSCode window reload, but this should only take a brief moment.

3. Install Rust analyzer extension into container

Once you're connected to a development container in vs code, you can install VSCode extensions into that container’s context. Although the Rust container image includes the Rust toolchain, it doesn't include any additional components specific to VSCode.

  1. Navigate to the Extensions marketplace (CTRL+SHIFT+X)
  2. Search for “Rust Analyzer
  3. Click the Install in Container button

Once you’ve installed the Rust Analyzer extension into the Rust container, you should have advanced development features available, including: auto-completion, syntax highlighting, refactoring, and much more.

4. Customize and run Rust application

Now that our environment is all set up, you can edit your application code in src/main.rs and then build / test your Rust application. Let’s install the “uuid” crate and change our main.rs file to something like the following, or feel free to be creative!

cargo add uuid --features=all
 

Main.rs

fn main() {

    println!("{}", uuid::Uuid::new_v4().to_string());

}

This simple program will print out a Universal Unique Identifier (UUID) every time you run it. It’s nothing fancy, but at least it shows something different on each launch! Now let’s try to run the program from the VSCode Integrated Terminal again.

We could just use the cargo run command, but let’s compile a slimmed-down release build, and then run it.

cargo build --release

./target/release/sample-app
 

You should see a UUID printed out to the terminal, similar to the screenshot below.

How to Develop Rust Applications in Linux Containers 2

And That’s How You Develop Rust Applications in Linux 

In this article, we learned how to use the Rust toolchain inside of an isolated Linux container, rather than installing the toolchain on our host operating system. This layer of isolation protects the Rust toolchain from other applications that might step on its toes, and prevents the Rust toolchain from having accidental impact on other applications.

If you need to develop applications using different versions of the Rust toolchain simultaneously, then using Linux containers can help you accomplish that easily. Some Rust crates may require that you use the cutting edge nightly version of the Rust toolchain, rather than the current release version. Other projects you’re developing may require you to use an older Rust toolchain, due to breaking changes.

StratusGrid is Your Partner in Cloud Modernization

If you’d like StratusGrid’s team of high-caliber engineers to help you build and optimize software and/or automation tools in the cloud, please reach out to us through our contact form. Make sure you subscribe to our blog for more, similar articles!

BONUS: Find Out if Your Organization is Ready for The Cloud ⤵️

FinOps

Similar posts