Setting up a dev container for Rust
- Primary author: Daniel Henderson
- Reviewer: Maxim Chadaev
Prerequisites
Before we dive in, make sure you have:
- A GitHub account: If you don’t have one yet, sign up at GitHub.
- 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.
Part 1. Project Setup: Creating the Repository
Step 1. Create a Local Directory and Initialize Git
- Open your terminal or command prompt.
- Create a new directory for your project or navigate to the directory you would like it to be located in.
- Initialize a new git project.
- Create a
README.md
file and make your first commit.
Step 2. Create a Remote Repository on GitHub
- Log in to your GitHub account and navigate to the Create a New Repository page.
- Fill in the details as needed. Make sure to set your repository name to the same name you used for your directory.
- Click Create Repository.
Step 3. Link your Local Repository to GitHub
- Add the GitHub repository as a remote:
Replace
<your-username>
and<project-name>
with your GitHub username and project name respectively. - Check your default branch name with the subcommand
git branch
. If it's notmain
, rename it tomain
with the following command:git branch -M main
. - Push your local commits to the GitHub repository:
Part 2. Setting Up the Development Environment
Step 1. Add Development Container Configuration
- In VS Code, open your project's directory. You can do this via: File > Open Folder.
- Install the Dev Containers extension for VS Code.
- 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, in this case, the latest version of a Rust environment.customizations
: Adds useful configurations to VS Code like installing the rust-analyzer extension. When you search for VSCode extensions on the marketplace, you will find the string identifier of each extension in its sidebar. Adding extensions here ensures other developers on your project have them installed in their dev containers automatically.
{
"name": "My Rust Project",
"image": "mcr.microsoft.com/vscode/devcontainers/rust:latest",
"customizations": {
"vscode": {
"settings": {},
"extensions": ["rust-lang.rust-analyzer"]
}
}
}
Step 2. Reopen the Project in a VSCode Dev Container
Reopen the project in the container by pressing Ctrl+Shift+P
(or Cmd+Shift+P
on Mac), typing "Dev Containers: Reopen in Container," and selecting the option. This may take a few minutes while the image is downloaded.
Once your dev container setup completes, close the current terminal tab, open a new terminal pane within VSCode, and try running rustc --version
to check if your container is running the latest version.
Part 3. Setting up the Rust Project
Step 1: Initialize the Project
We will now go through the steps for creating a new project in Rust.
- In your dev container's terminal in your project's directory, run
cargo new <subdirectory-name> --vcs none
. The flag--vcs none
(Version Control System) prevents a newgit
repository from being created on your behalf. This will create a new directory. - Navigate into this directory through the terminal to the subdirectory.
- Open
main.rs
and edit the default "Hello World!" program into "Hello COMP423!" as follows: - Save and then run
cargo run
. This will compile and run the program. You should see the outputHello COMP423
in your terminal after.
Note
You can also run cargo build
. This will compile the program into an executible which you can find in the target folder. This is different from cargo run
because it does not immediately run the program after it compiles it. Use cargo build
when you want to send the executible file to others and not run it immediately. This executable file can be found in the target/debug/
directory, and it will be of the same name as <subdirectory-name>
. You can run this file by running ./target/debug/<subdirectory-name>
in your terminal, provided you are in the <subdirectory-name>
directory.
Note
For those who are familiar with the C programming language, cargo build
is Rust's way of compiling the program into bytecode, just like gcc main.c -o my_program
. On the other hand cargo run
does that, as well as running ./my_program
right after.
Step 2: Push to GitHub
Now you can commit and push these changes to github. First, navigate out of the subdirectory.
Then stage your changes and commit them, with an appropriate message, before pushing to your remote repository. Now, you are done!