在Debian系統中,Rust依賴管理主要通過Cargo(Rust官方包管理器)實現,核心操作如下:
安裝工具鏈
rustup
安裝Rust和Cargo(支持多版本管理):curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env # 重新加載環境變量
sudo apt update && sudo apt install rustc cargo
項目依賴管理
Cargo.toml
文件中添加依賴項(如[dependencies] serde = "1.0"
),然后運行cargo build
自動下載依賴。cargo update
更新所有依賴到最新版本(或指定包名更新)。Cargo.toml
中移除依賴項后,運行cargo build
清理。構建與驗證
cargo build
cargo run
cargo tree
(需安裝cargo-tree
工具)。創建Debian包(可選)
cargo-deb
工具:cargo install cargo-deb
cargo deb
,輸出至target/debian/
目錄。說明:Cargo通過Cargo.toml
聲明依賴,Cargo.lock
鎖定版本,確保構建一致性。全局依賴可通過cargo install
安裝為可執行文件。