Linux下Rust包管理操作指南
一 环境准备与安装
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shsource "$HOME/.cargo/env"cargo --versionrustup self uninstall二 项目与依赖管理
cargo new hello_world --bincargo new my_lib --libserde = { version = "1.0", features = ["derive"] }rand = "0.8"cargo update(会更新 Cargo.lock 中记录的精确版本)cargo buildcargo runcargo checkcargo testcargo doccargo treecargo clean三 常用工作流与命令速查
cargo initcargo fmtcargo clippy --all-targets -- --D warningscargo tarpaulin --ignore-testscargo login [token]cargo packagecargo publishcargo uninstall ripgrep(仅卸载二进制名称)四 国内镜像与私有源配置
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'tuna'
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
rm -rf ~/.cargo/.package-cache--registry 指定。五 常见问题与进阶
rustup target add x86_64-unknown-linux-gnu(示例)cargo build --target x86_64-unknown-linux-gnucargo build --release --target x86_64-unknown-linux-gnucargo cleanbuild.target-dir 指定(例如 CARGO_TARGET_DIR=target-ci cargo build)。