Rust Setup Guide - CodeSwordsman

Set up Rust: install rustup with the MSVC dependency, manage toolchains and components, switch cargo mirrors, create projects, cross-compile and fix common errors.

Rust 1.78+ (stable)Updated 2026-10-2927 commands

Install & Version Management

On Windows install the MSVC build tools first, then rustup, or linking fails

winget install Microsoft.VisualStudio.2022.BuildTools
winget install Rustlang.Rustup

Select 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 --version

Install nightly, keep stable as global default, and override it for one directory

rustup toolchain install nightly
rustup default stable
rustup override set nightly

Add formatting and lint components, then update all toolchains

rustup component add rustfmt clippy
rustup update

Mirrors & Proxy

Write 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 -v

Route 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:7890

Environment Variables

Verify the cargo bin directory is on PATH

echo %USERPROFILE%\.cargo\bin
# macOS / Linux
echo $HOME/.cargo/bin

Show the real compiler paths for the active toolchain when multiple versions conflict

rustup which rustc
rustup which cargo

Switch 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/rustup

Optimize the build for the current CPU; do not enable this for binaries shipped to other machines

export RUSTFLAGS="-C target-cpu=native"

Create & Verify

Create a binary crate and run it; the first compile is slow

cargo new hello --bin
cd hello
cargo run

Produce an optimized binary under target/release

cargo build --release
./target/release/hello

Add dependencies without editing Cargo.toml by hand

cargo add serde --features derive
cargo add tokio --features full

If 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 test

Cross-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-musl

IDE & Plugins

Install the rust-analyzer language server and the CodeLLDB debugger

code --install-extension rust-lang.rust-analyzer
code --install-extension vadimcn.vscode-lldb

Install the standard library sources for go-to-definition and implementation views

rustup component add rust-src

Rebuild and rerun on file changes instead of running commands manually

cargo install cargo-watch
cargo watch -x run

Troubleshooting

When the linker is missing, the MSVC build tools or the C++ workload is absent

# Windows
where link
where cl

Reset the default toolchain and upgrade when the compiler misbehaves

rustup default stable
rustup update stable

Remove the target directory and rebuild when artifacts are corrupt

cargo clean

Find crates present in multiple versions to trace duplicate dependencies

cargo tree -d

Pin a single dependency to a known-good version after a breaking upstream release

cargo update -p serde --precise 1.0.203
Risky

Fully remove rustup and every toolchain, including tools under ~/.cargo

rustup self uninstall

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.