Visual Studio Code Tips For Hello World Rust Project

Maxim
3 min readOct 16, 2020

--

I love VSC and using daily for work and fun as well. Sharing a couple of tips on how to get started with VSC for Rust project.

If you are lazy as me, then code auto-complete is an essential part of your workflow. You know that I’m talking about ^ + space combination, auto imports, debugging, running tests, etc. Let me share a couple of things you need to set up to optimize your working flow.

Install rust as instructed here if have not done it yet. Then use the nightly toolchains version. This part is critical and autocomplete wasn’t working for me on the stable version.

# installing rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# install nightl ytoolchain
rustup toolchain install nightly
rustup default nightly
# optional steps: folder in Visual Studio Code from terminal
# mkdir -p ~/projects/rust/
# open path in Visual Studio code
# code ~/projects/rust/

Install those VSC extensions:

After bootstrapping a new Rust project via cargo as documented here you can start typing io and pressing ^ + space you will see autocomplete in action and online help as well:

Now let’s talk about debugging configuration. VSC can detect project type and suggest to generate a launch configuration by clicking on create a launch json file. So cool and awesome 👏

Here is this configuration for this hello world project:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'hello_cargo'",
"cargo": {
"args": [
"build",
"--bin=hello_cargo",
"--package=hello_cargo"
],
"filter": {
"name": "hello_cargo",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'hello_cargo'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=hello_cargo",
"--package=hello_cargo"
],
"filter": {
"name": "hello_cargo",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

Then you can set a breakpoint and you can dig into details. Please note that above fn main() you will see Run and Debug buttons:

Happy coding and have fun you all.

--

--

Maxim

Principal Software Engineer at Docker, ex f5 Networks engineer. Opinions are mine