如何在 Linux 中安装 Rust 编程语言
创始人
2024-03-02 01:34:00
0

Rust 通常被称为 rust-lang。Rust 是一个由 Mozilla Research 赞助的通用的、多范式、现代的、跨平台和开源系统编程语言。

它旨在实现安全性、速度和并发性等目标。

Rust 在语法上与 C++ 相似,但它的设计者希望它在保持性能的同时提供更好的内存安全性。

Rust 目前在许多组织中使用,例如 Firefox、Chef、Dropbox、Oracle、GNOME 等。

如何在 Linux 中安装 Rust 语言?

我们可以通过多种方式安装 Rust,但以下是官方推荐的安装方式。

$ curl https://sh.rustup.rs -sSf | 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.

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

  /home/daygeek/.cargo/bin

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

  /home/daygeek/.profile
  /home/daygeek/.bash_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
  modify PATH variable: yes

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'
 77.7 MiB /  77.7 MiB (100 %)   1.2 MiB/s ETA:   0 s                
info: downloading component 'rust-std'
 54.2 MiB /  54.2 MiB (100 %)   1.2 MiB/s ETA:   0 s                
info: downloading component 'cargo'
  4.7 MiB /   4.7 MiB (100 %)   1.2 MiB/s ETA:   0 s                
info: downloading component 'rust-docs'
  8.5 MiB /   8.5 MiB (100 %)   1.2 MiB/s ETA:   0 s                
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
info: default toolchain set to 'stable'

  stable installed - rustc 1.31.0 (abe02cefd 2018-12-04)


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

运行以下命令配置当前 shell。

$ source $HOME/.cargo/env

运行以下命令验证已安装的 Rust 版本。

$ rustc --version
rustc 1.31.0 (abe02cefd 2018-12-04)

如何测试 Rust 编程语言?

安装 Rust 后,请按照以下步骤检查 Rust 语言是否正常工作。

$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world

创建一个文件并添加以下代码并保存。确保 Rust 文件始终以 .rs 扩展名结尾。

$ vi 2g.rs

fn main() {
 println!("Hello, It's 2DayGeek.com - Best Linux Practical Blog!");
}

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

$ rustc 2g.rs

上面的命令将在同一目录中创建一个可执行的 Rust 程序。

$ 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

运行 Rust 可执行文件得到输出。

$ ./2g
Hello, It's 2DayGeek.com - Best Linux Practical Blog!

好了!正常工作了。

将 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.31.0 (abe02cefd 2018-12-04)

运行以下命令从系统中删除 Rust 包。

$ rustup self uninstall

卸载 Rust 包后,删除 Rust 项目目录。

$ rm -fr ~/projects

via: https://www.2daygeek.com/how-to-install-rust-programming-language-in-linux/

作者:Prakash Subramanian 选题:lujun9972 译者:geekpi 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关内容

等了30年,微软MS-DO...
曾经称“开源是毒瘤”时有多么嫌弃,现在“微软 ❤️ 开源”就有多么...
2025-06-26 09:11:25
微软开源 Edit 命令行...
IT之家 5 月 20 日消息,微软昨日(5 月 19 日)发布博...
2025-05-20 08:16:36
做AI应用的最佳姿势:首选...
↓推荐关注↓ 最近我花了一些时间用 Tauri+SvelteKi...
2025-02-27 22:19:11
Rust 补丁两年零通过,...
整理|冬梅、明知山 策划|Tina 1Asahi Linux 首席...
2025-02-08 23:14:39
bypassSecurit...
问题描述:在Angular中使用bypassSecurityTru...
2025-01-12 20:02:27
不移动情况下将Rust的盒...
在Rust中,可以使用&符号来创建引用,这样可以在不移动变量的情况...
2025-01-11 06:00:23

热门资讯

Helix:高级 Linux ... 说到 基于终端的文本编辑器,通常 Vim、Emacs 和 Nano 受到了关注。这并不意味着没有其他...
使用 KRAWL 扫描 Kub... 用 KRAWL 脚本来识别 Kubernetes Pod 和容器中的错误。当你使用 Kubernet...
JStock:Linux 上不... 如果你在股票市场做投资,那么你可能非常清楚投资组合管理计划有多重要。管理投资组合的目标是依据你能承受...
通过 SaltStack 管理... 我在搜索Puppet的替代品时,偶然间碰到了Salt。我喜欢puppet,但是我又爱上Salt了:)...
Epic 游戏商店现在可在 S... 现在可以在 Steam Deck 上运行 Epic 游戏商店了,几乎无懈可击! 但是,它是非官方的。...
《Apex 英雄》正式可在 S... 《Apex 英雄》现已通过 Steam Deck 验证,这使其成为支持 Linux 的顶级多人游戏之...
如何在 Github 上创建一... 学习如何复刻一个仓库,进行更改,并要求维护人员审查并合并它。你知道如何使用 git 了,你有一个 G...
2024 开年,LLUG 和你... Hi,Linuxer,2024 新年伊始,不知道你是否已经准备好迎接新的一年~ 2024 年,Lin...
什么是 KDE Connect... 什么是 KDE Connect?它的主要特性是什么?它应该如何安装?本文提供了基本的使用指南。科技日...
Opera 浏览器内置的 VP... 昨天我们报道过 Opera 浏览器内置了 VPN 服务,用户打开它可以防止他们的在线活动被窥视。不过...