手机版
你好,游客 登录 注册
背景:
阅读新闻

如何在Linux中安装Rust编程语言

[日期:2019-03-02] 来源:Linux公社  作者:醉落红尘 [字体: ]

Rust(俗称Rust-Lang)是一种相对较新的开源实用系统编程语言,运行速度极快,可防止段错误,并保证线程安全。 它是由Mozilla开发并由LLVM支持的安全并发语言。

它支持零成本抽象,移动语义,保证内存安全,没有数据争用的线程,基于特征的泛型和模式匹配。 它还支持类型推断,最小运行时以及高效的C绑定。

Rust可以在很多平台上运行,并且正在被Dropbox,CoreOS,NPM等公司/组织用于生产。

在本文中,我们将展示如何在Linux中安装Rust编程语言并设置系统以开始编写带有Rust的程序。

在Linux中安装Rust编程语言

要安装Rust,请使用以下官方方法通过installer-script安装rust,这需要curl命令行下载,如图所示。

$ sudo apt-get install curl  [在 Debian/Ubuntu 上]
# yum install install curl  [在 CentOS/RHEL 上]
# dnf install curl          [在Fedora 上]

然后通过在终端中运行以下命令来安装Rust,并按照屏幕上的说明进行操作。请注意,实际安装了Rust,并且由Rust工具管理。

示例:

linuxidc@linuxidc:~/www.linuxidc.com$ 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/linuxidc/.cargo/bin

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

  /home/linuxidc/.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

如何在Linux中安装Rust编程语言

在Linux中安装Rust

>1

info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
320.9 KiB / 320.9 KiB (100 %) 254.7 KiB/s ETA:  0 s               
info: latest update on 2019-02-28, rust version 1.33.0 (2aa4c46cf 2019-02-28)
info: downloading component 'rustc'
 66.7 MiB /  84.7 MiB ( 79 %)  19.2 KiB/s ETA: 963 s                     
 70.9 MiB /  84.7 MiB ( 84 %)  19.2 KiB/s ETA: 738 s                     
 71.0 MiB /  84.7 MiB ( 84 %)  19.2 KiB/s ETA: 735 s               
 71.0 MiB /  84.7 MiB ( 84 %)  19.2 KiB/s ETA: 734 s               


 84.7 MiB /  84.7 MiB (100 %)  76.8 KiB/s ETA:  0 s               
info: downloading component 'rust-std'
 35.5 MiB /  56.8 MiB ( 62 %)  41.6 KiB/s ETA: 525 s                     
 56.8 MiB /  56.8 MiB (100 %)  99.2 KiB/s ETA:  0 s               
info: downloading component 'cargo'
  4.4 MiB /  4.4 MiB (100 %)  44.8 KiB/s ETA:  0 s               
info: downloading component 'rust-docs'
  8.5 MiB /  8.5 MiB (100 %)  67.2 KiB/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.33.0 (2aa4c46cf 2019-02-28)


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中安装Rust编程语言

在Linux中安装Rust

Rust安装完成后,Cargo的bin目录(~/.cargo/bin - 安装了所有工具)将添加到PATH环境变量~/.profile中。

在安装过程中,rustup会尝试将cargo的bin目录添加到PATH中;如果由于某种原因失败,请手动操作以开始使用Rust。

将Rust Cargo Bin目录添加到PATH

将Rust Cargo Bin目录添加到PATH

export PATH="/home/linuxidc/.cargo/bin:$PATH"

接下来,使用修改后的PATH来源~/.profile文件,并通过运行这些命令配置当前shell以使用Rust环境。

linuxidc@linuxidc:~/www.linuxidc.com$ source ~/.profile
linuxidc@linuxidc:~/www.linuxidc.com$ source ~/.cargo/env

最后,运行以下命令验证系统上安装的Rust的版本。

linuxidc@linuxidc:~/www.linuxidc.com$ rustc --version
rustc 1.33.0 (2aa4c46cf 2019-02-28)

就是目前最新版本,见《Rust 1.33 发布,对const fns的重大改进  https://www.linuxidc.com/Linux/2019-03/157184.htm

 检查在Linux中安装的Rust版本

检查在Linux中安装的Rust版本

在Linux中测试Rust编程语言

现在您已经在系统上安装了Rust,您可以通过创建第一个Rust程序来测试它,如下所示。首先创建一个程序文件所在的目录。

$ mkdir linuxidc.com
$ cd linuxidc.com

创建一个名为linuxidc.rs的文件,将以下代码行复制并粘贴到该文件中。

fn main() {
    println!("Hello World, www.linuxidc.com - 互联网上Linux操作系统指南!");
}

然后运行以下命令,该命令将在当前目录中创建名为linuxidc的可执行文件。

linuxidc@linuxidc:~/www.linuxidc.com$ rustc linuxidc.rs

最后,如图所示执行测试。

linuxidc@linuxidc:~/www.linuxidc.com$ ./linuxidc
Hello World, www.linuxidc.com - 互联网上Linux操作系统指南!

用Rust语言编写程序

用Rust语言编写程序

重要提示:您应该注意有关Rust发布的这些要点:

  • 必须有一个为期6周的快速发布过程,确保随时可以获得许多Rust。
  • 其次,所有这些构建都通过在每个支持的平台上以一致的方式进行Rust来管理,从而允许从beta和夜间释放通道安装Rust,并支持其他交叉编译目标。

Rust主页:https://www.rust-lang.org/en-US/

在本文中,我们已经解释了如何在Linux中安装和使用Rust编程语言。试试看,并通过下面的留言提供您的反馈或分享任何疑问。

下面关于Rust的文章您也可能喜欢,不妨参考下:

Rust 的详细介绍请点这里
Rust 的下载地址请点这里

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2019-03/157229.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款