기술나눔

Rust开发环境搭建

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Rust开发环境搭建

环境

rust: 1.79.0(2024-06-13)
  • 1

1. Rustup下载器在线安装

windows:

https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe

unix:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • 1

2. Rust独立安装包离线安装

windows:

https://static.rust-lang.org/dist/rust-1.79.0-x86_64-pc-windows-msvc.msi

linux:

https://static.rust-lang.org/dist/rust-1.79.0-x86_64-unknown-linux-gnu.tar.xz

macos:

https://static.rust-lang.org/dist/rust-1.79.0-x86_64-apple-darwin.pkg

3. Rust命令行

  • 查看rust版本
$ rustc -V
rustc 1.79.0
  • 1
  • 2
  • 新建rust项目
cargo new hello_world
  • 1
  • cargo编译rust项目
$ cargo build
  • 1
  • rustc编译rust项目
$ rustc src/main.rs
  • 1
  • 运行rust程序
$ ./target/debug/hello_world
Hello, world!
  • 1
  • 2

4. IDE集成开发环境

4.1 VSCode

Rust with Visual Studio Code

4.2 vs2022

[User Manual (rust-analyzer.github.io)

5. 使用镜像加速cargo

$CARGO_HOME/config中加入如下内容

[source.crates-io]
replace-with = 'mirror'

[source.mirror]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
  • 1
  • 2
  • 3
  • 4
  • 5

注:$CARGO_HOME:在 Windows 系统默认为:%USERPROFILE%.cargo,在类 Unix 系统默认为:$HOME/.cargo


Reference:

  1. Other Installation Methods - Rust Forge (rust-lang.org)