Rust初学者环境搭建(非Viatual Studio ,自定义安装目录)

一、资源准备

1.1 Rust安装器下载

1.2 汉化学习文档

二、安装准备

  • 注意:以下环境变量的配置是为了修改rustup-init.exe安装时的默认路径,默认路径是 C:\User\用户名\.rustup 和 C:\Users\用户名\.cargo,添加以上环境变量后默认路径修改为 C:\WorkSpace\Rust_environment\.rustup 和 C:\WorkSpace\Rust_environment\.cargo,读者可根据自己的电脑目录及存储自定义这两个地址。

    • 在环境变量-用户变量中添加:

    • 变量名:CARGO_HOME 变量值:C:\WorkSpace\Rust_environment\.cargo

    • 在环境变量-用户变量中添加:

    • 变量名:RUSTUP_HOME 变量值:C:\WorkSpace\Rust_environment\.rustup

    • 在在环境变量-用户变量-path中添加:

    • %CARGO_HOME%

    • %RUSTUP_HOME%

  • 随后点击运行下载好的文件rustup-init.exe

  • 注意:Rust默认需要C++ build Tools (也就是MSVC运行库:微软VC编译器)才能安装,也就是说普通用户需要安装Viatual Studio2022或更高版本(社区版足矣),另外还要在在Viatual Studio installer额外勾选C++ build Tools,下载完成后才能适配(如果你是高级用户和企业用户可以自行配置C++ build Tools),如果读者的IDE就是Viatual Studio,那么推荐以这种默认方法安装,如果不是,以下会介绍非Viatual Studio的安装,即MinGW(MInimalist GNU on)运行库式安装的,但IDE还请各位自行适配。(如果还没有十分顺手的IDE的小伙伴采用此方法后续适配自己的IDE容易踩坑,但有利于学习,作者采用的是VSCode,跨平台好一些,追求方便快捷的可选用其他)

  • 以下为安装对话

Rust Visual C++ prerequisites

Rust requires a linker and Windows API libraries but they don't seem to be
available.

These components can be acquired through a Visual Studio installer.

1) Quick install via the Visual Studio Community installer
   (free for individuals, academic uses, and open source).

2) Manually install the prerequisites
   (for enterprise and advanced users).

3) Don't install the prerequisites
   (if you're targetting the GNU ABI).

>3 #此处为作者添加的对话:选择1会下载Visual Stdio;选择2表示你是高级用户或企业用户;选择3即是采用MinGW编译


Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  C:\WorkSpace\Rust_environment\.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  C:\WorkSpace\Rust_environment\.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  C:\WorkSpace\Rust_environment\.cargo\bin

This path will then be added to your PATH environment variable by
modifying the HKEY_CURRENT_USER/Environment/PATH registry key.

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-pc-windows-msvc
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>2 #此处为作者添加的对话:选1表示默认当前配置安装,选2表示自定义这些配置,选3表示取消安装

I'm going to ask you the value of each of these installation options.
You may simply press the Enter key to leave unchanged.

Default host triple? [x86_64-pc-windows-msvc]
x86_64-pc-windows-gnu #此处为作者添加的对话:修改为x86_64-pc-windows-gnu而不是默认的x86_64-pc-windows-msvc

Default toolchain? (stable/beta/nightly/none) [stable]
stable #此处为作者添加的对话:为了稳定更新,默认stable就stable

Profile (which tools and data to install)? (minimal/default/complete) [default]
default #此处为作者添加的对话:下载默认的工具有利于学习文档的辅助学习,所以还是按默认的来

Modify PATH variable? (Y/n)
Y #此处为作者添加的对话:同意添加环境变量,也可自定义


Current installation options:


   default host triple: x86_64-pc-windows-gnu
     default toolchain: stable
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1 #此处为作者添加的对话:确认配置无误,按此配置安装,选1

info: profile set to 'default'
info: setting default host triple to x86_64-pc-windows-gnu
info: syncing channel updates for 'stable-x86_64-pc-windows-gnu'
info: latest update on 2022-11-03, rust version 1.65.0 (897e37553 2022-11-02)
info: downloading component 'cargo'
  7.3 MiB /   7.3 MiB (100 %)   3.1 MiB/s in  2s ETA:  0s
info: downloading component 'clippy'
  3.7 MiB /   3.7 MiB (100 %)   2.8 MiB/s in  1s ETA:  0s
info: downloading component 'rust-docs'
 18.9 MiB /  18.9 MiB (100 %)   3.1 MiB/s in  6s ETA:  0s
info: downloading component 'rust-mingw'
  4.2 MiB /   4.2 MiB (100 %)   3.2 MiB/s in  1s ETA:  0s
info: downloading component 'rust-std'
 28.1 MiB /  28.1 MiB (100 %)   2.9 MiB/s in  9s ETA:  0s
info: downloading component 'rustc'
 74.0 MiB /  74.0 MiB (100 %)   3.1 MiB/s in 25s ETA:  0s
info: downloading component 'rustfmt'
  6.6 MiB /   6.6 MiB (100 %)   3.1 MiB/s in  2s ETA:  0s
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 18.9 MiB /  18.9 MiB (100 %)   1.8 MiB/s in 12s ETA:  0s
info: installing component 'rust-mingw'
info: installing component 'rust-std'
 28.1 MiB /  28.1 MiB (100 %)  13.5 MiB/s in  2s ETA:  0s
info: installing component 'rustc'
 74.0 MiB /  74.0 MiB (100 %)  13.9 MiB/s in  9s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-pc-windows-gnu'

  stable-x86_64-pc-windows-gnu installed - rustc 1.65.0 (897e37553 2022-11-02)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload its PATH environment variable to include
Cargo's bin directory (%USERPROFILE%\.cargo\bin).

Press the Enter key to continue.

三、Hello,World

  • 新世界已经开始,言说不如实行,Rust的官方文档十分有利于新手学习,代码学习作者建议参照文档学习,本文中的文档链接是好心人的汉化文档,英文不好的小伙伴自行食用。

四、补充

  • 如果后续小伙伴采用rustc,Cargo进行编译时出现error: linking with x86_64-w64-mingw32-gcc failed: exit code: 1 的错误,可能是小伙伴事先安装了MinGW,可以将环境变量中的此值删除,另外发生此错误时同时会生成许多二进制文件,建议除了我们自己编写的文件(main.rs等等)以外,其他全部删除,重新编译。如果问题依然没有解决,请移至其他文章。

参考文章

  1. Rust 环境搭建 | 菜鸟教程 (runoob.com)
  2. MSVC编译器介绍_顺其自然~的博客-CSDN博客_msvc
  3. 自定义Rust安装路径,自定义安装Rust_ZZkStellaris的博客-CSDN博客_rust安装路径
  4. VSCode中配置Rust时报错:error: linking with x86_64-w64-mingw32-gcc failed: exit code: 1_coding_cats的博客-CSDN博客
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值