安裝
# 安裝正式版本, 下載安裝腳本
[root@localhost ~]# wget https://sh.rustup.rs -O rustup-init.sh
[root@localhost ~]# chmod 755 rustup-init.sh
# 運行安裝腳本, 交互提示選擇1默認安裝
[root@localhost ~]# ./rustup-init.sh
# 或者 安裝nightly版本
[root@localhost ~]# wget https://static.rust-lang.org/rustup.sh -O rustup-nightly.sh
[root@localhost ~]# chmod 755 rustup-nightly.sh
[root@localhost ~]# ./rustup-nightly.sh --channel=nightly
# 查看安裝好了之后, rust 各組件命令的二進制文件.
[root@localhost ~]# ls ~/.cargo/bin/
cargo rls rustc rustdoc rust-gdb rust-lldb rustup
# 手動將rust可執(zhí)行文件目錄加入到系統(tǒng)環(huán)境變量.
# 下次再次登錄時就可以直接運行rust相關(guān)命令了.
[root@localhost ~]# echo "export PATH=\"$HOME/.cargo/bin:$PATH\" " >> ~/.bashrc
?
補充說明
rust并不會像其他編程語言一樣, 直接將所有命令放置在系統(tǒng)的標準位置; 而是放置在當前用戶的~/.cargo/bin目錄下, 我的理解是誰安裝它就誰可以用它, 由于我現(xiàn)在是處于學(xué)習(xí)狀態(tài), 所以暫時就不考慮如何標準部署安裝.
- cargo 是一個包管理器、項目工程依賴發(fā)布管理工具
- rustc 是rust語言的解釋器
- rustup 是一個升級或者卸載的工具
- 其他暫時不知道
?
驗證
運行命令
# 將rust科執(zhí)行文件路徑加入到當前環(huán)境變量中
[root@localhost ~]# source ~/.bashrc
# 運行rustc, 查看版本號
[root@localhost ~]# rustc --version
rustc 1.18.0 (03fc9d622 2017-06-06)
重新登錄, 再次運行
# 退出當前登錄
[root@localhost ~]# exit
# 再次登錄(不需要執(zhí)行 source ~/.bashrc)
[root@localhost ~]# rustc --version
rustc 1.18.0 (03fc9d622 2017-06-06)
?
Hello World
使用 cargo 創(chuàng)建一個項目
# 創(chuàng)建一個項目集目錄
[root@localhost ~]# mkdir projects
[root@localhost ~]# cd projects/
# 用cargo創(chuàng)建一個helloworld程序目錄
[root@localhost projects]# cargo new helloworld --bin
Created binary (application) `helloworld` project
# 進入程序目錄
[root@localhost projects]# cd helloworld
# 查看cargo都做了什么
[root@localhost helloworld]# ls
Cargo.toml src
# 查看main.rs源文件
[root@localhost helloworld]# cat src/main.rs
fn main() {
println!("Hello, world!");
}
# 查看Cargo.html文件
[root@localhost helloworld]# cat Cargo.toml
[package]
name = "helloworld"
version = "0.1.0"
authors = ["zhengtong <zhengtong0898@aliyun.com>"]
[dependencies]
# 運行代碼
[root@localhost helloworld]# cargo run
Compiling helloworld v0.1.0 (file:///root/projects/helloworld)
Finished dev [unoptimized + debuginfo] target(s) in 0.46 secs
Running `target/debug/helloworld`
Hello, world!
不使用 cargo 創(chuàng)建一個項目
[root@localhost ~]# cd ~/projects/
# 創(chuàng)建helloworld_manual項目目錄
[root@localhost ~]# mkdir ~/helloworld_manual
[root@localhost ~]# cd ~/helloworld_manual
# 創(chuàng)建程序文件
[root@localhost helloworld_manual]# vim main.rs
fn main() {
println!("Hello, world!");
}
# 將源代碼編譯成二進制文件
[root@localhost helloworld_manual]# rustc main.rs
# 查看變編譯后的文件分布狀態(tài)
[root@localhost helloworld_manual]# ls
main main.rs
# 運行main二進制文件
[root@localhost helloworld_manual]# ./main
Hello, world!
rust 是工程語言
不知道在哪, 反正就是聽說了rust是一名工程性質(zhì)的語言, 它對項目的工程化是比較有要求的; 因此專門開發(fā)了一個cargo工具, 通過cargo我可以將編譯、運行兩個拆分執(zhí)行步驟合并到一起, 只需要cargo run它就會幫我編譯然后幫我運行程序。
-
fn
聲明一個函數(shù)的定義 -
main
是函數(shù)名稱, 同事它也是一個特殊的函數(shù), 它在項目文件中是必須存在的, 運行入口在這里. -
括號
()
括號用來傳遞參數(shù). -
println!
是rust內(nèi)置的打印到標準輸出的命令, !是一個宏(macro), 暫時還不是很清楚它的本質(zhì)含義. -
分號
;
每行執(zhí)行代碼(花括號后面不需要)后面都要寫上分號. -
rustc
用來將源代碼文件編譯成二進制文件, 它會檢查語法. -
cargo
在單一文件中看不出來它的作用, 但是實際上它負責著項目依賴組件的管理(自動下載)、更新和各個依賴組件的版本號控制; -
cargo run
將編譯和運行兩個步驟合并在一起;