On Windows install the MSVC build tools first, then rustup, or linking fails
winget install Microsoft.VisualStudio.2022.BuildTools
winget install Rustlang.RustupSelect the Desktop development with C++ workload in Build Tools
Set up Rust: install rustup with the MSVC dependency, manage toolchains and components, switch cargo mirrors, create projects, cross-compile and fix common errors.
On Windows install the MSVC build tools first, then rustup, or linking fails
winget install Microsoft.VisualStudio.2022.BuildTools
winget install Rustlang.RustupSelect the Desktop development with C++ workload in Build Tools
Install rustup with the official script on macOS/Linux and load it into the current shell
# macOS / Linux
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"Show the default toolchain plus the compiler and package manager versions
rustup show
rustc --version
cargo --versionInstall nightly, keep stable as global default, and override it for one directory
rustup toolchain install nightly
rustup default stable
rustup override set nightlyAdd formatting and lint components, then update all toolchains
rustup component add rustfmt clippy
rustup updateWrite this into ~/.cargo/config.toml (%USERPROFILE%\.cargo\config.toml on Windows) to use a China mirror
[source.crates-io]
replace-with = 'rsproxy-sparse'
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"Configure an extra registry so private crates can target it explicitly
[registries.rsproxy]
index = "sparse+https://rsproxy.cn/index/"Show verbose logs to confirm which source the dependencies came from
cargo build -vRoute cargo through a proxy with environment variables; use set on Windows
export https_proxy=http://127.0.0.1:7890
export http_proxy=http://127.0.0.1:7890Verify the cargo bin directory is on PATH
echo %USERPROFILE%\.cargo\bin
# macOS / Linux
echo $HOME/.cargo/binShow the real compiler paths for the active toolchain when multiple versions conflict
rustup which rustc
rustup which cargoSwitch the rustup distribution mirror when toolchain downloads are slow
export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
export RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustupOptimize the build for the current CPU; do not enable this for binaries shipped to other machines
export RUSTFLAGS="-C target-cpu=native"Create a binary crate and run it; the first compile is slow
cargo new hello --bin
cd hello
cargo runProduce an optimized binary under target/release
cargo build --release
./target/release/helloAdd dependencies without editing Cargo.toml by hand
cargo add serde --features derive
cargo add tokio --features fullIf the add subcommand is missing, run cargo install cargo-edit first
Formatting, linting and tests — wire all three into CI
cargo fmt
cargo clippy -- -D warnings
cargo testCross-compile a statically linked Linux binary that runs in a bare container
rustup target add x86_64-unknown-linux-musl
cargo build --release --target x86_64-unknown-linux-muslInstall the rust-analyzer language server and the CodeLLDB debugger
code --install-extension rust-lang.rust-analyzer
code --install-extension vadimcn.vscode-lldbInstall the standard library sources for go-to-definition and implementation views
rustup component add rust-srcRebuild and rerun on file changes instead of running commands manually
cargo install cargo-watch
cargo watch -x runWhen the linker is missing, the MSVC build tools or the C++ workload is absent
# Windows
where link
where clReset the default toolchain and upgrade when the compiler misbehaves
rustup default stable
rustup update stableRemove the target directory and rebuild when artifacts are corrupt
cargo cleanFind crates present in multiple versions to trace duplicate dependencies
cargo tree -dPin a single dependency to a known-good version after a breaking upstream release
cargo update -p serde --precise 1.0.203Fully remove rustup and every toolchain, including tools under ~/.cargo
rustup self uninstallThis website uses cookies to ensure you get the best experience on our website. Privacy Policy.