Windows 11 + WSL + VS Code + Rust 环境配置

最近,秋招基本结束,毕设中期也完事了,算是有一些时间了,打算开始学点新东西。

前言

可以在 Win 或者 Linux 上搭建 rust 环境,在网上搜索后,我发现在 Linux 上搭建 Rust 环境要比在 Win 上快捷很多,但是我日常又使用 Win 系统。

所以最终选择在 Win11 的 WSL2 中搭建 Rust 环境,在用 Vs Code 连接 WSL。

总体步骤

  1. Win11 安装 WSL
  2. Win11 WSL 换源
  3. Win11 WSL 安装 gcc
  4. Win11 WSL 安装 rust
  5. Win11 vscode 安装 Remote-WSL
  6. Win11 vscode 安装 rust-analyzer
  7. 简单验证
  8. 运行与调试

Win11 安装 WSL

参考:https://learn.microsoft.com/zh-cn/windows/wsl/about

https://learn.microsoft.com/zh-cn/windows/wsl/install

前提条件:

image-20231126161500760

以管理员模式打开 PowerShell 或 Windows 命令提示符,输入 wsl --install 命令,就开始安装 WSL 了,这了默认会安装最新的稳定版本的 Ubuntu。可以手动选择别的版本,具体操作见 微软教程.

安装完成后,需要重启电脑,配置才能生效。

ps:可能是因为我一直开着梯子的原因,安装 WSL 的时候,一直提示 wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。

不知道会不会对 WSL 的网络产生影响,保险起见我 按照 教程做了一些配置。

WSL 中有一个配置文件用于配置高级设置选项,那就是 .wslconfig.这个文件默认是没有的,如果要配置,我们要手动创建。

这个文件在 C:\Users\<UserName>\.wslconfig 中。

创建文件后,填入以下内容并保存。

[experimental]
autoMemoryReclaim=gradual  # gradual  | dropcache | disabled
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

再打开 power shell,输入 wsl --shutdown,重新打开 WSL,就没有之前的提示了。

Win11 WSL 换源

WSL 和正常的 Ubunut 系统一样,原始使用国外源,但是我们使用不方便,所以可以改为国内源,更改方法和 ubunut 系统中一模一样。

ps:要先确定下 WSL 中安装的系统版本,我这里默认安装的 Ubuntu 22.04.3 LTS.

国内源

清华源:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

阿里源:

deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

中科大源:

deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

网易源:

deb http://mirrors.163.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jammy-backports main restricted universe multiverse

选择一个国内源后,执行

sudo apt-get update
sudo apt-get upgrade

Win11 WSL 安装 gcc

打开WSL,执行下面的命令

apt-get update  # 确保apt-get是最新,可能install gcc时报错
sudo apt install gcc

Win11 WSL 安装 rust

两行命令即可:

sudo apt install rustc
sudo apt install cargo

等安装完成后在终端运行 cargo version

若出现以下内容则证明 rust 安装成功。

image-20231126163636737

Win11 vscode 安装 Remote-WSL

目前,vscode 已经提供插件可使用 windows 的 vscode 连接到 wsl2 中,这个插件就是 WSL

image-20231126163800014

所以,只要 windows 上的 vscode 安装该插件就可以连接到 wsl2 中。

第一次的时候需要等待一些时间,因为需要做一些初始化的工作

当初始化完成后点击 Open Folder 将会选择 wsl2 里的某个文件夹。

image-20231126163942891

Win11 vscode 安装 rust-analyzer

当用vscode连接WSL后,安装的插件就有两个“版本”。一个是win上使用的,一个是安装在WSL中的。

image-20231126164254143

而这里,我们暂时只需要一个,那就是rust-analyzer

在连接WSL的Vs Code窗口中,搜索rust-analyzer,安装插件就可以。

简单验证

在WSL中,随便找一个目录,输入 cargo init hello-world

再输出 code .,就可以使用Vs Code打开Rust项目了。

image-20231126164600855

在Vs Code的终端中运行 cargo run,验证环境是否配置成功。如果成果,效果如下图所示。

image-20231126164727968

到这里,Rust的基本环境就配置成功了。

运行与调试

image-20231126170853688

在WSL中输入code .打开VS Code后,点击代码左上角的“Run”就可以执行程序。

若向debug,需要安装插件 CodeLLDB

安装插件后,就可以使用调试。

image-20231126171239141

目前在WSL上安装的插件如下图。

image-20231126171146275

posted @ 2023-11-26 17:01  LLW_NEU  阅读(4311)  评论(1编辑  收藏  举报