Setting up a dev container for Rust
- Primary author: Mann Barot
- Reviewer: Daniel Zhang
Rust
Created in 2012, Rust is a modern programming language used to create reliable software.
Pre-requisites
Installs
Remember
- Rust will be installed in the container, not the host machine
- Ensure the "Dev Containers" extension is installed in VS Code
Steps
-
On a folder on your machine, create a folder using the terminal
Use
git initto initialize a git repository. Use this to create a README: -
Open this folder in VS Code
- Create a folder named ".devcontainer" (for simplicity) and add a devcontainer.json file in it. Include the following in it:
What was that?
Your dev container .json file communicates to the container what needs to be setup. In this file, you give it a name (RustExample), install the latest image of Rust, and add a rust analyzer extension
-
In the .json file, press Ctrl+Shift+P, type and click "Dev Containers: Rebuild & Reopen Container (just rebuild container is fine too). This might take a minute. (Not working? Ensure Docker is running)
-
Once container starts, open a terminal and use the following to ensure you have the most recent version of Rust
-
In the terminal, run the following
cargo new hello --vcs none. -
In the newly created hello folder (cd hello), go to src --> main.rs file (Rust extension is .rs). Edit to the following code:
-
cd into the newly made hello folder, run
cargo build - Use
./target/debug/helloto run the file. You should see this in the terminal If you don't see this, go back to main.rs file and ensure the code is correct - You can also use
cargo runfrom the hello subdirectory to compile and run the program in 1 step
build vs run
while cargo build only compiles the project but doesn't actually run it (you have to do it manually as seen above), cargo run does both
### Done with the project? Go back using cd .. and use git add . & git commit -m "" to log your files using Git