这是本节的多页打印视图。 点击此处打印.

返回本页常规视图.

Rust安装

在各个操作系统上安装Rust

Rust安装方式参考官方地址: https://www.rust-lang.org/en-US/install.html

1 - Linux&Mac安装

在Linux和Mac上安装Rust

安装

执行命令:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

然后依照屏幕提示:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

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:

  /home/sky/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  /home/sky/.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:

  /home/sky/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /home/sky/.profile
  /home/sky/.bashrc
  /home/sky/.zshenv

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

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               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: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2021-07-29, rust version 1.54.0 (a178d0322 2021-07-26)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 16.7 MiB /  16.7 MiB (100 %)   4.5 MiB/s in  3s ETA:  0s
info: downloading component 'rust-std'
 21.9 MiB /  21.9 MiB (100 %)   6.0 MiB/s in  4s ETA:  0s
info: downloading component 'rustc'
 50.1 MiB /  50.1 MiB (100 %)   8.7 MiB/s in  6s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 16.7 MiB /  16.7 MiB (100 %)  12.6 MiB/s in  1s ETA:  0s
info: installing component 'rust-std'
 21.9 MiB /  21.9 MiB (100 %)  16.0 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 50.1 MiB /  50.1 MiB (100 %)  17.8 MiB/s in  2s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.54.0 (a178d0322 2021-07-26)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source $HOME/.cargo/env

如果不想重新登录而立即生效,只需要在终端执行命令 source $HOME/.cargo/env 即可。

验证安装:

rustc --version
rustc 1.54.0 (a178d0322 2021-07-26)

配置cargo

打开(或创建)文件 ~/.cargo/config,加入以下内容:

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'

[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

参考:

2 - Windows安装

在Windows上安装Rust

安装vs2015

TBD: 下次验证,参考 https://rustlang-cn.org/office/rust/book/getting-started/ch01-01-installation.html

在安装rust之前,windows平台上需要先安装 Microsoft C++ build tools,推荐2015版本。如果不安装,后面在编译时,会报错说"link.exe"无法找到。

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that VS 2013 or VS 2015 was installed with the Visual C++ option

error: aborting due to previous error

error: Could not compile `ws2_32-sys`

打开下面的网站:

http://landinghub.visualstudio.com/visual-cpp-build-tools

选择"Download Visual C++ Build Tools 2015"。

下载之后按照提示一路安装即可。

windows安装rust

操作系统为windows 10 64位。

下载rustup.init.exe,然后安装,按照指示操作,中间要下载rustc等安装文件。

Rust Visual C++ prerequisites

If you will be targeting the GNU ABI or otherwise know what you are doing then
it is fine to continue installation without the build tools, but otherwise,
install the C++ build tools before proceeding.

Continue? (Y/n) y


Welcome to Rust!

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

It will add the cargo, rustc, rustup and other commands to Cargo's bin
directory, located at:

  C:\Users\aoxia\.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
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation


info: updating existing rustup installation


Rust is installed now. Great!

To get started you need Cargo's bin directory (%USERPROFILE%\.cargo\bin) in
your PATH environment variable. Future applications will automatically have the
correct environment, but you may need to restart your current shell.

Press the Enter key to continue.

%USERPROFILE%\.cargo\bin 加入到PATH环境变量中。

验证安装,执行rustc --version

$> rustc --version
rustc 1.33.0 (2aa4c46cf 2019-02-28) 

3 - Rust版本升级

升级Rust版本

官方方式

通过 rustup 安装了 Rust 之后,更新到最新版本只要运行如下更新脚本:

$ rustup update

尝试在mac下从 1.40 升级到 1.42 成功,输出如下:

$ rustup update
info: syncing channel updates for 'stable-x86_64-apple-darwin'
463.3 KiB / 463.3 KiB (100 %) 414.2 KiB/s in  1s ETA:  0s
info: latest update on 2020-03-12, rust version 1.42.0 (b8cedc004 2020-03-09)
info: downloading component 'rust-src'
  2.2 MiB /   2.2 MiB (100 %) 313.6 KiB/s in  8s ETA:  0s
info: downloading component 'cargo'
  3.7 MiB /   3.7 MiB (100 %) 321.3 KiB/s in 14s ETA:  0s
info: downloading component 'clippy'
  1.3 MiB /   1.3 MiB (100 %) 327.7 KiB/s in  5s ETA:  0s
info: downloading component 'rust-docs'
 12.1 MiB /  12.1 MiB (100 %) 323.2 KiB/s in 43s ETA:  0s
info: downloading component 'rust-std'
 16.1 MiB /  16.1 MiB (100 %) 345.6 KiB/s in  1m  3s ETA:  0s
info: downloading component 'rustc'
 54.5 MiB /  54.5 MiB (100 %) 342.4 KiB/s in  4m 20s ETA:  0s    
info: downloading component 'rustfmt'
  1.9 MiB /   1.9 MiB (100 %) 288.0 KiB/s in  7s ETA:  0s
info: removing previous version of component 'rust-src'
info: removing previous version of component 'cargo'
info: removing previous version of component 'clippy'
info: removing previous version of component 'rust-docs'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: removing previous version of component 'rustfmt'
info: installing component 'rust-src'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 12.1 MiB /  12.1 MiB (100 %)   8.6 MiB/s in  1s ETA:  0s
info: installing component 'rust-std'
info: installing component 'rustc'
 54.5 MiB /  54.5 MiB (100 %)  18.3 MiB/s in  3s ETA:  0s
info: installing component 'rustfmt'
info: checking for self-updates

  stable-x86_64-apple-darwin updated - rustc 1.42.0 (b8cedc004 2020-03-09) (from rustc 1.40.0 (73528e339 2019-12-16))

info: cleaning up downloads & tmp directories

尝试在linux 下从 1.54.0 升级到 1.55.0 成功,输出如下:

➜  ~ rustup update
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'

679.5 KiB / 679.5 KiB (100 %) 463.5 KiB/s in  1s ETA:  0s
info: latest update on 2021-09-09, rust version 1.55.0 (c8dfcfe04 2021-09-06)
info: downloading component 'rust-src'
info: downloading component 'cargo'
  6.1 MiB /   6.1 MiB (100 %)   4.4 MiB/s in  1s ETA:  0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 17.0 MiB /  17.0 MiB (100 %)   4.5 MiB/s in  3s ETA:  0s
info: downloading component 'rust-std'
 22.3 MiB /  22.3 MiB (100 %)   4.6 MiB/s in  5s ETA:  0s
info: downloading component 'rustc'
 51.0 MiB /  51.0 MiB (100 %)   4.2 MiB/s in 11s ETA:  0s
info: downloading component 'rustfmt'
info: removing previous version of component 'rust-src'
info: removing previous version of component 'cargo'
info: removing previous version of component 'clippy'
info: removing previous version of component 'rust-docs'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: removing previous version of component 'rustfmt'
info: installing component 'rust-src'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 17.0 MiB /  17.0 MiB (100 %)  11.3 MiB/s in  1s ETA:  0s
info: installing component 'rust-std'
 22.3 MiB /  22.3 MiB (100 %)  16.7 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 51.0 MiB /  51.0 MiB (100 %)  18.9 MiB/s in  2s ETA:  0s
info: installing component 'rustfmt'
info: checking for self-updates

  stable-x86_64-unknown-linux-gnu updated - rustc 1.55.0 (c8dfcfe04 2021-09-06) (from rustc 1.54.0 (a178d0322 2021-07-26))

info: cleaning up downloads & tmp directories

如果要卸载 Rust 和 rustup,运行如下卸载脚本:

$ rustup self uninstall

归档内容:曾经遇到的升级问题

尝试过升级已经安装的rust/cargo,试图从1.22.1升级到1.23,发现重新运行rustup脚本,虽然报告说安装成功,但是实际不会安装新的版本。

暂时没有找到升级的方法,只好用最笨的办法,先删除再全新安装:

cd
rm -rf .cargo/ .rustup/

这个方式理所当然的很不好,原有的所有内容都要重头来一次。

后面似乎没有再遇到类似问题。

4 - Clion设置

Clion安装和设置

下载

https://www.jetbrains.com/clion/download/

安装

安装clion

解压缩安装文件,按照 Install-Linux-tar.txt 的提示,执行

cd /bib
./clion.sh

启动clion界面之后,在左下角点 “Options Menu” 按钮,点 “Create Desktop Entry…” 创建启动菜单。

安装rust和toml插件

在 clion 的 “settings” -> “plugin” 中,安装 Rust 和 toml 两个插件。重启 clion。

配置

安装Rust标准库

进入 “settings” -> “language & framework” 下找到 “rust”,在 “Standard library” 那里选择 “Download via Rustup” 下载。

备注:如果下载速度慢,建议科学上网。

在CLion中Debug

详细介绍参考:Debugging - Rust | JetBrains

5 - Intellij Rust使用

Intellij Rust的使用技巧

背景

intellij-rust 网站:

https://intellij-rust.github.io/

格式化代码

Ctrl+Alt+L

TBD:这个方式格式化的代码,和 rustfmt 是否一致?待验证

选中代码

ctrl+w 扩大代码选择范围,可以连续使用

ctrl+shift+w 缩小代码选择范围,和 ctrl+w 扩大的顺序相反,可以连续使用

代码注释

选中代码之后(包括手工选择和 ctrl+w),Ctrl+Shift+/ 用块注释将代码注释为/* */Ctrl+/ 用行注释将代码注释为//

代码补全

Ctrl+Space 做代码补全,但是和输入法快捷键冲突。

Alt+/ 是 “dumb completion”

代码导航

Alt+F7 在caret中找到使用

Ctrl+B goto declaration

Ctrl+N goto class

Ctrl+Shift+Alt+N goto symbol,查找任何symbol(types, methods, functions, fields),配合 Ctrl+N 使用

Ctrl+U goto super

Ctrl+F12 在编辑页面弹出窗口显示当前文件的文件结构

Alt+7在打开一个导航栏显示当前文件的文件结构

Ctrl+Q 显示注释

Ctrl+Shift+P 显示表达式的类型

6 - Rust标准库中文文档

Rust标准库中文文档

https://github.com/wtklbm/rust-library-i18n

Rust 核心库和标准库的源码级中文翻译,可作为 IDE 工具的智能提示,也可以生成本地 API 文档

备注:更新到了 1.56.1 最新版本,真是不容易,鸣谢作者。

离线 HTML 文档

https://github.com/wtklbm/rust-library-i18n/blob/main/BuildHtml.md

参考上面的文档构建本地离线文档

备注:遇到一点小问题,提交了一个issue: https://github.com/wtklbm/rust-library-i18n/issues/7