本教程适用于以下操作系统版本

  • Ubuntu 22.04 (Jammy Jellyfish)
  • Ubuntu 20.04 (Focal Fossa)

本页内容

  • 先决条件
  • 安装 Rust
  • 用 Rust 创建一个示例应用程序
  • 结论

Rust 是一种开源且在如今非常流行的编程语言,由 Graydon Hoare 于 2006 年开发。它极其快速,可防止段错误,并保证线程和内存安全。它支持零成本抽象、无数据竞争的线程、移动语义、高效的 C 绑定、极小运行时和模式匹配。它与 C++ 非常相似,可以在多个平台上运行。

在本教程中,我将向您展示如何在 Ubuntu 20.04 上安装 Rust 编程语言。

先决条件

  • 一台运行 Ubuntu 20.04 的服务器。
  • 服务器上已配置 root 密码。

安装 Rust

要安装 Rust,您需要在系统中安装 Curl 及其他软件包。

apt-get install curl build-essential make gcc -y

安装 Curl 软件包后,下载并运行 Rust 安装脚本,如下所示:

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:

  /root/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  /root/.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:

  /root/.cargo/bin

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

  /root/.profile
  /root/.bashrc

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 并按 Enter 继续。您应该得到以下输出:

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-06-17, rust version 1.53.0 (53cb7b09b 2021-06-17)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
 48.4 MiB /  48.4 MiB (100 %)  26.8 MiB/s in  1s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 16.1 MiB /  16.1 MiB (100 %)   1.9 MiB/s in  6s ETA:  0s
info: installing component 'rust-std'
 25.3 MiB /  25.3 MiB (100 %)   5.8 MiB/s in  4s ETA:  0s
info: installing component 'rustc'
 48.4 MiB /  48.4 MiB (100 %)   7.1 MiB/s in  7s 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.53.0 (53cb7b09b 2021-06-17)


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

安装完成后,您需要为当前 shell 激活 Rust 环境。您可以使用以下命令激活它:

source ~/.profile
source ~/.cargo/env

接下来,使用以下命令验证 Rust 的版本:

rustc --version

您应该得到以下输出:

rustc 1.53.0 (53cb7b09b 2021-06-17)

用 Rust 创建一个示例应用程序

接下来,您需要创建一个示例应用程序来测试 Rust。

首先,为您的应用程序创建一个目录:

mkdir app

接下来,将目录切换到 app,并使用以下命令创建一个示例应用程序:

cd app
nano app.rs

添加以下代码:

fn main() {
    println!("Hello World, this is my first app");
}

保存并关闭文件,然后使用以下命令编译程序:

rustc app.rs

这将在当前目录中创建一个名为 app 的可执行文件。

接下来,使用以下命令运行程序:

./app

您应该看到以下输出:

Hello World, this is my first app

要更新 Rust 软件包,请运行以下命令:

rustup update

您应该得到以下输出:

info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: checking for self-updates

  stable-x86_64-unknown-linux-gnu unchanged - rustc 1.53.0 (53cb7b09b 2021-06-17)

如果您想从系统中卸载 Rust,请运行以下命令:

rustup self uninstall

您应该得到以下输出:

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

结论

恭喜!您已在 Ubuntu 20.04 服务器上成功安装 Rust。现在您可以使用 Rust 编写速度极快且内存占用极低的代码。