Skip to content

cargo

About 189 wordsLess than 1 minute

2022-06-21

# Display version information to confirm that Cargo is installed
cargo version
# Create a new project binary program

cargo new --bin
# Create a new project library
cargo new --lib

# Run unit tests in a project
cargo test
# Quickly compile a project without generating binaries to check for errors
cargo check
# Automatically format code
cargo fmt
# Compile a project
cargo build
# Compile and run a project in one step
cargo run
# Linter check for errors
cargo clippy --all-targets -- --D warnings
# Check code coverage
cargo tarpaulin --ignore-tests

Install/Upgrade

Applicable to Linux and MacOS

curl -sSf https://static.rust-lang.org/rustup.sh | sh

In Windows, download rustup-init.exe and run it.

Switch source

Configuration file: ~/.cargo/config

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'tuna' # 👈 If you need to submit package annotations to configure the source

[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# registry = "git://mirrors.ustc.edu.cn/crates.io-index"

Switching source requires deleting the cache directory

rm -rf ~/.cargo/.package-cache # ⚠️ Delete the contents of the cache directory