Rust 2018開發環境配置與開發效率工具集
文件列表見:ofollow,noindex">Rust 移動端跨平臺複雜圖形渲染專案開發系列總結(目錄)
一句話概括:macOS/Linux使用者首選CLion + Rust外掛,折騰VSCode收益太低 。以下內容來自參與開發gfx-rs/hal 、gfx-rs/wgpu 等Rust主流開源圖形專案時所作嘗試的總結。
配置Rust編譯環境
使用Rust開發macOS、iOS、Android等跨平臺共享原始碼的專案,開發環境避免不了這些系統所要求的開發環境,即:
- macOS、iOS需要安裝Xcode
- Android需要Android Studio、Android SDK、Android NDK,並且配置SDK、NDK到環境變數。如果不想手工配置SDK、NDK變數,對於macOS,推薦先安裝Android Studio到Application,之後通過Android Studio安裝Android SDK、NDK,然後向profile、zsh配置檔案等寫入SDK、NDK變數。
-
修改Rust軟體更新源為中科大站點對國內使用者而言可以提高下載速度,已翻牆可不考慮。
// 1. 開啟環境變數配置檔案 vi ~/.bashrc // 2. 加入如下內容 export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup // 3. 啟用新配置內容 source ~/.bashrc 複製程式碼
-
安裝Rsut,如果要安裝nightly編譯工具鏈才加
--channel=nightly
根據朋友反饋,2018年11月21日用下面的中科大源安裝會報錯,官方源沒問題。// 以下命令二選一 // 中科大源 curl -sSf https://mirrors.ustc.edu.cn/rust-static/rustup.sh | sh # -s -- --channel=nightly // 官方源 curl https://sh.rustup.rs -sSf | sh 複製程式碼
-
cargo環境變數設定
當前版本的cargo裝好後,並不自動設定環境變數。在此進行手動配置,方便後面使用cargo安裝的效率工具。在此以macOS為例,mac上的cargo一般安裝在
~/.cargo/bin
下。export CARGO_BIN="[你的HOME目錄]/.cargo/bin" export PATH="$PATH:$CARGO_BIN" 複製程式碼
IDE配置
CLion與推薦外掛
- Rust外掛 提供程式碼提示、補全、跳轉等功能,比Rust Language Server(RLS)穩定、好用,外掛功能的更新速度快
-
Toml
方便編寫
Cargo.toml
檔案 - Active Intellij Tab Hightlighter 高亮當前開啟的Tab頁
- Dash 查文件方便
- Git Conflict 在原始檔中用顏色區分程式碼衝突,比Intellij系列產品原生做法更直觀
- Grep Console 過濾控制檯輸出,比預設功能更強
-
HighlightBracketPair
高亮顯示游標所在的區域,比如在某個
{}
,()
或[]
內。
不推薦Visual Studio Code的原因
RLS不穩定導致程式碼跳轉經常失效是最重要的原因,但是,VSCode的優勢是,在無法程式碼跳轉的情況下還能提供比CLion更強的程式碼提示,這讓我感到意外。
另外,VSCode配置起來麻煩,對Rust新手不友好。
提高開發維護效率的工具集
CI配置appveyor與travis
-
appveyor配置檔案
appveyor.yml
language: rust sudo: false matrix: include: - rust: stable script: - cargo test --all --locked - rustup component add rustfmt-preview - cargo fmt -- --write-mode=diff 複製程式碼
-
travis配置檔案
.travis.yml
language: rust rust: - stable - nightly branches: except: - staging.tmp before_install: # Do not run bors builds against the nightly compiler. # We want to find out about nightly bugs, so they're done in master, but we don't block on them. - if [[ $TRAVIS_RUST_VERSION == "nightly" && $TRAVIS_BRANCH == "staging" ]]; then exit; fi script: - cargo test - cargo build #--manifest-path your_project_path/Cargo.toml --features remote - cargo build #- (cd examples && make) #TODO 複製程式碼
cbindgen 給Rust程式碼自動生成C標頭檔案
給iOS/Android等編寫跨平臺C++/Rust專案最終還是以C介面方式讓外部使用,當提供較多介面時,手寫容易出錯、開發慢,此時用自動標頭檔案生成器是更合理的選擇,cbindgen可幫我們實現這一目標。
-
安裝
cargo install cbindgen
-
更新cbindgen
cargo install --force cbindgen
-
使用方式一:命令列執行
cbindgen crate/ -o crate/bindings.h 複製程式碼
-
使用方式一:作為專案的預處理使用方式寫成
build.rs
,對於複雜專案,推薦用此方案extern crate cbindgen; use std::env; fn main() { let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); cbindgen::Builder::new() .with_crate(crate_dir) .generate() .expect("Unable to generate bindings") .write_to_file("bindings.h"); } 複製程式碼
bindgen 給C標頭檔案生成Rust繫結程式碼
和cbindgen相反,bindgen可生成Rust呼叫C函式所需的FFI繫結程式碼,但是這個工具在遇到多重包含如#include "other_file.h"
時會出錯,詳細說明見官方文件。
-
安裝
cargo install bindgen
-
使用
bindgen input.h -o bindings.rs
-
--rust-target
指定Rust版本,如--rust-target 1.30
-
--rust-target nightly
使用nightly工具鏈
-
sccahe 多工作區共享編譯快取
目前Rust只支援工作區workspace內部多個專案間的編譯快取,不支援workspace之間的快取。對於多個workspace引用了部分相同版本的元件,這花費了多餘的編譯時間,沒意義。藉助第三方工具sccahe 可解決此問題。
cargo install sccache export RUSTC_WRAPPER=sccache
rustfmt 統一程式碼風格
為避免無意義的風格爭論,推薦使用Rust官方出品的統一程式碼風格元件rustfmt 。以下所有命令都需要在已配置好Rust環境的終端上執行。
rustup component add rustfmt-preview rustup update cargo fmt
自定義rustfmt程式碼風格
不建議自定義程式碼風格,最好和官方預設程式碼保持一致。定製風格規則參考rustfmt#configuring-rustfmt 。
用built輸出Rust專案構建資訊
built 是個編譯依賴(dev-dependencies)的開源專案,詳細用法見專案說明。
-
配置Cargo.toml
[dev-dependencies] built = "*" 複製程式碼
-
使用示例
pub mod built_info { include!(concat!(env!("OUT_DIR"), "/built.rs")); } info!("This is version {}{}, built for {} by {}.", built_info::PKG_VERSION, built_info::GIT_VERSION.map_or_else(|| "".to_owned(), |v| format!(" (git {})", v)), built_info::TARGET, built_info::RUSTC_VERSION); trace!("I was built with profile \"{}\", features \"{}\" on {} using {}", built_info::PROFILE, built_info::FEATURES_STR, built_info::BUILT_TIME_UTC, built_info::DEPENDENCIES_STR); 複製程式碼
輸出資訊:
This is version 0.1.0 (git 62eb1e2), built for x86_64-apple-darwin by rustc 1.16.0-nightly (bf6d7b665 2017-01-15). I was built with profile "debug", features "DEFAULT, ERR_PRINTLN" on Thu, 16 Feb 2017 19:00:08 GMT using android_glue 0.2.1, ansi_term 0.9.0, bitflags 0.3.3, bitflags 0.4.0, bitflags 0.6.0, bitflags 0.7.0, block 0.1.6, built 0.1.0, byteorder 0.5.3, bytes 0.3.0, cfg-if 0.1.0, cgl 0.1.5, cgmath 0.7.0, ... 複製程式碼
Rust開發iOS專案的效率工具
cargo-lipo
cargo lipo
一個命令可編譯出iOS目前支援的5個CPU架構靜態庫,且自動合併成一個多合一的universal靜態庫。
cargo install cargo-lipo cargo lipo
Rust專案開啟Bitcode編譯
RUSTFLAGS="-C llvm-args=\"-fembed-bitcode\"" cargo build 複製程式碼
You can tell cargo to pass any argument you wish to the Rust compiler by setting theRUSTFLAGS
environment variable. The Rustc compiler has a flag-C llvm-args=val
that you can use to pass additional arguments to llvm.