千家信息网

Windows WSL2安装Rust开发环境的方法步骤

发表于:2025-02-24 作者:千家信息网编辑
千家信息网最后更新 2025年02月24日,本篇内容主要讲解"Windows WSL2安装Rust开发环境的方法步骤",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Windows WSL2安装Rust
千家信息网最后更新 2025年02月24日Windows WSL2安装Rust开发环境的方法步骤

本篇内容主要讲解"Windows WSL2安装Rust开发环境的方法步骤",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Windows WSL2安装Rust开发环境的方法步骤"吧!

安装WSL

这里安装的是Alpine Linux,体积小,消耗资源少。

  1. 安装Apline WSL

https://www.microsoft.com/en-us/p/alpine-wsl/9p804crf0395#activetab=pivot:overviewtab

  1. 安装与配置:sudo命令

# 登录rootsu# 安装sudoapk add sudo# 将用户名添加到wheel中,才能执行sudoaddgroup [username] wheel# 去除sudo时,输入密码visudo# 找到行"# %wheel ALL=(ALL) NOPASSWD: ALL"去掉'#'号取消注释,:wq保存。
  1. 添加阿里云Apline源,加速软件安装

sudo sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
  1. 安装基础软件包

sudo apk add bash wget curl git vim tar gzip build-base

安装Rust

1. 加速Rust安装

export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-staticexport RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

2. 安装Rust

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

3. 配置cargo阿里云源, 加速cargo install

vim ~/.cargo/config

将下面的内容,放到上方文件中,然后 :wq

[source.crates-io]registry = "https://github.com/rust-lang/crates.io-index"replace-with = "rustcc"[source.rustcc]registry = "https://code.aliyun.com/rustcc/crates.io-index.git"

测试Rust

  1. 检查版本:

rustc -V# 输出:rustc 1.51.0 (2fd73fabe 2021-03-23)cargo -V# 输出: cargo 1.51.0 (43b129a20 2021-03-16)

2. 检查是否正常build

# 创建一个demo项目cargo new first_democd first_democargo build##  输出:   Compiling first_demo v0.1.0 (/home/xxx/workspace/vscode/demo/first_demo)##          Finished dev [unoptimized + debuginfo] target(s) in 0.78scargo run##  输出:      Finished dev [unoptimized + debuginfo] target(s) in 0.00s##             Running `target/debug/first_demo`##  Hello, world!

安装vscode(Visual Studio Code)

  1. windows中安装vscode:

https://code.visualstudio.com/download

  1. 打开VSCode, 安装 Remote - WSL 与 Rust 两款插件.

  2. "ctrl + shift + p" 调出命令,输入wsl,选择"Remote-WSL:New Window using Distro...",再选择"Apline"即可。

  1. 打开创建的"first_demo",就可以开始玩耍了。

到此,相信大家对"Windows WSL2安装Rust开发环境的方法步骤"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0