Setting up a dev container for Rust¶
- Primary author: Reece Corter
- Reviewer: Benjamin Corter
Overview
This tutorial will help you: - Create a blank directory and initialize a new Git repository into it - Set up a Dev Container with the official Rust image and 'rust-analyzer' - Write a basic program that will print Hello COMP423 - Understand the difference between 'cargo build' and 'cargo run' and their uses
Prerequisites¶
- Visual Studio Code
- Docker Desktop
- Basic Git knowledge
No Rust install on Host
DO NOT install Rust locally. We will install & run everything in a dev container.
Step 1: Create a Blank Directory & Initialize Git Repo¶
Step 2: Dev Container Configuration¶
- Create a folder named
.devcontainerand navigate into it:
- Create a file named
devcontainer.jsonwith the following contents:
{
"name": "Rust DevContainer",
"image": "mcr.microsoft.com/devcontainers/rust:1",
"customizations": {
"vscode": {
"settings": {},
"extensions": ["rust-lang.rust-analyzer"]
}
}
}
Step 3: Reopen in Dev Container¶
- In Visual Studio Code, open the
rust-hellofolder. - Press Ctrl + Shift + P & select Dev Containers: Reopen in Container.
- Wait for the container to build.
When completed, run:
You should see something like `rustc 1.x.x (yyyy-mm-dd)'. (1.84.0 as of 2025-01-09)
Step 4: Create a New Rust Project¶
From inside the Dev Container terminal:
- '--bin' creates a binary (executable) project.
- '--vcs none prevents Cargo from creating another Git repo inside 'hello-comp423'.
Step 5: Write "Hello COMP423" Program¶
Open 'hello-comp423/src/main.rs'. Replace any existing code with:
Step 6: Build & Run Program¶
Option A: Build & Run Manually¶
This should output:
Relation to 'gcc' in COMP211
'cargo build' is similar to using 'gcc hello-comp423.c -o hello-comp423': it compiles the program but does not run automatically.
Option B: Build & Run in One Step¶
This command compiles (if needed) and executes the program. You will see:
Difference from 'cargo build'
- 'cargo build': only compiles your program.
- 'cargo run': compiles and immediately runs the compiled file in a single command.
Summary¶
- Step 1: Create a new folder, git init.
- Step 2: Add '.devcontainer/devcontainer.json' with the Rust base image + 'rust-analyzer'.
- Step 3: Reopen in container, verify 'rustc --version'.
- Step 4: Create project with cargo new --bin --vcs none hello-comp423.
- Step 5: Write "Hello COMP423" in main.rs.
- Step 6: Compile with cargo build and run the resulting file manually (Option A), or use cargo run (Option B) to do it all in one step.