Rust认识安装
1、Rust认识
Rust官网
Rust 官网:www.rust-lang.org/
JS
https://www.rust-lang.org/
Rust中文官网:www.rust-lang.org/zh-CN/
JS
https://www.rust-lang.org/zh-CN/
🍎 配置电脑环境变量的地方
JS
此电脑=>属性=>高级系统设置=>环境变量=>系统变量=>Path
👉Rust优点
对Rust了解越深,越发感觉他的精美,良好的后端API服务的优点可以说它都包含了,总结为以下几点:
● 健壮性:程序缺陷和bug少,能够在编译时发现大多数错误。
● 高性能:API 服务具备低延迟和高吞吐量的能力,适应高并发场景。
● 低资源消耗:在相同吞吐量和响应速度下,占用的系统资源(CPU、内存)越低
● 开发效率:开发效率应高于大部分语言/框架,提升业务效率
● 可扩展性:具备良好的扩展性,可以适应业务需求和用户需求
2、RustWindows安装
我本地系统是Window10和Window11 的系统,都尝试进行了安装
前置条件:安装C++环境
👉 下载
rust底层是依赖C环境的,所以先安装C/C++编译环境
javascript
https://github.com/niXman/mingw-builds-binaries/releases/download/13.2.0-rt_v11-rev0/x86_64-13.2.0-release-posix-seh-ucrt-rt_v11-rev0.7z
👉 配置环境变量
下载解压到任意盘根目录,然后把文件夹的bin目录路径配置到系统环境变量path
javascript
// 我本地环境地址如下
E:\anzhuang\c\mingw64\bin
D:\anzhuang\c\mingw64\bin
环境变量地址
此电脑=>属性=>高级系统设置=>环境变量=>系统变量=>Path
👉检测是否成功
javascript
gcc -v
// 如果是安装成功以后会出现以下提示 '
xxx
gcc version 13.2.0 (x86_64-posix-seh-rev0, Built by MinGW-Builds project)
安装Rust
👉 下载
地址rustup-init.exe(link.zhihu.com/?target=htt...)
javascript
Window:下载并运行
rustup-init.exe(https://link.zhihu.com/?target=https%3A//static.rust-lang.org/rustup/dist/i686-pc-windows-gnu/rustup-init.exe)
下载过程会给我们提示以下信息
javascript
Rust Visual C++ prerequisites
Rust requires a linker and Windows API libraries but they don't seem to be
available.
These components can be acquired through a Visual Studio installer.
1) Quick install via the Visual Studio Community installer
(free for individuals, academic uses, and open source).
2) Manually install the prerequisites
(for enterprise and advanced users).
3) Don't install the prerequisites
(if you're targeting the GNU ABI).
简单解释一下上面的信息
javascript
Rust Visual C++先决条件
Rust需要一个链接器和Windows API库,但它们似乎不是
可用。
这些组件可以通过Visual Studio安装程序获取。
1) 通过Visual Studio社区安装程序快速安装
(个人、学术用途和开源免费)。
2) 手动安装必备组件
(适用于企业和高级用户)。
3) 不安装必备组件
(如果你的目标是GNU ABI)。
如果我们直接是安装过C/C++的环境的,我们直接安装Rust的时候选择3即可(也建议这样子做)
👉 检测是否成功
javascript
rustc -V
rustup -V
cargo -V
rustup show
👉 win11系统注意
提示选择
win11 选择3 以后会出现下面信息,大致意思还是让选择版本 (选择 1 标准安装)
javascript
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\admin\.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
C:\Users\admin\.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\admin\.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 standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
提示
javascript
info: profile set to 'default'
info: default host triple is x86_64-pc-windows-msvc
warning: Updating existing toolchain, profile choice will be ignored
info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'
info: default toolchain set to 'stable-x86_64-pc-windows-msvc'
stable-x86_64-pc-windows-msvc unchanged - rustc 1.85.0 (4d91de4e4 2025-02-17)
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload its PATH environment variable to include
Cargo's bin directory (%USERPROFILE%\.cargo\bin).
Press the Enter key to continue.
就是安装成功了,重新启动cmd命令行即可
卸载Rust
👉命令卸载
如果我们是通过 rustup(Rust 的官方安装工具)安装 Rust 的,可以使用 rustup 自带的卸载命令来移除 Rust
javascript
rustup self uninstall
//检测是否卸载成功
rustc --version
//查看环境变量是否卸载干净
在 Windows 上,通过 "系统属性" > "高级系统设置" > "环境变量" 来删除 Rust 的路径
3、Mac安装
方式一
javascript
打开这个网站
https://rustup.rs/
复制一下安装命令
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
方式二
安装方式是用brew来安装,几乎都能成功
javascript
brew install rust
验证是否安装成功
javascript
rustc --version
查看文档
javascript
rustup doc
工具安装
使用 VSCode,安装 Rust 插件即可

4、Rust-Hello World
1、创建你的第一个 Rust 项目
Rust 的文件以 .rs 结尾,我们可以通过 cargo
来创建和管理 Rust 项目。cargo
是 Rust 的构建系统和包管理工具。
打开终端,运行以下命令来创建一个新的 Rust 项目:
rust
cargo new rust-learn
cd rust-learn
查看项目文件结构:
bash
hello_world
├── Cargo.toml // 配置文件
└── src
└── main.rs // 默认的源代码文件
Cargo.toml 是项目的配置文件,管理依赖关系。
src/main.rs 是你将编写 Rust 代码的地方
在 src/main.rs 文件中,你会看到 Rust 自动生成的代码:
fn main() {
println!("Hello World");
}
代码释义:
● fn 用来定义一个函数,等同于 Javascript 中的 function
● main 函数,Rust 可执行文件的入口
● println!,是一个 Rust macro(宏) 用来输出字符串
2、编译运行程序
bash
cargo run
javascript
报错
sudo xcodebuild -license
错误是因为在 macOS 上,Rust 需要使用 Xcode 提供的工具链来进行编译和链接,而你的系统尚未同意 Xcode 的许可证协议。为了解决这个问题,你需要同意 Xcode 的许可证协议。按照以下步骤操作:
同意 Xcode 的许可证
打开终端并运行以下命令:
bash
sudo xcodebuild -license
xcode安装部分:
/Applications/Xcode.app/Contents/Developer
设置
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
再次
cargo run 已经成功解决
3、运行代码
bash
# windows
.\main.exe
# Linux/mac
./main
Cargo 命令
cargo build: 编译程序,生成可执行文件。默认会在 target/debug 目录中生成可执行文件。
cargo run: 编译并运行程序,相当于执行 cargo build 后立即运行可执行文件。
cargo clean: 清理编译过程中生成的文件。
cargo test: 运行项目中的测试。
cargo doc: 生成项目的文档。