在 Linux 中安装 Rust 编程语言

或者 休息时间 是一种现代、快速、跨平台的开源系统编程语言,旨在取代 C/C++,但具有高级抽象来取悦那些来自 C# 和 Java 的人。 它具有许多特性,包括零成本抽象、移动语义、保证内存安全、没有数据竞争的线程、基于特征的泛型、模式匹配、类型推断、最小运行时间和高效的 C 绑定等。Rust 正在生产中积极使用由 Canonical、Chef、Coursera、CoreOS、Dropbox、Mozilla、NPM 等流行组织提供。 今天,我们将学习在 Linux 中安装 Rust 编程语言。

内容

  1. 在 Linux 中安装 Rust 编程语言
  2. 测试 Rust 编程语言
  3. 故障排除
  4. 启用 Tab 补全
  5. 更新锈
  6. 卸载锈

在 Linux 中安装 Rust 编程语言

Rust 语言可以通过几种不同的方式安装。 官方推荐的安装 Rust 的方法是使用 生锈, t他是 Rust 工具链安装程序。 使用 rustup 安装 rust 非常简单。 您所要做的就是打开终端并运行以下命令:

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

或者,

$ curl https://sh.rustup.rs -sSf | sh

类型 1 (第一)以默认值继续安装。 或者,键入 2 自定义安装。 我要使用默认值,所以我输入了 1.

样本输出:

info: downloading installer  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:    /home/ostechnix/.cargo/bin  This can be modified with the CARGO_HOME environment variable.  Rustup metadata and toolchains will be installed into the Rustup home directory, located at:    /home/ostechnix/.rustup  This can be modified with the RUSTUP_HOME environment variable.  This path will then be added to your PATH environment variable by modifying the profile file located at:    /home/ostechnix/.profile  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                profile: default   modify PATH variable: yes  1) Proceed with installation (default) 2) Customize installation 3) Cancel installation >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 2020-06-18, rust version 1.44.1 (c7087fe00 2020-06-17) info: downloading component 'cargo'   5.0 MiB /   5.0 MiB (100 %)   3.3 MiB/s in  1s ETA:  0s info: downloading component 'clippy' info: downloading component 'rust-docs'  12.2 MiB /  12.2 MiB (100 %)   2.6 MiB/s in  5s ETA:  0s info: downloading component 'rust-std'  17.7 MiB /  17.7 MiB (100 %)   2.4 MiB/s in  8s ETA:  0s info: downloading component 'rustc'  60.3 MiB /  60.3 MiB (100 %)   2.2 MiB/s in 26s ETA:  0s info: downloading component 'rustfmt'   3.4 MiB /   3.4 MiB (100 %)   2.6 MiB/s in  1s ETA:  0s info: installing component 'cargo' info: installing component 'clippy' info: installing component 'rust-docs'  12.2 MiB /  12.2 MiB (100 %)   2.5 MiB/s in  4s ETA:  0s info: installing component 'rust-std'  17.7 MiB /  17.7 MiB (100 %)   8.6 MiB/s in  4s ETA:  0s info: installing component 'rustc'  60.3 MiB /  60.3 MiB (100 %)   7.2 MiB/s in  9s ETA:  0s info: installing component 'rustfmt' info: default toolchain set to 'stable'    stable installed - rustc 1.44.1 (c7087fe00 2020-06-17)  Rust is installed now. Great!  To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH environment variable. Next time you log in this will be done automatically.  To configure your current shell run source $HOME/.cargo/env
在 Linux 中使用 rustup 安装 rust 编程语言

正如您在上面的输出中看到的,rustup 安装程序脚本下载并安装了 Rust 编程语言的官方编译器,其包管理器名为 货物,并且它添加了 货物, 休息, 休息 和 Cargo 的 bin 目录的其他命令,位于 ~/.cargo/bin. 然后,它通过修改将 Cargo 的 bin 目录添加到您的 PATH 环境变量 轮廓 文件位于 ~/.profile.

最后,运行以下命令来配置您当前的 shell:

$ source $HOME/.cargo/env

要验证安装的版本,请运行:

$ rustc --version rustc 1.44.1 (c7087fe00 2020-06-17)

好吧! 我们刚刚安装了最新的 Rust 版本。 让我们继续通过创建一个示例 rust 程序来看看它是否有效。

测试 Rust 编程语言

创建一个主项目目录来放置你所有的 Rust 代码。这个目录作为我们所有 Rust 程序的父目录。

例如,我将创建一个名为 my_rust_projects 在我的 $HOME 目录。

$ mkdir ~/my_rust_projects

转到该目录:

$ cd ~/my_rust_projects

接下来创建一个二进制文件 ‘你好世界’ 使用 Cargo 打包,如下所示。

$ cargo new hello_world

上面的命令将创建一个名为“hello_world”的新目录,其中包含所有必要的文件。

将 CD 放入该目录:

$ cd hello_world

最后使用命令运行 hello_world 程序:

$ cargo run

样本输出:

Compiling hello_world v0.1.0 (/home/sk/my_rust_projects/hello_world)     Finished dev [unoptimized + debuginfo] target(s) in 0.44s      Running `target/debug/hello_world` Hello, world!
在 Rust 中运行 hello world 程序

这是使用 Cargo 的推荐方式。

或者,您可以手动创建项目目录,将代码写入文本文件,最后编译并运行它,如下所示。

让我们创建项目目录:

$ mkdir hello_world

cd 进入该目录:

$ cd hello_world

现在使用任何文本或 GUI 编辑器编写你的第一个 Rust 程序。 rust 文件总是以扩展名结尾 .rs.

编写一个示例 rust 程序,例如 ostechnix.rs, 做:

$ vi ostechnix.rs

复制并粘贴以下代码。

fn main() {     println!("Hello, Welcome To OSTechNix blog!"); } 

退出 键和类型 :wq 保存并退出文件。

接下来运行以下命令来编译 rust 代码。

$ rustc ostechnix.rs

上述命令将创建一个名为 ostechnix 在当前目录中。

$ ls ostechnix ostechnix.rs

最后用命令运行 Rust 程序:

$ ./ostechnix  Hello, Welcome To OSTechNix blog!
在 Linux 中用 Rust 编程语言运行 Hello World 程序在 Linux 中用 Rust 编程语言运行 Hello World 程序

它的工作!

故障排除

有时你可能会得到 “错误:链接器 cc 未找到” 使用 Cargo 编译程序时的消息。 要解决此问题,请按照以下链接中的说明安装 GCC 等 C 编译器。

  • 如何在 Linux 上修复 Rust 错误“linker ‘cc’ not found”

启用 Tab 补全

Rustup 支持流行 shell 的制表符补全,例如 重击, , Zsh电源外壳.

启用 Tab 补全 重击, 做:

$ mkdir -p ~/.local/share/bash-completion/completions
$ rustup completions bash >> ~/.local/share/bash-completion/completions/rustup

$ mkdir -p ~/.config/fish/completions
$ rustup completions fish > ~/.config/fish/completions/rustup.fish

Zsh

$ mkdir ~/.zfunc

然后将以下行添加到您的 ‘.zshrc’ 文件中,就在 ‘compinit’ 之前:

fpath+=~/.zfunc

现在您可以使用以下命令安装完成脚本:

$ rustup completions zsh > ~/.zfunc/_rustup

启用选项卡完成后,您应该注销并重新登录到您的 shell 会话以使更改生效。

更新锈

要在新版本发布时更新 rust,请运行:

$ rustup update info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' info: latest update on 2018-03-29, rust version 1.25.0 (84203cac6 2018-03-25) info: downloading component 'rustc' info: downloading component 'rust-std' info: downloading component 'cargo' info: downloading component 'rust-docs' info: removing component 'rustc' info: removing component 'rust-std' info: removing component 'cargo' info: removing component 'rust-docs' info: installing component 'rustc' info: installing component 'rust-std' info: installing component 'cargo' info: installing component 'rust-docs' info: checking for self-updates  stable-x86_64-unknown-linux-gnu updated - rustc 1.25.0 (84203cac6 2018-03-25)

此命令还检查更新 休息 并自动安装最新版本。

如果您想手动检查更新并安装最新版本的 休息 在不更新已安装的工具链的情况下,键入:

$ rustup self update

卸载锈

您可以随时使用以下命令卸载 rust 语言:

$ rustup self uninstall

此命令将从您的系统中删除 rust,并且所有上述更改都将被还原。

Thanks for hacking in Rust!  This will uninstall all Rust toolchains and data, and remove $HOME/.cargo/bin  from your PATH environment variable.  Continue? (y/N) y  info: removing rustup home info: removing cargo home info: removing rustup binaries info: rustup is uninstalled

最后,删除 rust 项目的父目录。

$ rm -fr ~/my_rust_projects

而且,这就是现在的全部。 您现在知道如何使用 rustup 安装 Rust,如何更新它,创建和运行示例 rust 程序,最后如何从系统中删除 rust。 希望这很有用。

资源:

  • 休息网站
  • Rustup GitHub 存储库

相关阅读:

  • 在 Linux 中使用 Conda 创建 Rust 虚拟环境

安装rustLinux开源编程编程语言Rust编程语言rust-lang