2018-12-21 16:59:08 +08:00
|
|
|
|
[#]: collector: (lujun9972)
|
2018-12-24 09:03:09 +08:00
|
|
|
|
[#]: translator: (geekpi)
|
2018-12-28 23:48:04 +08:00
|
|
|
|
[#]: reviewer: (wxy)
|
2018-12-28 23:48:39 +08:00
|
|
|
|
[#]: publisher: (wxy)
|
|
|
|
|
[#]: url: (https://linux.cn/article-10392-1.html)
|
2018-12-21 16:59:08 +08:00
|
|
|
|
[#]: subject: (How To Install Rust Programming Language In Linux)
|
|
|
|
|
[#]: via: (https://www.2daygeek.com/how-to-install-rust-programming-language-in-linux/)
|
|
|
|
|
[#]: author: (Prakash Subramanian https://www.2daygeek.com/author/prakash/)
|
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
如何在 Linux 中安装 Rust 编程语言
|
2018-12-21 16:59:08 +08:00
|
|
|
|
======
|
|
|
|
|
|
2018-12-28 23:48:04 +08:00
|
|
|
|
Rust 通常被称为 rust-lang。Rust 是一个由 Mozilla Research 赞助的通用的、多范式、现代的、跨平台和开源系统编程语言。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
它旨在实现安全性、速度和并发性等目标。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
Rust 在语法上与 C++ 相似,但它的设计者希望它在保持性能的同时提供更好的内存安全性。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
Rust 目前在许多组织中使用,例如 Firefox、Chef、Dropbox、Oracle、GNOME 等。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
### 如何在 Linux 中安装 Rust 语言?
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
我们可以通过多种方式安装 Rust,但以下是官方推荐的安装方式。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ curl https://sh.rustup.rs -sSf | sh
|
|
|
|
|
info: downloading installer
|
|
|
|
|
|
|
|
|
|
Welcome to Rust!
|
|
|
|
|
|
2018-12-28 23:48:04 +08:00
|
|
|
|
This will download and install the official compiler for the Rust programming
|
2018-12-21 16:59:08 +08:00
|
|
|
|
language, and its package manager, Cargo.
|
|
|
|
|
|
2018-12-28 23:48:04 +08:00
|
|
|
|
It will add the cargo, rustc, rustup and other commands to Cargo's bin
|
2018-12-21 16:59:08 +08:00
|
|
|
|
directory, located at:
|
|
|
|
|
|
2018-12-28 23:48:04 +08:00
|
|
|
|
/home/daygeek/.cargo/bin
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
This path will then be added to your PATH environment variable by modifying the
|
|
|
|
|
profile files located at:
|
|
|
|
|
|
2018-12-28 23:48:04 +08:00
|
|
|
|
/home/daygeek/.profile
|
|
|
|
|
/home/daygeek/.bash_profile
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
You can uninstall at any time with rustup self uninstall and these changes will
|
|
|
|
|
be reverted.
|
|
|
|
|
|
|
|
|
|
Current installation options:
|
|
|
|
|
|
2018-12-28 23:48:04 +08:00
|
|
|
|
default host triple: x86_64-unknown-linux-gnu
|
|
|
|
|
default toolchain: stable
|
|
|
|
|
modify PATH variable: yes
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
1) Proceed with installation (default)
|
|
|
|
|
2) Customize installation
|
|
|
|
|
3) Cancel installation
|
|
|
|
|
>1
|
|
|
|
|
|
|
|
|
|
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
|
|
|
|
|
info: latest update on 2018-12-06, rust version 1.31.0 (abe02cefd 2018-12-04)
|
|
|
|
|
info: downloading component 'rustc'
|
2018-12-28 23:48:04 +08:00
|
|
|
|
77.7 MiB / 77.7 MiB (100 %) 1.2 MiB/s ETA: 0 s
|
2018-12-21 16:59:08 +08:00
|
|
|
|
info: downloading component 'rust-std'
|
2018-12-28 23:48:04 +08:00
|
|
|
|
54.2 MiB / 54.2 MiB (100 %) 1.2 MiB/s ETA: 0 s
|
2018-12-21 16:59:08 +08:00
|
|
|
|
info: downloading component 'cargo'
|
2018-12-28 23:48:04 +08:00
|
|
|
|
4.7 MiB / 4.7 MiB (100 %) 1.2 MiB/s ETA: 0 s
|
2018-12-21 16:59:08 +08:00
|
|
|
|
info: downloading component 'rust-docs'
|
2018-12-28 23:48:04 +08:00
|
|
|
|
8.5 MiB / 8.5 MiB (100 %) 1.2 MiB/s ETA: 0 s
|
2018-12-21 16:59:08 +08:00
|
|
|
|
info: installing component 'rustc'
|
|
|
|
|
info: installing component 'rust-std'
|
|
|
|
|
info: installing component 'cargo'
|
|
|
|
|
info: installing component 'rust-docs'
|
|
|
|
|
info: default toolchain set to 'stable'
|
|
|
|
|
|
2018-12-28 23:48:04 +08:00
|
|
|
|
stable installed - rustc 1.31.0 (abe02cefd 2018-12-04)
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Rust is installed now. Great!
|
|
|
|
|
|
2018-12-28 23:48:04 +08:00
|
|
|
|
To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
|
2018-12-21 16:59:08 +08:00
|
|
|
|
environment variable. Next time you log in this will be done automatically.
|
|
|
|
|
|
|
|
|
|
To configure your current shell run source $HOME/.cargo/env
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
运行以下命令配置当前 shell。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ source $HOME/.cargo/env
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
运行以下命令验证已安装的 Rust 版本。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ rustc --version
|
|
|
|
|
rustc 1.31.0 (abe02cefd 2018-12-04)
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
### 如何测试 Rust 编程语言?
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
安装 Rust 后,请按照以下步骤检查 Rust 语言是否正常工作。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ mkdir ~/projects
|
|
|
|
|
$ cd ~/projects
|
|
|
|
|
$ mkdir hello_world
|
|
|
|
|
$ cd hello_world
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
创建一个文件并添加以下代码并保存。确保 Rust 文件始终以 .rs 扩展名结尾。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ vi 2g.rs
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
println!("Hello, It's 2DayGeek.com - Best Linux Practical Blog!");
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
运行以下命令编译 rust 代码。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ rustc 2g.rs
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
上面的命令将在同一目录中创建一个可执行的 Rust 程序。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ ls -lh
|
|
|
|
|
total 3.9M
|
|
|
|
|
-rwxr-xr-x 1 daygeek daygeek 3.9M Dec 14 11:09 2g
|
|
|
|
|
-rw-r--r-- 1 daygeek daygeek 86 Dec 14 11:09 2g.rs
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
运行 Rust 可执行文件得到输出。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ ./2g
|
|
|
|
|
Hello, It's 2DayGeek.com - Best Linux Practical Blog!
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
好了!正常工作了。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
将 Rust 更新到最新版本。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ rustup update
|
|
|
|
|
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
|
|
|
|
|
info: checking for self-updates
|
|
|
|
|
|
2018-12-28 23:48:04 +08:00
|
|
|
|
stable-x86_64-unknown-linux-gnu unchanged - rustc 1.31.0 (abe02cefd 2018-12-04)
|
2018-12-21 16:59:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
运行以下命令从系统中删除 Rust 包。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ rustup self uninstall
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-27 08:53:41 +08:00
|
|
|
|
卸载 Rust 包后,删除 Rust 项目目录。
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ rm -fr ~/projects
|
|
|
|
|
```
|
2018-12-28 23:48:04 +08:00
|
|
|
|
|
2018-12-21 16:59:08 +08:00
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
via: https://www.2daygeek.com/how-to-install-rust-programming-language-in-linux/
|
|
|
|
|
|
|
|
|
|
作者:[Prakash Subramanian][a]
|
|
|
|
|
选题:[lujun9972][b]
|
2018-12-27 08:53:41 +08:00
|
|
|
|
译者:[geekpi](https://github.com/geekpi)
|
2018-12-28 23:48:04 +08:00
|
|
|
|
校对:[wxy](https://github.com/wxy)
|
2018-12-21 16:59:08 +08:00
|
|
|
|
|
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
|
|
|
|
|
|
[a]: https://www.2daygeek.com/author/prakash/
|
2018-12-28 23:48:04 +08:00
|
|
|
|
[b]: https://github.com/lujun9972
|