Rust安装、VS Code 搭建 Rust 开发环境

本文介绍如何使用rustup工具链安装Rust,并在Windows和Linux环境下配置好Rust开发环境。此外,还提供了在VSCode中搭建Rust开发环境的方法,包括安装必要的插件及创建和运行简单的HelloWorld项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、安装 Rust

官网安装教程:https://www.rust-lang.org/zh-CN/tools/install

用 rustup 管理工具链

Rust 由工具 rustup 安装和管理。Rust 有着以 6 星期为周期的 快速版本迭代机制,支持 大量平台,因而不同时期存在大量不同的 Rust 构建版本。 rustup 用于管理不同平台下的 Rust 构建版本并使其互相兼容, 支持安装由 Beta 和 Nightly 频道发布的版本,并支持其他用于交叉编译的编译版本。

如果您曾经安装过 rustup,可以执行 rustup update 来升级 Rust。

对于大多数开发者而言,推荐用 rustup 来安装 Rust 。

Cargo:Rust 的构建工具和包管理器

Cargo:Rust 的构建工具和包管理器
您在安装 Rustup 时,也会安装 Rust 构建工具和包管理器的最新稳定版,即 Cargo。Cargo 可以做很多事情:

  • cargo build 可以构建项目
  • cargo run 可以运行项目
  • cargo test 可以测试项目
  • cargo doc 可以为项目构建文档
  • cargo publish 可以将库发布到 crates.io。
    要检查您是否安装了 Rust 和 Cargo,可以在终端中运行:
cargo --version

cargo手册: https://doc.rust-lang.org/cargo/index.html

Windows中的Rust安装

在Windows上, 打开链接 https://www.rust-lang.org/zh-CN/tools/install 然后按照说明安装Rust。按照所有说明进行操作之后, Rust将被安装并显示屏幕:

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  C:\Users\intha\.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  C:\Users\intha\.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  C:\Users\intha\.cargo\bin

This path will then be added to your PATH environment variable by
modifying the HKEY_CURRENT_USER/Environment/PATH registry key.

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-pc-windows-msvc
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

info: profile set to 'default'
info: default host triple is x86_64-pc-windows-msvc
info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'
info: latest update on 2021-02-11, rust version 1.50.0 (cb75ad5db 2021-02-10)
info: downloading component 'cargo'
  3.6 MiB /   3.6 MiB (100 %) 907.9 KiB/s in  4s ETA:  0s
info: downloading component 'clippy'
  1.6 MiB /   1.6 MiB (100 %) 730.8 KiB/s in  2s ETA:  0s
info: downloading component 'rust-docs'
 14.6 MiB /  14.6 MiB (100 %) 985.6 KiB/s in 16s ETA:  0s
info: downloading component 'rust-std'
 20.9 MiB /  20.9 MiB (100 %) 992.0 KiB/s in 23s ETA:  0s
info: downloading component 'rustc'
 52.1 MiB /  52.1 MiB (100 %) 1020.8 KiB/s in  1m  3s ETA:  0s
info: downloading component 'rustfmt'
  2.0 MiB /   2.0 MiB (100 %) 964.9 KiB/s in  2s ETA:  0s
info: installing component 'cargo'
info: using up to 500.0 MiB of RAM to unpack components
info: installing component 'clippy'
info: installing component 'rust-docs'
 14.6 MiB /  14.6 MiB (100 %)   1.2 MiB/s in 15s ETA:  0s
info: installing component 'rust-std'
 20.9 MiB /  20.9 MiB (100 %)   8.0 MiB/s in  1m 34s ETA:  0s
info: installing component 'rustc'
 52.1 MiB /  52.1 MiB (100 %)   8.3 MiB/s in  6s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-pc-windows-msvc'

  stable-x86_64-pc-windows-msvc installed - rustc 1.50.0 (cb75ad5db 2021-02-10)


Rust is installed now. Great!

To get started you need Cargo's bin directory (%USERPROFILE%\.cargo\bin) in
your PATH environment variable. Future applications will automatically
have the correct environment, but you may need to restart your current shell.

Press the Enter key to continue.

安装后, Rust的PATH变量会自动添加到你的系统PATH中。

打开命令提示符, 然后运行以下命令:

$ rustc --version

linux 中Rust安装

在Linux上安装Rust一般会很简单。按照官网(Install Rust)的说明,我们应该能够通过执行下面的命令来安装它。

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

但是,在国内,有时候,安装过程会很慢甚至不稳定。 因此,我可能会需要配置toolchain和rustup更新服务器的镜像。 (其中 RUSTUP_DIST_SERVER默认指向 https://static.rust-lang.org, RUSTUP_UPDATE_ROOT默认指向https://static.rust-lang.org/rustup)

# 用于更新 toolchain
export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
# 用于更新 rustup
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

在国内我们还有可能需要配置http://crates.io的镜像。我们可以通过修改配置文件~/.cargo/config(如果不存在则需要手动创建)来配置镜像。

# ~/.cargo/config
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

安装

root@aaa2:~# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /root/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  /root/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /root/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /root/.profile
  /root/.bashrc

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2021-12-02, rust version 1.57.0 (f1edd0429 2021-11-29)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 17.9 MiB /  17.9 MiB (100 %)   3.1 MiB/s in  4s ETA:  0s
info: installing component 'rust-std'
 24.9 MiB /  24.9 MiB (100 %)   8.1 MiB/s in  2s ETA:  0s
info: installing component 'rustc'
 53.9 MiB /  53.9 MiB (100 %)   9.5 MiB/s in  5s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.57.0 (f1edd0429 2021-11-29)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source $HOME/.cargo/env
root@aaa2:~# source $HOME/.cargo/env
root@aaa2:~# rustc -V
rustc 1.57.0 (f1edd0429 2021-11-29)

最后根据上面提醒,执行 source $HOME/.cargo/env

rust为我们提供一个脚步~/.cargo/env帮助我们把Cargon的bin目录写入环境变量PATH中 .

Rustup的根目录: ~/.rustup, 可以通过环境变量RUSTUP_HOME来修改
Cargo的根目录:~/.cargo, 可以通过环境变量CARGO_HOME来修改
Cargo的bin目录:~/.cargo/bin, 其中包含cargo, rustup等其他命令。该路径需要添加到环境变量PATH中。

二、VS Code 搭建 Rust 开发环境

VS Code 搭建 Rust 开发环境
参考URL: https://baijiahao.baidu.com/s?id=1685431743373044619

打开 vs code 找到插件工具栏,单击打开并在搜索栏输入 “rust” 搜索 Rust 插件
在这里插入图片描述

Hello World

  1. 新建一个文件夹,然后用VS Code打开
    在这里插入图片描述2. 创建新项目
    我们将在新的 Rust 开发环境中编写一个小应用。首先用 Cargo 创建一个新项目。在您的终端中执行:
cargo new hello-rust

这会生成一个名为 hello-rust 的新目录,其中包含以下文件:

hello-rust
|- Cargo.toml
|- src
  |- main.rs
  • Cargo.toml 为 Rust 的清单文件。其中包含了项目的元数据和依赖库。
  • src/main.rs 为编写应用代码的地方。

cargo new 会生成一个新的“Hello, world!”项目!我们可以进入新创建的目录中,执行下面的命令来运行此程序:

cargo run

您应该会在终端中看到如下内容:

PS E:\code\rust-test\hello-rust> cargo run
   Compiling hello-rust v0.1.0 (E:\code\rust-test\hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 3.29s
     Running `target\debug\hello-rust.exe`
Hello, world!
PS E:\code\rust-test\hello-rust> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西京刀客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值