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

相关推荐
桦说编程17 分钟前
简单方法实现子任务耗时统计
java·后端·监控
盖世英雄酱5813636 分钟前
物品超领取损失1万事故复盘(一)
java·后端
凌览40 分钟前
别再死磕 Nginx!http-proxy-middleware 低配置起飞
前端·后端
拾玖不会code41 分钟前
简单分表场景下的业务发散思考:分表如何保证丝滑?
后端
CryptoRzz41 分钟前
印度尼西亚(IDX)股票数据对接开发
java·后端·websocket·web3·区块链
我要改名叫嘟嘟1 小时前
2025年终总结(中),读书22本,是想看就看想停就停不再问心的读书
程序员
咕白m6251 小时前
通过 C# 快速生成二维码 (QR code)
后端·.net
踏浪无痕2 小时前
架构师如何学习 AI:三个月掌握核心能力的务实路径
人工智能·后端·程序员
小毅&Nora2 小时前
【后端】【SpringBoot】① 源码解析:从启动到优雅关闭
spring boot·后端·优雅关闭
嘻哈baby2 小时前
从TIME_WAIT爆炸到端口耗尽:Linux短连接服务排查与优化
后端