Skip to content

Setting up a dev container for Rust:

Welcome to this step-by-step guide for setting up a Rust project in a Dev Container! Hopefully by the end of this tutorial you'll have a fully functional Rust development environment and a simple Rust program!

Prerequisites


Required tools

Before we begin, make sure you have the following:

  1. Git installed: Install git if you don't already have it.
  2. Visual Studio Code(VS Code): Download and install it from here.
  3. Docker installed: Required to run the dev container. Get Docker here.
  4. Command-line basics: Basic knowledge of terminal commands and Git.

Set-Up Guide


STEP 1. Create a Local Directory and Initialize Git

(A) Open your terminal or command prompt.

(B) Create a new directory for your project. (Note: Of course, if you'd like to organize this tutorial somewhere else on your machine, go ahead and change into that parent directory first. By default this will be in your user's home directory.):

mkdir rust-hello
cd rust-hello
(C) Initialize a new Git repository:
git init

STEP 2. Setting Up the Development Environment

(A) In VS Code, open the rust-hello directory. You can do this via: File > Open Folder.

(B) Install the Dev Containers extension for VS Code.

(C) Create a .devcontainer directory in the root of your project with the following file inside of this "hidden" configuration directory: devcontainer/devcontainer.json

The devcontainer.json file defines the configuration for your development environment. Here, we're specifying the following:

  • name: A descriptive name for your dev container.

  • image: The Docker image to use. For this tutorial, we'll be using a base image of Rust from Microsoft.

  • customizations.vscode.extensions: list of Visual Studio Code extensions to be installed in the development container. Extensions enhance your productivity by adding features specific to your development language or tools. For Rust, the rust-analyzer extension is essential, providing capabilities such as code completion, inline documentation, and real-time diagnostics.

  • customizations.vscode.settings: Visual Studio Code-specific settings for customizing your workspace environment inside the container. In this example, the rust-analyzer.cargo.autoReload setting is enabled to ensure that the Rust Analyzer automatically reloads the Cargo workspace when files are added, removed, or updated.

    {
      "name": "Rust Dev Container",
      "image": "mcr.microsoft.com/devcontainers/rust:latest",
      "customizations": {
        "vscode": {
          "extensions": [
            "rust-lang.rust-analyzer"
          ],
          "settings": {
            "rust-analyzer.cargo.autoReload": true
          }
        }
      }
    }
    

Why a Dev Container?

Dev Containers provide a consistent, reproducible development environment, reducing setup time and dependency issues.

STEP 3. Build and Open the Dev Container

(A) Open the project in VS Code.

(B) Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P) and select Dev Containers: Reopen in Container.

(C) You can verify Rust is installed by running:

rustc --version

Expected Output

plaintext rustc 1.x.x (or newer)

Hello COMP423 Project


STEP 4. Initialize a Rust Project

Use cargo to create a new binary project.

cargo new hello-comp423 --vcs none
cd hello-comp423

Avoid Git Reinitialization

The --vcs none flag prevents cargo from creating a new Git repository since you already initialized one earlier."

STEP 5. Writing the Program

Edit the src/main.rs file to print "Hello COMP423" to the terminal:

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

STEP 6. Build and Run the Program

(A) Run the following command to compile the Rust program:

cargo build

Where's the binary?

The compiled binary is located in the target/debug directory. To run it manually, execute: ./target/debug/hello-comp423

(B) Instead of building and running separately, we can use the cargo run command to run the program directly.

cargo run

What's the difference between build and run?

  • cargo build: Compiles the project without running it.
  • cargo run: Builds the project (if necessary) and immediately runs the resulting binary.

Final Words


Congratulations! 🎉 You've successfully set up a Rust development environment in a Dev Container and created a program that prints "Hello COMP423." You now have the foundation to build and explore more Rust projects.

Next Steps

  • Experiment with Rust features like modules, traits, and ownership. - Try using cargo test to write and run unit tests.

Sources