Windows 11 使用 rustup-init.exe 安装 Rust 完整教程 个人主页编程的一拳超人⛺️ 欢迎关注点赞 留言 收藏 于高山之巅方见大河奔涌于群峰之上更觉长风浩荡。 ——《人民日报》适用系统Windows 11 64位安装方式官方 rustup-init.exe工具链x86_64-pc-windows-msvcWindows 原生编译必备目录Windows 11 使用 rustup-init.exe 安装 Rust 完整教程安装到 D:\develop_tools\Rust一、安装前说明二、前置配置国内镜像可选但强烈推荐三、步骤1下载 rustup-init.exe方式1官网下载推荐方式2winget 安装Windows 11 自带四、步骤2配置 D 盘安装路径核心步骤必做方法一临时环境变量当前终端生效推荐方法二永久环境变量所有终端生效五、步骤3运行安装程序并配置依赖3.1 启动 Rust 安装程序3.2 安装 Microsoft C 生成工具最重要3.3 将 C 生成工具安装到 D 盘可选但推荐六、步骤4完成 Rust 标准安装七、步骤5验证安装必做八、步骤6配置 Cargo 国内镜像D 盘路径版九、步骤7测试 Rust 环境Hello World十、常用命令十一、常见问题✅ 安装完成标准一键安装 Rust 到 D:\develop_tools\Rust 的 PowerShell 脚本一键安装脚本使用方法脚本说明Windows 11 使用 rustup-init.exe 安装 Rust 完整教程安装到 D:\develop_tools\Rust适用系统Windows 10/11 64位安装方式官方 rustup-init.exe工具链x86_64-pc-windows-msvcWindows 原生编译必备最终固定目录结构安装程序自动生成无需手动提前创建D:\develop_tools\Rust\.rustupRust 工具链、版本管理、组件存放D:\develop_tools\Rust\.cargoCargo 包管理器、缓存、二进制文件、全局配置D:\develop_tools\Rust\target可选统一所有 Rust 项目编译输出目录一、安装前说明路径要求所有路径不能包含中文、空格、特殊字符如D:\开发工具\Rust会导致编译报错依赖说明Rust 在 Windows 上必须依赖 Microsoft C 生成工具才能正常编译程序本教程会同步教你将其也安装到 D 盘旧版本清理如果之前在 C 盘装过 Rust必须先执行以下命令彻底卸载再删除 C 盘残留的.rustup和.cargo文件夹rustup self uninstall权限说明全程无需管理员权限普通用户终端即可完成二、前置配置国内镜像可选但强烈推荐打开 Windows PowerShell执行以下命令永久配置国内最快的 rsproxy 镜像解决官方下载慢、超时问题# 永久配置 Rustup 下载镜像用户级所有终端生效[System.Environment]::SetEnvironmentVariable(RUSTUP_DIST_SERVER,https://rsproxy.cn,User)[System.Environment]::SetEnvironmentVariable(RUSTUP_UPDATE_ROOT,https://rsproxy.cn/rustup,User)# 立即生效仅当前终端会话$env:RUSTUP_DIST_SERVER https://rsproxy.cn$env:RUSTUP_UPDATE_ROOT https://rsproxy.cn/rustup三、步骤1下载 rustup-init.exe方式1官网下载推荐打开 Rust 官方下载页https://www.rust-lang.org/tools/install点击Download rustup-init.exe (64-bit)保存到 D 盘任意无中文目录例如D:\Temp\rustup-init.exe方式2winget 安装Windows 11 自带winget install Rustlang.Rustup注意通过 winget 安装后仍需执行下文步骤2配置 D 盘路径再运行安装程序四、步骤2配置 D 盘安装路径核心步骤必做二选一即可推荐方法一临时设置灵活易改不会污染系统全局环境方法一临时环境变量当前终端生效推荐保持当前 PowerShell 窗口打开进入rustup-init.exe所在目录cd D:\Temp执行以下命令指定 D 盘安装路径$env:RUSTUP_HOME D:\develop_tools\Rust\.rustup$env:CARGO_HOME D:\develop_tools\Rust\.cargo绝对不要关闭当前终端直接执行下一步安装程序方法二永久环境变量所有终端生效按WinR→ 输入sysdm.cpl→ 点击「高级」→「环境变量」在用户变量仅当前用户生效推荐中新建两个变量变量名RUSTUP_HOME变量值D:\develop_tools\Rust\.rustup变量名CARGO_HOME变量值D:\develop_tools\Rust\.cargo点击「确定」保存关闭所有已打开的终端重新打开一个新的 PowerShell进入rustup-init.exe所在目录cd D:\Temp五、步骤3运行安装程序并配置依赖3.1 启动 Rust 安装程序在当前 PowerShell 窗口执行.\rustup-init.exe3.2 安装 Microsoft C 生成工具最重要出现以下界面时必须输入1并回车Rust Visual C prerequisites 1) Quick install via the Visual Studio Community installer 2) Manually install the prerequisites 3) Dont install the prerequisites 13.3 将 C 生成工具安装到 D 盘可选但推荐弹出 Visual Studio 安装器后点击右下角「安装位置」修改三个路径均指向 D 盘例如安装位置D:\develop_tools\Visual Studio\2022\BuildTools下载缓存D:\develop_tools\Visual Studio\Packages共享组件、工具和 SDKD:\develop_tools\Visual Studio\Shared只勾选「使用 C 的桌面开发」组件右侧可选组件保持默认即可取消勾选「安装后启动 Visual Studio」点击「安装」等待完成约 10~30 分钟取决于网络速度六、步骤4完成 Rust 标准安装C 生成工具安装完成后会自动回到 Rust 安装界面此时直接按回车键使用默认配置即可Current installation options: default host triple: x86_64-pc-windows-msvc default toolchain: stable (default) profile: default modify PATH variable: yes 1) Proceed with standard installation 2) Customize installation 3) Cancel installation 直接回车说明因为已经提前配置了RUSTUP_HOME和CARGO_HOME环境变量安装程序会自动将所有文件安装到 D 盘无需选择「Customize installation」等待自动下载安装完成出现以下提示代表安装成功Rust is installed now. Great!按回车退出安装程序。七、步骤5验证安装必做关闭所有终端重新打开一个新的 PowerShell依次执行以下命令验证验证核心命令是否可用rustc--version cargo--version rustup--version三个命令均正常输出版本号即为通过。验证工具链是否为 MSVCWindows 原生必备rustup show✅ 必须出现x86_64-pc-windows-msvc (default)验证是否成功安装到 D 盘echo$env:RUSTUP_HOMEecho$env:CARGO_HOME✅ 输出必须分别为D:\develop_tools\Rust\.rustup和D:\develop_tools\Rust\.cargo八、步骤6配置 Cargo 国内镜像D 盘路径版由于我们将 Cargo 安装到了 D 盘配置文件路径不再是默认的 C 盘用户目录执行以下命令创建并编辑配置文件# 创建 D 盘 Cargo 配置目录如果不存在mkdir D:\develop_tools\Rust\.cargo# 创建并打开配置文件notepad D:\develop_tools\Rust\.cargo\config.toml在打开的记事本中粘贴以下内容保存并关闭[source.crates-io] replace-with rsproxy [source.rsproxy] registry sparsehttps://rsproxy.cn/index/ [registries.rsproxy] index https://rsproxy.cn/crates.io-index [net] git-fetch-with-cli true # 可选统一所有项目编译输出到 D 盘节省 C 盘空间 [build] target-dir D:/develop_tools/Rust/target九、步骤7测试 Rust 环境Hello World在任意目录执行以下命令创建并运行测试项目# 创建新项目cargo new hello_rust cd hello_rust# 编译并运行cargo run看到输出Hello, world!代表 Rust 环境完全正常且所有编译产物都会生成在D:\develop_tools\Rust\target目录下。十、常用命令# 查看版本rustc--version cargo--version rustup--version# 更新 Rust 到最新稳定版rustup update# 查看已安装的工具链rustup toolchain list# 切换工具链例如切换到 nightly 版rustup default nightly# 卸载 Rustrustup self uninstall十一、常见问题命令找不到关闭所有终端重新打开确保安装程序已自动将D:\develop_tools\Rust\.cargo\bin添加到系统 PATH编译报错 link.exe 未找到重新安装 Visual Studio 的「使用 C 的桌面开发」组件确保安装路径无中文下载速度慢检查本文第2步和第8步的国内镜像是否配置正确仍安装到 C 盘确认配置环境变量后没有关闭终端且旧版本 Rust 已彻底卸载并删除残留文件夹✅ 安装完成标准rustc --version正常显示版本cargo --version正常显示版本rustup show显示x86_64-pc-windows-msvc (default)echo $env:RUSTUP_HOME输出 D 盘路径cargo run可正常编译运行 Hello World一键安装 Rust 到 D:\develop_tools\Rust 的 PowerShell 脚本脚本功能自动配置国内镜像、设置 D 盘安装路径、下载官方安装程序、自动完成安装、配置 Cargo 镜像、验证安装结果全程无需手动干预。一键安装脚本将以下内容复制到记事本保存为install-rust-d.ps1注意后缀名必须是.ps1不是.txt# Rust 一键安装到 D:\develop_tools\Rust 脚本 适用系统Windows 10/11 64位 依赖PowerShell 5.1系统自带 ## 临时允许当前进程执行脚本不修改系统全局设置Set-ExecutionPolicyBypass-ScopeProcess-ForceClear-HostWrite-Host-ForegroundColor CyanWrite-Host Rust 一键安装到 D:\develop_tools\Rust-ForegroundColor CyanWrite-Host-ForegroundColor CyanWrite-Host# --------------------------# 1. 配置国内镜像永久生效# --------------------------Write-Host步骤 1/7配置国内加速镜像...-ForegroundColor Yellow[System.Environment]::SetEnvironmentVariable(RUSTUP_DIST_SERVER,https://rsproxy.cn,User)[System.Environment]::SetEnvironmentVariable(RUSTUP_UPDATE_ROOT,https://rsproxy.cn/rustup,User)# 立即生效$env:RUSTUP_DIST_SERVER https://rsproxy.cn$env:RUSTUP_UPDATE_ROOT https://rsproxy.cn/rustupWrite-Host✅ 国内镜像配置完成-ForegroundColor Green# --------------------------# 2. 设置 D 盘安装路径永久生效# --------------------------Write-Host步骤 2/7配置 D 盘安装路径...-ForegroundColor Yellow$RUSTUP_HOMED:\develop_tools\Rust\.rustup$CARGO_HOMED:\develop_tools\Rust\.cargo[System.Environment]::SetEnvironmentVariable(RUSTUP_HOME,$RUSTUP_HOME,User)[System.Environment]::SetEnvironmentVariable(CARGO_HOME,$CARGO_HOME,User)# 立即生效$env:RUSTUP_HOME $RUSTUP_HOME$env:CARGO_HOME $CARGO_HOMEWrite-Host✅ 安装路径设置完成$RUSTUP_HOME-ForegroundColor GreenWrite-Host✅ 安装路径设置完成$CARGO_HOME-ForegroundColor Green# --------------------------# 3. 清理旧版本如果存在# --------------------------Write-Host步骤 3/7检查并清理旧版本 Rust...-ForegroundColor Yellowif(Get-Commandrustup-ErrorAction SilentlyContinue){Write-Warning检测到已安装的 Rust 版本正在卸载...rustup self uninstall-y# 删除可能残留的文件夹if(Test-PathC:\Users\$env:USERNAME\.rustup){Remove-Item-Recurse-ForceC:\Users\$env:USERNAME\.rustup}if(Test-PathC:\Users\$env:USERNAME\.cargo){Remove-Item-Recurse-ForceC:\Users\$env:USERNAME\.cargo}Write-Host✅ 旧版本清理完成-ForegroundColor Green}else{Write-Host✅ 未检测到旧版本跳过清理-ForegroundColor Green}# --------------------------# 4. 创建临时目录并下载安装程序# --------------------------Write-Host步骤 4/7下载官方安装程序...-ForegroundColor Yellow$TEMP_DIRD:\Tempif(-not(Test-Path$TEMP_DIR)){New-Item-ItemType Directory-Path$TEMP_DIR|Out-Null}$INSTALLER_PATH$TEMP_DIR\rustup-init.exe# 下载安装程序Invoke-WebRequest-Urihttps://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe-OutFile$INSTALLER_PATHWrite-Host✅ 安装程序下载完成$INSTALLER_PATH-ForegroundColor Green# --------------------------# 5. 自动运行安装程序# --------------------------Write-Host步骤 5/7开始安装 Rust约 5-15 分钟取决于网络速度...-ForegroundColor YellowWrite-Host⚠️ 请勿关闭此窗口安装过程中会自动处理所有选项-ForegroundColor Red# -y 参数自动确认所有默认选项无需手动输入$INSTALLER_PATH-y--default-toolchain stable--profile default--modify-path yesWrite-Host✅ Rust 本体安装完成-ForegroundColor Green# --------------------------# 6. 配置 Cargo 国内镜像# --------------------------Write-Host步骤 6/7配置 Cargo 国内镜像...-ForegroundColor Yellow$CARGO_CONFIG$CARGO_HOME\config.toml# 创建配置文件目录if(-not(Test-Path$CARGO_HOME)){New-Item-ItemType Directory-Path$CARGO_HOME|Out-Null}# 写入配置内容 [source.crates-io] replace-with rsproxy [source.rsproxy] registry sparsehttps://rsproxy.cn/index/ [registries.rsproxy] index https://rsproxy.cn/crates.io-index [net] git-fetch-with-cli true # 统一所有项目编译输出到 D 盘节省 C 盘空间 [build] target-dir D:/develop_tools/Rust/target |Out-File-FilePath$CARGO_CONFIG-Encoding UTF8Write-Host✅ Cargo 镜像配置完成-ForegroundColor Green# --------------------------# 7. 刷新环境变量并验证安装# --------------------------Write-Host步骤 7/7验证安装结果...-ForegroundColor Yellow# 刷新环境变量$env:Path [System.Environment]::GetEnvironmentVariable(Path,User);[System.Environment]::GetEnvironmentVariable(Path,Machine)# 验证核心命令Write-HostWrite-Host--- 安装验证 ----ForegroundColor Cyan rustc--version cargo--version rustup--versionWrite-HostWrite-Host-ForegroundColor GreenWrite-Host Rust 已成功安装到 D:\develop_tools\Rust-ForegroundColor GreenWrite-Host-ForegroundColor GreenWrite-HostWrite-Host 后续步骤-ForegroundColor YellowWrite-Host1. 关闭所有终端重新打开一个新的 PowerShellWrite-Host2. 运行以下命令测试环境Write-Host cargo new hello_rustWrite-Host cd hello_rustWrite-Host cargo runWrite-HostWrite-Host 提示所有编译产物会自动生成在 D:\develop_tools\Rust\targetWrite-HostPause使用方法右键点击保存好的install-rust-d.ps1文件选择「使用 PowerShell 运行」等待脚本自动完成所有步骤全程无需手动操作安装完成后关闭所有终端重新打开一个新的 PowerShell即可使用 Rust脚本说明安全无副作用仅修改当前用户的环境变量不修改系统全局设置自动清理旧版本如果检测到已安装的 Rust会自动卸载并删除残留文件国内加速全程使用 rsproxy 镜像解决官方下载慢、超时问题自动配置自动完成 Cargo 镜像配置和统一编译输出目录设置完整验证安装完成后自动验证所有核心命令是否正常如果运行时提示「无法加载文件因为在此系统上禁止运行脚本」请先以管理员身份打开 PowerShell执行以下命令后再运行脚本Set-ExecutionPolicyRemoteSigned-Scope CurrentUser输入Y确认即可。