Setting Up a Dev Container for Rust
- Primary author: Caitlin Estrada
- Reviewer: Sanjana Gopalswamy
Prerequisites
- Visual Studio Code with the Microsoft Dev Containers extension
- Docker installed for creating and managing containers
Step 1: Create Blank Git Repository and Initialize
-
Open the terminal and create a new directory for your project:
-
Initialize a new Git repository:
Step 2: Create a new Dev Container project
- Create a
.devcontainer
directory in the root of your project with the following file inside of this "hidden" configuration directory:.devcontainer/devcontainer.json
Step 3: Dev Container Configuration
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: A base image from Microsoft
- extensions: Add the
rust-analyzer
VSCode plugin by the Rust Programming Language Group - version: Show the
rustc --version
to prove a recent version of Rust
// A sample devcontainer.json
{
"name": "Rust Development",
"image": "mcr.microsoft.com/vscode/devcontainers/rust:1",
"customizations": {
"vscode": {
"settings": {},
"extensions": ["rust-lang.rust-analyzer"],
}
},
"postCreateCommand": "rustup update && rustc --version"
}
Rust version
The most recent Rust version is 1.83.0
Step 4: Create a new rust project
- Open a new terminal
- Create a new project using Cargo:
- Navigate into your project directory:
Step 5: Write a basic "Hello COMP 423" program
Step 6: Compile
Build your project using Cargo:
What does this command do?
It compiles your project's source code into an executable binary (if it's a binary project) or a library (if it's a library project).
Step 7: Run
Run the project using Cargo:
Expected Output
Bug
Verify that this is your output to ensure you followed the tutorial correctly!