Rust on Windows 11, My Complete Setup Guide
Install Rust
- From this page
- Download then run
rustup-init.exe(64b) - Open a terminal and run this command
rustc --versionjust to make sure everything is OK.
Install VSCode
- From this page, download and install VSCode
Install rust-analyzer extension for VSCode
- Open VSCode then from
- The extensions pane, on the left
- Or from this page
- Install
rust-analyzer
Setup Linting
- From VSCode
CTRL + ,to open theSettings- Type in
rust-analyzer check - Replace
checkbyclippyin theRust-analyzer > Check : Command(see below)
Setup Inlay Hints
By default, I find the type information a little too invasive, unnecessarily lengthening the lines of code and blurring readability. They’re very useful, but I’d like to display them on demand.
- In VSCode
CTRL + ,to open theSettings- Enter
inlay - Select
offUnlessPressed - Then press
CTRL + ALTin the editor when you want to check the types of the variables
Not pressing CTRL+ALT
Pressing CTRL+ALT
Do you see the i32 and u8 in gray ?
Setup line width
- When saving file the formatter may reformat long lines
- I create a
rustfmt.tomlfile at the root of the project - So far my
rustfmt.tomlhas only only one line
max_width = 200
Debugging code 1/2
- Install Build Tools for Visual Studio
- This is my preferred option because I code in C++.. It comes with everything : compiler, linker, debugger…
- Others may install CodeLLDB VSCode extension. It only provides a debugger. May be enough if you plan to only use Rust.
- Open a terminal
- Create a project (
cargo new rust_test4web)
cd .\rust_test4web\code .- Copy the lines below for example in the file
src\main.rs
fn main() {
let x = 5;
println!("{}", x);
let y = 6u8;
println!("{}", y);
let mut zoubida = 18;
println!("{}", zoubida);
zoubida = 19;
println!("{}", zoubida);
}
- Set a breakpoint on line 8
- See below the red dot on the left
- You can either click or strike
F9when the cursor is on the line of interest
- In the editor click on the “Debug” (see below in the red rectangle)
- A
target/debugdirectory is created - While looking for the outputs, keep in mind they occurs in the
DEBUG CONSOLEnot in theTERMINAL. See below
- Once the code stops on the line, you can then inspect variables, go step by step…
Debugging code 2/2
If you want to debug code when you press F5 and have more options (like passing arguments for example)
- Create a
.vscodefolder at the root of the project - Create a
tasks.jsonfile in that directory - Copy and paste the lines below
{
"version": "2.0.0",
"tasks": [
{
"label": "cargo-build-debug",
"type": "cargo",
"command": "build",
"args": [],
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "cargo-build-release",
"type": "cargo",
"command": "build",
"args": [
"--release"
],
"problemMatcher": [
"$rustc"
]
}
]
}
- Save the
.jsonfile (CTRL+S) - Create a
launch.jsonfile in the.vscodefolder - Copy and paste the lines below
{
"version": "0.2.0",
"configurations": [
{
"type": "cppvsdbg",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
"args": [],
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "RUST_BACKTRACE",
"value": "short"
}
],
"preLaunchTask": "cargo-build-debug"
},
{
"type": "cppvsdbg",
"request": "launch",
"name": "Release",
"program": "${workspaceFolder}/target/release/${workspaceFolderBasename}.exe",
"args": [],
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "RUST_BACKTRACE",
"value": "short"
}
],
"preLaunchTask": "cargo-build-release"
}
]
}
If LLDB extension is installed
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
"args": [],
"cwd": "${workspaceFolder}",
"env": {
"RUST_BACKTRACE": "1"
},
"sourceLanguages": ["rust"],
"preLaunchTask": "cargo-build-debug",
},
{
"type": "lldb",
"request": "launch",
"name": "Release",
"program": "${workspaceFolder}/target/release/${workspaceFolderBasename}.exe",
"args": [],
"cwd": "${workspaceFolder}",
"env": {
"RUST_BACKTRACE": "1"
},
"sourceLanguages": ["rust"],
"preLaunchTask": "cargo-build-release"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "cargo-build-debug",
"type": "cargo",
"command": "build",
"args": [],
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "cargo-build-release",
"type": "cargo",
"command": "build",
"args": [
"--release"
],
"problemMatcher": [
"$rustc"
]
}
]
}
Here is how it should look like
- On the left hand side, click on the
Run & Debugicon (the bug and the triangle icon,CTRL+SHIFT+Dotherwise)- Make sure the current config in the list box is set on
Debug - See below
- Make sure the current config in the list box is set on
- Switch back to the code
- Set a breakpoint somewhere
- Press
F5- A
target/debugdirectory is created - The debugger stops on the breakpoint
- A
3 ways to run the Debug version of your code without the debugger
Make sure the current configuration is still Debug
Option 1 :
- If you press
CTRL+F5this run the Debug version of the code but without debugging it - So, the debugger does not stop on the breakpoint.
Option 2 :
- In the editor
- Click
Run(instead ofDebug)
Option 3 :
- In VSCode
- Open a terminal
CTRL+ù(azerty keyboard) - Enter
cargo run
Create and Run a Release Version 1/2
- On the left hand side, click on the
Run & Debugicon (CTRL+SHIFT+D)- Make sure the current config is set on
Release
- Make sure the current config is set on
- You can either press F5 or CTRL+F5
- A
target/releasedirectory is created
Create and Run a Release Version 2/2
- In VSCode
- Open a terminal
CTRL+ù(azerty keyboard) - Enter
cargo run --release
How to build only (either, Debug or Release version)
Option 1 :
- Click on
Terminal/Run Task...option - Select
cargo-build-debugorcargo-build-release
Option 2 :
- Open a terminal in VSCode
- Either type
cargo buildorcargo build --release
Optional
Color Syntax for .toml files
- In VSCode install “Even Better TOML” extension
Get hints help & support while editing cargo.toml files
- Mostly helps when editing versions of the crates to be included in the project
- In VSCode install “dependi” extension (crates extension is now deprecated)
This may help
- Read this page
- Enter
rustup docin a terminal