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!
- Primary author: Nicholas Nguyen
- Reviewer: Logan Gatza
Prerequisites
Required tools
Before we begin, make sure you have the following:
- Git installed: Install git if you don't already have it.
- Visual Studio Code(VS Code): Download and install it from here.
- Docker installed: Required to run the dev container. Get Docker here.
- 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.):
(C) Initialize a new Git repository: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, therust-analyzerextension 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, therust-analyzer.cargo.autoReloadsetting is enabled to ensure that the Rust Analyzer automatically reloads the Cargo workspace when files are added, removed, or updated.
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:
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.
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:
STEP 6. Build and Run the Program
(A) Run the following command to compile the Rust program:
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.
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.