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

相关推荐
我学上瘾了1 天前
Spring Cloud的前世今生
后端·spring·spring cloud
波波0071 天前
ASP.NET Core 健康检查实战:不只是一个 /health 接口
后端·asp.net
小码哥_常1 天前
Spring Boot 搭建邮件发送系统:开启你的邮件自动化之旅
后端
石榴树下的七彩鱼1 天前
图片修复 API 接入实战:网站如何自动去除图片水印(Python / PHP / C# 示例)
图像处理·后端·python·c#·php·api·图片去水印
我叫黑大帅1 天前
为什么TCP是三次握手?
后端·网络协议·面试
我叫黑大帅1 天前
如何排查 MySQL 慢查询
后端·sql·面试
techdashen1 天前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
消失的旧时光-19431 天前
Spring Boot 实战(五):接口工程化升级(统一返回 + 异常处理 + 错误码体系 + 异常流转机制)
java·spring boot·后端·解耦
Rust研习社1 天前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust
夕颜1111 天前
Skill 机器人 vs Hermes Agent:两种「AI 越用越聪明」的路径
后端