介紹Rust(1): 安裝與包管理工具
來自簡書
介紹Rust
Rust 是由Mozilla主導開發的專門用來編寫高效能應用程式的系統級程式語言, 也是一門強調安全、併發、高效的語言。 Graydon Hoare從2006年開發Rust,之後Mozilla對Rust很感興趣,並把Graydon吸收到Mozilla社群。在大家的一番努力下,終於在2015年5月份釋出了 Rust 正式版 v1.0.0。
安裝
使用安裝指令碼快速安裝,從之後踩得坑來看,mac 系統 、Linux、Win Subsystem 只推薦
使用這種方式安裝:
curl https://sh.rustup.rs -sSf | sh
如何檢視版本呢?
rustc -V
不想安裝Rust 環境呢?
Rust線上執行工具play-rust 不會讓你上當,就看你本地的網路爭不爭氣了。
rust 起手式
//hi.rs
fn main(){
println!("hello rust")
}
複製程式碼
編譯執行
rustc hi.rs #編譯出 hi執行檔案
./hi #執行
複製程式碼
C / Golang / Rust 分別列印hello world
,看下它們編譯出的檔案大小。
61B hi-c.c
8.3K hi-c
72B hi-go.go
1.1M hi-go
39B hi.rs
477K hi-rust
複製程式碼
rust的477K比C的8K大不少,這裡要吐槽下Go了,Go 預設靜態編譯足足有1.1M啊。不過比起 Rust的編譯時間,Go這點硬碟空間又算得了什麼呢。
Rust Cargo
Car-go 是Rust的包管理工具,集成了 下載/管理依賴/測試/編譯,跟npm pip bundler 作用類似,使用Cargo沒有Make 、CMake帶來的那種尷尬,不會被不知所云的Makefile折磨的死去活來。這點上 Go的包管理工具快快受死吧。
檢視cargo的版本 cargo -V
,在安裝rust的時候,cargo也一併安裝了。
使用 cargo新建個專案
cargo new hyper --bin
hyper
├── Cargo.toml # 專案配置檔案,類似npm的pakage.json
└── src
└── main.rs
複製程式碼
Cargo.toml
使用Toml檔案配置。 Toml 是一種極簡的配置檔案格式。
# Cargo.toml
[package]
name = "hyper"
version = "0.0.1"
authors = ["shooter"]
[dependencies]
time = "0.1.12"
regex = "0.1.41"
rocket = "0.3.16"
複製程式碼
編譯
cargo build
hyper
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
└── debug
├── build
├── deps
├── examples
├── hyper
├── hyper.d
├── incremental
└── native
複製程式碼
更新
cargo update
執行
cargo run
Cargo跟 ruby-bundler有很多相似之處,Rust 最初的包管理專案失敗後, Mozilla 請了 Yehuda Katz 和 Carl Lerche 開發了Cargo。他們正是Bundler的主要作者。
請看下一章 介紹Rust(2): 基本數字型別
參考: crates.io/
www.rust-lang.org/en-US/insta…
zhuanlan.zhihu.com/time-and-sp… 某Rust大佬的知乎專欄