Rust(0) - cargo

剛開始接觸 Rust 時發現 Rust 編寫過的 Python 套件速度相當驚人,現在很常看到 Rust 用來編寫各種以加速為目的套件。

記憶體管理是程式效能的重點,GC 雖然帶來方便的記憶體釋放機制但也降低了系統效能。Rust 捨去 GC 搭配 zero-cost abstractions 實現高效能的系統,搭配並行及安全管理寫過的人就會愛上這門語言。

1. 利用 cargo 建立專案:

利用 cargo 工具建立一個 Rust 專案。

$ cargo new rust_test
$ cd rust_test

初始化後的專案架構,Cargo.toml 控制套件版本、main.rs 是入口。

.
├── Cargo.toml
└── src
    └── main.rs

編譯並運行 main.rs,看到 "Hello, world!" 就代表成功了。

$ cargo build
$ cargo run

編譯過的執行檔會以專案名稱來命名,本例會在 target/debug/ 生成執行檔 rust_test。

$ ./target/debug/rust_test

使用 cargo build --release 可以 build release 版本。

使用 cargo build --run 可以 run release 的執行檔。


留言

熱門文章