rust 安装及开发环境配置

前沿

大概 7 年前我开始接触 rust,当时出于兴趣,把 rust 的语法很快速的学了一遍,也根据教程写了一些小程序进行练习,但是由于工作并没有任何需要使用到 rust 的地方,因此中断对 rust 学习和使用后,很快就忘记了。

现在,打算对 rust 知识做个梳理,第一步即是开发环境的搭建,这是系列第一篇。

安装

1. Linux/MacOS

arduino 复制代码
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

或者

ruby 复制代码
$ curl -f -L https://static.rust-lang.org/rustup.sh -O
$ sh rustup.sh

rustup installs rustc, cargo, rustup and other standard tools to Cargo's bin directory. On Unix it is located at $HOME/.cargo/bin and on Windows at %USERPROFILE%.cargo\bin.

卸载

shell 复制代码
$ sudo /usr/local/lib/rustlib/uninstall.sh

Rust is an 'ahead-of-time compiled language', which means that you can compile a program, give it to someone else, and they don't need to have Rust installed.

Cargo

Cargo 是包管理器(package manager),类似于 nodejs 的 npm

Cargo 主要管理下面三件事:

  • 构建代码
  • 下载依赖
  • 构建依赖

注:如果你是通过官方的 installer 安装的 rust,那么 cargo 会自动安装。

执行 cargo build 的时候,会把 cargo.toml 中指定的 dependencies 默认都下载到 ~/.cargo/registry/src 目录下

MacOS 中,rust 安装之后,会在 ~/.rustup/toolchains/stable-x86_64-apple-darwin/ 目录中安装 toolchains 工具,这里包括了 rust 的源码,比如标准库等等。

如果需要更新标准库,则执行 rustup update 即可。

编译安装(Install from source):

dependencies

  • g++ 5.1 or later or clang++ 3.5 or later

  • python 2.7 (but not 3.x)

  • GNU make 3.81 or later

  • cmake 3.4.3 or later

  • curl

  • git

  • ssl which comes in libssl-dev or openssl-devel

  • pkg-config if you are compiling on Linux and targeting Linux

1. Linux:

shell 复制代码
$ git clone https://github.com/rust-lang/rust.git
$ cd rust

$ cp config.toml.example config.toml

$ ./x.py build && ./x.py install

2. Windowns

64 位 Windows 安装程序: static.rust-lang.org/dist/rust-n...

安装完成之后, 所有的 rust 工具都在 %USERPROFILE%.cargo\bin 目录下,所有的 Rust Toolchains 包括 rustc, cargo and rustup。应该把这个目录加入 PATH 环境变量。

查看版本信息:

css 复制代码
rustc --version

此时, 还不能编译 hello.rs 文件,还需要 Visual studio 提供的 C++ Build Tool

MSVC 的 C++ Build Tool 会自动安装在 C:\Program Files (x86)\Microsoft Visual C++ Build Tools 目录中

选择上图中这个 Visual C++ 2015 x64 Native Build Tools Command Prompt

实际上执行的命令是:

shell 复制代码
$ cmd.exe %comspec% /k ""C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"" amd64

上述命令本质上就是检测了下系统类型,初始化相应的环境变量。

rustup: the Rust toolchain installer

rustup 是 Rust 语言工具的安装器,可以安装三种不同版本的 Rust 语言,分别是:stable,beta,nightly

通过 rustup 安装 nightly

ruby 复制代码
$ rustup install nightly

nightly 已经安装,但是还没激活,通过下列命令来检测是否激活:

arduino 复制代码
$ rustup run nightly rustc --verison

把 nightly 设为默认

arduino 复制代码
$ rustup default nightly

rustup 就相当于 python 里的 pyenv,由于 rust 提供 stable,beta,nightly 3个版本的 rust,一个机器上可以三种版本都安装(rust 称其为不同的 Channel),装了 其中一个版本之后,还需要激活这个版本,才能默认使用该版本的 rust compiler 去编译源程序。

如果 toolchain 没激活, 会显示如下错误信息:

sql 复制代码
$ e:\god\proj\rust>rustup show

Default host: x86_64-pc-windows-msvc

no active toolchain

激活方式就是执行:

arduino 复制代码
$ rustup default nightly

激活之后,Now any time you run cargo or rustc you will be running the nightly compiler.

ruby 复制代码
$ rustup install stable-x86_64-pc-windows-msvc  

或者简写成

ruby 复制代码
$ rustup install stable-msvc  

然后,使用 rustup 来进行切换。

使用场景,比如可以把一个目录设置成使用 nightly 编译,

进入该目录之后, 执行:

arduino 复制代码
$ rustup override set nightly-2014-12-18

Directory overrides

Directories can be assigned their own Rust toolchain with rustup override. When a directory has an override then any time rustc or cargo is run inside that directory, or one of its child directories, the override toolchain will be invoked.

To pin to a specific nightly:

arduino 复制代码
rustup override set nightly-2014-12-18

Or a specific stable release:

arduino 复制代码
rustup override set 1.0.0

To see the active toolchain use rustup show. To remove the override and use the default toolchain again, rustup override unset.

想要查看当前目录使用的 rust 版本可以执行:

ruby 复制代码
$ rustup show

全文完!

如果你喜欢我的文章,欢迎关注我的微信公众号:deliverit

相关推荐
橙序员小站7 分钟前
Harness Engineering:从 OpenClaw 看 AI 助理的基础设施建设
后端·aigc·openai
小陈工31 分钟前
2026年3月28日技术资讯洞察:5G-A边缘计算落地、低延迟AI推理革命与工业智造新范式
开发语言·人工智能·后端·python·5g·安全·边缘计算
azhou的代码园1 小时前
基于SpringBoot+微信小程序的图片识别科普系统
spring boot·后端·微信小程序
Tony Bai2 小时前
Rust 看了流泪,AI 看了沉默:扒开 Go 泛型最让你抓狂的“残疾”类型推断
开发语言·人工智能·后端·golang·rust
用户3167361303422 小时前
javaLangchain4j从官方文档入手,看他做了什么——具体使用(二)
后端
無名路人2 小时前
Zsh 脚本 + VS Code 任务:NestJS + Vue3 一键部署到 1Panel
运维·后端·自动化运维
ybwycx3 小时前
springboot之集成Elasticsearch
spring boot·后端·elasticsearch
程途知微3 小时前
AQS 同步器——Java 并发框架的核心底座全解析
java·后端
jump_jump3 小时前
RTK:给 AI 编码助手瘦身的 Rust 代理
性能优化·rust·claude
iPadiPhone4 小时前
分布式架构的“润滑剂”:RabbitMQ 核心原理与大厂面试避坑指南
分布式·后端·面试·架构·rabbitmq