###安裝涉及的概念
rustup : 安裝rust和管理版本的工具,當前rust尚處於發展階段,存在三種類型的版本,穩定版、測試版、每日構建版本,使用rustup可以在這三種的版本之間切換,默認是穩定版本。通過rustup可以安裝rustc、cargo等工具。
cargo: rust的代碼組織管理工具,提供了一些列的工具來支撐從項目建立、構建、測試、運行直至部署整個流程。
rustc:rust語言的編譯器
###Linux Rust環境安裝:
當前Rust支持兩種安裝方式:
- 通過rust官網下載rustup安裝,linux在設置代理以后,通過如下命令進行下載:
https://forge.rust-lang.org/infra/other-installation-methods.html
curl https://sh.rustup.rs -sSf | sh
但是這種安裝方式在公司內部多個環境均沒有安裝成功過,每次安裝均報如下錯誤:
因此不推薦使用這種方式安裝。 - 直接在官網下載安裝包安裝:
2.1 rust編譯環境安裝
https://forge.rust-lang.org/infra/other-installation-methods.html
根據個人的環境下載安裝包,以linux為例,下載x86_64-unknown-linux-gnu安裝包,
解壓:tar –zxvf rust-1.41.0-x86_64-unknown-linux-gnu.tar.gz
進入目錄:執行./install.sh 完成安裝
檢查是否安裝成功,使用 rustc -V命令查看版本信息:
2.2 cargo 配置
修改cargo配置文件,在 ~/.cargo目錄下增加一個config文件,將如下內容加入到config文件
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'mirror'
[source.mirror]
registry = "http://mirrors.tools.huawei.com/rust/crates.io-index"
如果已有config文件,建議增加公司內部cargo的源,見上面config文件配置中repleace。mirror 部分的配置。
安裝完成后以一個實際的例子驗證cargo配置是否正確:
使用cargo新創建一個編譯工程:
cargo new demo --bin
修改src中的main.rs,將我們實際的demo代碼放入到main.rs:
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number !");
let secret_number = rand::thread_rng().gen_range(1,101);
println!("Please input your guess.");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.expect("Failed to read line");
println!("you guessed:{}",guess);
println!("The secret number is : {}",secret_number);
}
使用rustc 對工程進行編譯,出現如下錯誤:
出現如上錯誤的原因是,rust自帶的編譯器rustc在編譯時,不會去自動尋找第三方依賴,需要手動指定編譯,由於找到rand三方庫,因此使用rustc編譯直接報錯。
使用cargo可以解決包依賴的問題,cargo會自動尋找三方庫。
我們我們當前的代碼中依賴隨機數庫。 但是 Rust 語言核心和 Rust 標准庫都沒有提供生成隨機數的的方法或結構體。
我們只能借助於 https://crates.io/ 上其它開發者提供的第三方庫或 crates。
我們可以打開網站 crates.io 並在頂部的搜索中輸入 rand 然后回車來查找第三方隨機數生成庫。
從搜索的結果來看,由很多和隨機數生成的相關庫,不過我們只使用 rand。
點擊搜索結果中的 rand 會跳轉到 https://crates.io/crates/rand。
下圖是我們需要的 隨機數生成庫 rand 的基本信息截圖。
rand的版本是0.7.3,我們需要修改工程下的Cargo.toml文件,增加對rand庫版本的說明:
[package]
name = "demo"
version = "0.1.0"
authors = ["miles <senlan008@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand="0.7.3"
修改完成后,使用cargo build對工程進行構建:
root@ctup000107546:/home/rust/demo/src# cargo build
Updating `http://mirrors.tools.huawei.com/rust/crates.io-index` index
Downloaded cfg-if v0.1.10 (registry `http://mirrors.tools.huawei.com/rust/crates.io-index`)
Downloaded getrandom v0.1.14 (registry `http://mirrors.tools.huawei.com/rust/crates.io-index`)
Downloaded rand v0.7.3 (registry `http://mirrors.tools.huawei.com/rust/crates.io-index`)
Downloaded rand_core v0.5.1 (registry `http://mirrors.tools.huawei.com/rust/crates.io-index`)
Downloaded rand_chacha v0.2.2 (registry `http://mirrors.tools.huawei.com/rust/crates.io-index`)
Downloaded ppv-lite86 v0.2.8 (registry `http://mirrors.tools.huawei.com/rust/crates.io-index`)
Downloaded 6 crates (200.1 KB) in 0.65s
Compiling libc v0.2.72
Compiling getrandom v0.1.14
Compiling cfg-if v0.1.10
Compiling ppv-lite86 v0.2.8
Compiling rand_core v0.5.1
Compiling rand_chacha v0.2.2
Compiling rand v0.7.3
Compiling demo v0.1.0 (/home/rust/demo)
Finished dev [unoptimized + debuginfo] target(s) in 7.34s
root@ctup000107546:/home/rust/demo/src#
###Windows Rust環境安裝:
1、rust編譯環境安裝:
windows環境上安裝Rust需要先安裝MSVC或者gcc編譯器環境,由於涉A原因,建議安裝gcc,windows下gcc建議安裝minGW,minGW的安裝請參考:http://3ms.huawei.com/km/blogs/details/6223745
然后到rust官網下載 x86_64_pc_windows_gnu 安裝包安裝到windows上, 安裝完成后增加cargo配置文件:
在 C:/user/xxxx/.cargo目錄下增加config文件:
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'mirror'
[source.mirror]
registry = "http://mirrors.tools.huawei.com/rust/crates.io-index"
2、clion集成rust調測環境:
clion直接到toolcloud上下載安裝文件進行安裝。
安裝clion以后,安裝rust和Toml插件,到插件管理界面安裝插件

安裝完成后,重啟clion,在語言配置界面(File->Settings->Language&Frameworks)配置rust:
配置完成后就要可以在clion創建rust工程:
windows下安裝rust遇到如下問題:
安裝過rust之后,運行rustc或者cargo命令行命令時,出現error: no default toolchain configured 錯誤,解決此問題可通過
通過rustup重新指定link安裝目錄即可:
C:\Users\j00321907\.cargo\bin>rustup toolchain link mygnutoolchain "C:\Program Files\Rust stable GNU 1.44"
C:\Users\j00321907\.cargo\bin>rustup default mygnutoolchain
info: default toolchain set to 'mygnutoolchain'
C:\Users\j00321907\.cargo\bin>rustc.exe -V
rustc 1.44.1 (c7087fe00 2020-06-17)
