I recently started to look into Rust and found it quite interesting and fun to work with, reminded me of working a bit like go and C lang. Thus I though it would be good to go through a blog series on of learning Rust with VSCode.

I will be using WSL Ubuntu 20.04.3 LTS for my development. If you are not using WSL yet, it allows you to have a linux terminal right inside of your windows desktop. Windows Subsystem for Linux 2) allows users to run a Linux VM inside windows and gives a near native Linux experience.

Installing Rust

Lets install Rust in your Ubuntu environment.

The output of the command would look like below.

To verify your install try this command

Before we jump into VSCode, lets just try to create a simple hello world first in the most basic way without cargo (rust build manager, like npm).

Simple Hello World

Open a file and call it hello.rs and paste the code into it. You can use nano or vi or vim.

We can then compile with rustc and it will create an executable that your computer can execute/

Creating and Running a Project with Cargo

Lets create a new project with cargo. You may wonder what is cargo. Cargo is the Rust package manager. Cargo downloads your Rust package’s dependencies, compiles your packages, makes distributable packages, and uploads them to crates.io, the Rust community’s package registry. We will execute on your shell prompt like below and look inside the directory.

What is this Cargo.toml

Cargo.toml is a configuration file for the project. The extension .toml stands for Tom’s Obvious, Minimal Language. Lets take a look at the file

We also have the main.rs file, let’s take a look inside that file also, we will see that it looks similar to our hello world from before. Cargo has created a hello world sample for us.

This time rather than using rustc to compile the program, we will use cargo run to compile the source code and run it in one command:

The first three lines are information about what Cargo is doing. We can also use cargo run -q to make the output to be quiet.

What about VSCode?

So we can just type code . in your terminal it will launch vscode for us and we can then go into the extension section and install rust and rust-analyzer.

Install Rust Extension

rust-extension-vscode

rust-extension-vscode

Run Rust in VSCode

If you install rust analyzer then you will see the Run and Debug command ontop of your code where you can click on the run command to run Rust like below.

Run_Rust_In_VSCode

Run_Rust_In_VSCode

Here is the output that will show on your terminal.

Output_Rust_Terminal

Output_Rust_Terminal

Summary

So here is a quick summary of how to get Rust to run with vscode, in the next sections I will go through more learning of Rust and how to have fun with it.