Windows 11 + WSL + VS Code + Rust Environment Setup
Introduction
You can set up a Rust environment on either Windows or Linux. After searching online, I found that setting up a Rust environment on Linux is much quicker than on Windows, but I still use the Windows system on a daily basis.
Therefore, I ultimately chose to set up the Rust environment in the WSL2 of Windows 11, and then use VS Code to connect to WSL.
Overall Steps
- Install WSL on Windows 11
- Change the source on WSL in Windows 11
- Install gcc on WSL in Windows 11
- Install Rust on WSL in Windows 11
- Install Remote-WSL on VS Code in Windows 11
- Install rust-analyzer on VS Code in Windows 11
- Simple verification
- Run and debug
Install WSL on Windows 11
Reference: https://learn.microsoft.com/en-us/windows/wsl/about
https://learn.microsoft.com/en-us/windows/wsl/install
Prerequisites:
Open PowerShell or Windows Command Prompt as an administrator, and
enter the wsl --install
command to start installing WSL. By
default, this will install the latest stable version of Ubuntu. You can
manually choose other versions; see the Microsoft
tutorial for specific instructions.
After the installation is complete, you need to restart your computer for the configuration to take effect.
PS: It might be because I had a VPN running all the time, but during the WSL installation, it kept prompting "wsl: Localhost proxy configuration detected, but not mirrored to WSL. Localhost proxy is not supported in NAT mode for WSL."
I'm not sure if this would affect the network of WSL, but to be safe, I followed the instructions to make some configurations.
In WSL, there is a configuration file used to configure advanced setting options, which is .wslconfig. This file doesn't exist by default; if you want to configure it, you need to create it manually.
This file is located at
C:\Users\<UserName>\.wslconfig
.
After creating the file, enter the following content and save it:
1 |
|
Then, open PowerShell and enter wsl --shutdown
, and
reopen WSL. The previous prompt should be gone.
Change the Source on WSL in Windows 11
Just like a normal Ubuntu system, WSL uses foreign sources by default, which are inconvenient for us to use. Therefore, we can change to domestic sources, and the method is the same as in the Ubuntu system.
PS: Make sure to confirm the system version installed in WSL first. Here, the default installation is Ubuntu 22.04.3 LTS.
Domestic Sources
Tsinghua University:
1 |
|
Alibaba Cloud:
1 |
|
University of Science and Technology of China:
1 |
|
NetEase:
1 |
|
After choosing a domestic source, execute the following commands:
1 |
|
Install gcc on WSL in Windows 11
Open WSL and run the following commands:
1 |
|
Install Rust on WSL in Windows 11
Two commands are enough:
1 |
|
1 |
|
After the installation is complete, run cargo version
in
the terminal.
If you see the following output, it means Rust has been installed successfully.
Install Remote-WSL on VS Code in Windows 11
Currently, VS Code provides a plugin that allows you to connect to WSL2 from the Windows version of VS Code. This plugin is called Remote-WSL.
So, as long as you install this plugin in the Windows version of VS Code, you can connect to WSL2.
The first time you connect, you may need to wait for some time, as it needs to perform some initialization work.
After the initialization is complete, click "Open Folder" to select a folder in WSL2.
Install rust-analyzer on VS Code in Windows 11
After connecting VS Code to WSL, the installed plugins have two "versions": one for use on Windows, and one installed in WSL.
Here, we only need one for now, which is rust-analyzer.
In the VS Code window connected to WSL, search for rust-analyzer and install the plugin.
Simple Verification
In WSL, navigate to any directory and enter
cargo init hello-world
.
Then, enter code .
to open the Rust project in VS
Code.
In the VS Code terminal, run cargo run
to verify if the
environment is configured successfully. If successful, the output should
look like the following:
At this point, the basic Rust environment has been set up successfully.
Run and Debug
After entering code .
in WSL to open VS Code, click the
"Run" button in the top-left corner of the code editor to execute the
program.
To debug, you need to install the CodeLLDB plugin.
After installing the plugin, you can use the debugging functionality.
Currently, the plugins installed on WSL are as follows: