Installing Rust Tutorial

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,

1
rustc -V

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

  1. First, install necessary dependencies:
1
2
sudo apt update
sudo apt install build-essential curl gcc make -y
  1. Install Rust using rustup (recommended method):
1
2
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup toolchain install stable
  1. When prompted, press Enter to proceed with the default installation

  2. After installation completes, configure your current shell:

1
source $HOME/.cargo/env
  1. Verify your installation:
1
2
rustc -V
cargo -V

Setting Up Cargo Mirror

  1. Create the configuration directory and file:
1
2
mkdir -p ~/.cargo
touch ~/.cargo/config
  1. 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

1
rustc main.rs

Method 2. Use Cargo (Recommend)

  1. Create a new project:
1
2
cargo new hello_cargo
cd hello_cargo
  1. Build the project:
1
cargo build
  1. Run the project:
1
cargo run
  1. Check your code without building:
1
cargo check
  1. Build for production with optimizations:
1
cargo build --release

Project Structure

When using Cargo, your project will have the following structure:

1
2
3
4
5
6
hello_cargo/
├── Cargo.toml # Project configuration file
├── .git # Git repository
├── .gitignore # Git ignore file
└── src/
└── main.rs # Main source code file

Installing Rust Tutorial
https://www.hardyhu.cn/2025/04/13/Installing-Rust-Tutorial/
Author
John Doe
Posted on
April 13, 2025
Licensed under