如何在Linux中安裝Rust編程語言
Rust 通常被稱為 rust-lang。Rust 是一個(gè)由 Mozilla Research 贊助的通用的、多范式、現(xiàn)代的、跨平臺(tái)和開源系統(tǒng)編程語言。
它旨在實(shí)現(xiàn)安全性、速度和并發(fā)性等目標(biāo)。
Rust 在語法上與 C++ 相似,但它的設(shè)計(jì)者希望它在保持性能的同時(shí)提供更好的內(nèi)存安全性。
Rust 目前在許多組織中使用,例如 Firefox、Chef、Dropbox、Oracle、GNOME 等。
如何在 Linux 中安裝 Rust 語言?
我們可以通過多種方式安裝 Rust,但以下是官方推薦的安裝方式。
$ curl https://sh.rustup.rs -sSf | shinfo: downloading installerWelcome to Rust!This will download and install the official compiler for the Rust programminglanguage, and its package manager, Cargo.It will add the cargo, rustc, rustup and other commands to Cargo's bindirectory, located at:/home/daygeek/.cargo/binThis path will then be added to your PATH environment variable by modifying theprofile files located at:/home/daygeek/.profile/home/daygeek/.bash_profileYou can uninstall at any time with rustup self uninstall and these changes willbe reverted.Current installation options:default host triple: x86_64-unknown-linux-gnudefault toolchain: stablemodify PATH variable: yes1) Proceed with installation (default)2) Customize installation3) Cancel installation>1info: 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 sinfo: downloading component 'rust-std'54.2 MiB / 54.2 MiB (100 %) 1.2 MiB/s ETA: 0 sinfo: downloading component 'cargo'4.7 MiB / 4.7 MiB (100 %) 1.2 MiB/s ETA: 0 sinfo: downloading component 'rust-docs'8.5 MiB / 8.5 MiB (100 %) 1.2 MiB/s ETA: 0 sinfo: 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 PATHenvironment variable. Next time you log in this will be done automatically.To configure your current shell run source $HOME/.cargo/env
運(yùn)行以下命令配置當(dāng)前 shell。
$ source $HOME/.cargo/env
運(yùn)行以下命令驗(yàn)證已安裝的 Rust 版本。
$ rustc --versionrustc 1.31.0 (abe02cefd 2018-12-04)
如何測試 Rust 編程語言?
安裝 Rust 后,請(qǐng)按照以下步驟檢查 Rust 語言是否正常工作。
$ mkdir ~/projects$ cd ~/projects$ mkdir hello_world$ cd hello_world
創(chuàng)建一個(gè)文件并添加以下代碼并保存。確保 Rust 文件始終以 .rs 擴(kuò)展名結(jié)尾。
$ vi 2g.rsfn main() {println!("Hello, It's 2DayGeek.com - Best Linux Practical Blog!");}
運(yùn)行以下命令編譯 rust 代碼。
$ rustc 2g.rs
上面的命令將在同一目錄中創(chuàng)建一個(gè)可執(zhí)行的 Rust 程序。
$ ls -lhtotal 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
運(yùn)行 Rust 可執(zhí)行文件得到輸出。
$ ./2gHello, It's 2DayGeek.com - Best Linux Practical Blog!
好了!正常工作了。
將 Rust 更新到***版本。
$ rustup updateinfo: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'info: checking for self-updatesstable-x86_64-unknown-linux-gnu unchanged - rustc 1.31.0 (abe02cefd 2018-12-04)
運(yùn)行以下命令從系統(tǒng)中刪除 Rust 包。
$ rustup self uninstall
卸載 Rust 包后,刪除 Rust 項(xiàng)目目錄。
$ rm -fr ~/projects
































