在Linux中安装Rust编程语言
Rust或者rust-lang是一种现代,快速,跨平台的开源系统编程语言,旨在替代C/C++,但具有高级抽象功能,可满足来自Cand Java的要求。
它具有许多功能,包括零成本抽象,移动语义,保证的内存安全性,无数据争用的线程,基于特征的泛型,模式匹配,类型推断,最少的运行时间以及有效的C绑定等。
Rust正在生产中积极使用由诸如Canonical,Chef,Coursera,CoreOS,Dropbox,Mozilla,NPM等热门组织组成。
今天,我们将学习在Linux中安装Rust编程语言。
在Linux中安装Rust编程语言
可以用几种不同的方式安装Rust语言。
官方推荐的安装Rust的方法是使用Rust工具链安装程序Rustup。
使用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/theitroad/.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/theitroad/.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/theitroad/.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 2017-06-18, rust version 1.44.1 (c7087fe00 2017-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 2017-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
如我们在上面的输出中看到的,rustup安装程序脚本下载并安装了Rust编程语言的官方编译器及其名为Cargo的程序包管理器,并且将cargo,rustc,rustup和其他命令添加到位于以下位置的Cargo的bin目录中~/.cargo/bin。
然后,通过修改~/.profile中的配置文件,将Cargo的bin目录添加到PATH环境变量中。
最后,运行以下命令来配置当前的shell:
$source $HOME/.cargo/env
要验证安装的版本,请运行:
$rustc --version rustc 1.44.1 (c7087fe00 2017-06-17)
好的!我们刚刚安装了最新的Rust版本。
让我们继续前进,通过创建一个示例生锈程序来查看它是否有效。
测试Rust编程语言
创建一个主项目目录以放置所有Rust代码。
该目录作为我们所有Rust程序的父目录。
例如,我将在$HOME目录中创建一个名为“ my_rust_projects”的主项目目录。
$mkdir ~/my_rust_projects
转到该目录:
$cd ~/my_rust_projects
接下来,使用Cargo创建一个二进制的“ hello_world”包,如下所示。
$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!
这是推荐的使用Cargo方式。
或者,我们可以手动创建项目的目录,将代码编写到文本文件中,然后最终像下面那样编译并运行它。
让我们创建项目目录:
$mkdir hello_world
CD进入该目录:
$cd hello_world
现在,使用任何文本或者GUI编辑器编写第一个Rust程序。
rust文件始终以.rs扩展名结尾。
要编写rust示例程序,例如theitroad.rs,请执行以下操作:
$vi theitroad.rs
将以下代码复制并粘贴到其中。
fn main() { println!("Hello, Welcome To theitroad blog!"); }
按ESC键,然后键入:wq以保存并退出文件。
接下来运行以下命令来编译rust代码。
$rustc theitroad.rs
上面的命令将在当前目录中创建一个名为theitroad的可执行rust程序。
$ls theitroad theitroad.rs
最后使用以下命令运行Rust程序:
$./theitroad Hello, Welcome To theitroad blog!
工作正常!!
故障排除
有时,使用Cargo编译程序时,我们可能会收到“错误:找不到链接器cc”的消息。
要解决此问题,请按以下链接中所述安装C编译器(如GCC)。
如何在Linux上修复Rust错误“找不到链接器'cc'”
启用制表符完成
Rustup支持流行的shell(例如Bash,Fish,Zsh和Powershell)的制表符补全。
要为Bash启用制表符补全,请执行以下操作:
$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
然后在“ compinit”之前,将以下几行添加到“ .zshrc”文件中:
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 2016-03-29, rust version 1.25.0 (84203cac6 2016-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 2016-03-25)
该命令还会检查对rustup的更新,并自动安装最新版本。
如果要手动检查更新并安装rustup的最新版本而不更新已安装的工具链,请键入:
$rustup self update
卸载Rust
我们可以随时使用以下命令卸载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
最后,删除rust项目的父目录。
$rm -fr ~/my_rust_projects