Setting up a dev container for Rust
- Primary author: Cem Baykal
- Reviewer: Hugh Toomey
Hello! In this tutorial you will learn how to create a "Hello World" project in Rust. You will learn how to setup a Git repository, create a Rust development container, and how to make a simple Hello World program in Rust. Much of this tutorial is inspired by Starting a Static Website Project with MkDocs by Kris Jordan.
Prerequisites
Ensure you have satisfied the following before continuing:
1. A GitHub account which is available at GitHub.
2. Git which can be found at Installing Git
3. Visual Studio Code: Install VS Code
4. Docker Desktop: Install Docker
5. Fundamentals of using the command-line.
Repository Setup
- Local Repository Setup:
- Open either terminal or command-prompt on Mac and Windows respectively.
- Use the command line to create a new directory and go to it by using the following commands:
- Use Git to initialize a new Git repository:
- Let's create a README file to explain our new repository:
- Open either terminal or command-prompt on Mac and Windows respectively.
- Remote Repository Setup:
- Sign into your GitHub account
- Go to new repository and fill in the following:
- Repository Name: comp423-rust-tutorial
- Description: "Hello World in Rust"
- Visibility: Public
- Do not initialize a README, .gitignore, or license.
- Hit "Create Repository"
- Link Local And Remote Repositories:
- Back in the command line add your GitHub repository as a remote where
<yourusername>is your GitHub username using:git remote add origin https://github.com/<your-username>/comp423-rust-tutorial.git - Using
git branchcheck your default branch name. If it is notmainusegit branch -M main. - Push your local changes to the remote:
- If you refresh your repository in your browser you should see your changes. You can use
git logto see the commit ID's and messages of your commits.
- Back in the command line add your GitHub repository as a remote where
Development Container Setup
-
Let's create a Development Container Configuration
- Back in Visual Studio Code, open the newly created
comp423-rust-tutorialdirectory using File > Open Folder. - Navigate to Extensions and install the "Dev Containers" extension.
- Create a
.devcontainerdirectory and add a file nameddevcontainer.jsonwithin. - Add the following to this file:
Extra Info
- name: The label that appears in your VS Code Dev Container environment.
- image: Points directly to an existing Docker image—in this case, the official Rust image on Docker Hub.
- settings: Custom VS Code settings inside the container. For our purposes, we can leave this blank.
- extensions: Lists extensions that will be installed automatically in the container. In this, case we are installing the official rust-analyzer extension.
- postCreateCommand: Runs after the container is created. We do not need to do anything after creation.
- Back in Visual Studio Code, open the newly created
-
Opening Dev Container
- Press
Ctrl+Shift+P on Windows or Cmd+Shift+P on Macthen type and select "Dev Containers: Reopen in Container". Then press enter to reopen your project in a development container.
- Press
Rust!
- Once your development container is open, use the built-in terminal and run
rustc --version. You should seerustc 1.84.0. - In the VS Code terminal, write
cargo new hello_423 --vcs none. - This will create a new directory. Navigate to this folder in your terminal using
cd hello_423. - Go to the
hello_423/src/main.rsfile. Replace this file with: - In your terminal, write
cargo build. This command is equivalent to usinggccto compile a program into an executable file. This command compiles the code then places it into thetarget/debugdirectory. Then, enter./target/debug/hello_423. This runs the file and is equivalent to writing./for a file compiled withgcc. - In your terminal, write
cargo run. You will see the output immediately. This is because this command compiles and automatically executes the compiled program unlikebuild.
Build vs Run
When you use cargo build, Cargo compiles your Rust code and produces an executable or library, but it does not run the resulting binary. This means Cargo simply downloads any missing dependencies, compiles the project, and places the build artifacts in a designated target directory. On the other hand, cargo run not only builds your code—downloading and compiling dependencies as needed—but also executes the resulting binary immediately afterward. It’s a convenient way to streamline development, letting you compile and run your program in a single step rather than manually running the built binary each time.
Finally, CELEBRATE!!! You have successfully made a Hello World program from scratch in Rust!