Foreword
https://www.rust-lang.org/tools/install
https://www.cnblogs.com/timefiles/p/17930394.html
Installing Rust on Windows

Setting Up Mirror Repositories
Win + X
, Press A
1 2
| [environment]::SetEnvironmentvariable("RUSTUP_DIST_SERVER", "https://mirrors.ustc.edu.cn/rust-static", "User") [environment]::SetEnvironmentvariable("RUSTUP_UPDATE_ROOT", "https://mirrors.ustc.edu.cn/rust-static/ rustup", "User")
|
Open rustup-init.exe,
Setting Up Cargo Mirror
1 2 3 4 5 6
| "[source.crates-io] registry = 'https://github.com/rust-lang/crates.io-index' replace-with = 'ustc' [source.ustc] registry = 'https://mirrors.ustc.edu.cn/crates.io-index/' "|Out-File -Encoding utf8 $home\.cargo\config
|
Installing Rust on Ubuntu
Setting Up Mirror Repositories
Open a terminal and run the following commands:
1 2
| export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
|
To make these settings permanent, add them to your shell configuration file:
1 2 3
| echo 'export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static' >> ~/.bashrc echo 'export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup' >> ~/.bashrc source ~/.bashrc
|
Installation Process
- First, install necessary dependencies:
1 2
| sudo apt update sudo apt install build-essential curl gcc make -y
|
- Install Rust using rustup (recommended method):
1 2
| curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh rustup toolchain install stable
|
When prompted, press Enter to proceed with the default installation
After installation completes, configure your current shell:
- Verify your installation:
Setting Up Cargo Mirror
- Create the configuration directory and file:
1 2
| mkdir -p ~/.cargo touch ~/.cargo/config
|
- Add the mirror configuration:
1 2 3 4 5
| echo '[source.crates-io] registry = "https://github.com/rust-lang/crates.io-index" replace-with = "ustc" [source.ustc] registry = "https://mirrors.ustc.edu.cn/crates.io-index"' > ~/.cargo/config
|
Build Program
1 2 3
| fn main() { println!("Hello World"); }
|
Method 1. Use rustc
Directly
Method 2. Use Cargo
(Recommend)
- Create a new project:
1 2
| cargo new hello_cargo cd hello_cargo
|
- Build the project:
- Run the project:
- Check your code without building:
- Build for production with optimizations:
Project Structure
When using Cargo, your project will have the following structure:
1 2 3 4 5 6
| hello_cargo/ ├── Cargo.toml ├── .git ├── .gitignore └── src/ └── main.rs
|