Rust权威指南阅读笔记(一)Rust简介与安装

发布时间 2024-01-05 21:05:20作者: REWIND98

参考:

Windows10配置Rust开发环境 | jonssonyan' Website
win10安装Rust+VS Code配置Rust环境 - 鱼又悲 - 博客园 (cnblogs.com)

1.1 Rust简介

1.2 安装Rust

https://www.rust-lang.org/zh-CN/tools/install
下载 RUSTUP-INIT.EXE并运行
如遇到:component download failed for clippy-x86_64-pc-windows-msvc:...

在powershell中打开RUSTUP-INIT.EXE所在目录

$ENV:RUSTUP_DIST_SERVER='https://mirrors.ustc.edu.cn/rust-static' 
$ENV:RUSTUP_UPDATE_ROOT='https://mirrors.ustc.edu.cn/rust-static/rustup'
.\rustup-init.exe

1.3 Hello World

fn main() {
    println!("Hello World!");
}
  • 编译:rustc main.rs
  • 运行:./hello_world.exe
  • Rust文件后缀名:rs
  • 文件命名规范: hello_world.rs

解析

  • 定义函数:fn main() { }
    没有参数,没有返回值
  • main函数是每个Rust可执行程序最先运行的代码
  • 打印文本:println!("Hello World!");
    Rust的缩进是4个空格而不是tab
    println! 是一个Rust macro(宏),以!结尾的都是宏,函数没有!
    代码以;结尾

编译和运行是单独的两步

  • 运行Rust程序之前必须先编译,命令为:rustc 源文件名
  • 编译成功后,会生成一个二进制文件
    在Windows上还会生成一个.pdb文件,里面包含调试信息
  • Rust是ahead-of-time 编译的语言
    可以先编译程序,然后把可执行文件交给别人运行(无需安装Rust)
  • rustc只适合简单的Rust程序,如果文件比较多,程序比较大用rustc就不合适了,此时需要使用cargo

1.4 Cargo

cargo是什么

Cargo是Rust的构建系统和包管理工具,一构建代码、下载依赖的库、构建这些库..

使用cargo创建项目

cargo new hello_cargo
该命令会在当前目录下创建一个名为hello_cargo的文件夹,目录结构如下

hello_cargo
-src
    main.rs
.gitignore
Cargo.toml

直接初始化了一个新的Git仓库,
可以使用其他的VCS或者不用VCS:cargo new 的时候用 --vcs

Cargo.toml文件:

[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

[dependencies]
  • TOML(Tom's Obvious, Minimal Language)格式,是 Cargo 的配置格式
  • [pacakge],是一个区域标题,表示下方内容是用来配置包(package)的
    • name,项目名
    • version,项目版本
    • edition,使用的 Rust版本3
  • [dependencies],另一个区域的开始, 它会列出项目的依赖项
  • 在Rust中,代码的包(第三方库)称作crate

src/main.rs

源代码都应该放在src目录下
如果创建项目时没有使用cargo,也可以把项目转化为使用cargo:
-把源代码文件移动到src下
-创建Cargo.toml 并填写相应的配置

cargo build 创建可执行文件

第一次运行 cargo build 会在顶层目录生成 cargo.lock 文件

  • 该文件负责追踪项目依赖的精确版本
  • 不需要手动修改该文件

cargo run 构建和运行cargo项目

编译代码+执行结果

cargo check 检查代码

查代码,确保能通过编译,但是不产生任何可执行文件
cargo check 要比 cargo build 快得多

cargo build --release 为发布构建

编译时会进行优化,代码会运行的更快,但编译时间更长
会在target/release而不是target/debug生成可执行文件

常用命令

rustup相关命令

# 显示当前安装的工具链信息
rustup show
# 检查安装更新
rustup update
# 卸载
rustup self uninstall
# 设置当前默认工具链
rustup default stable-x86_64-pc-windows-gnu
# 查看帮助
rustup -h
# -------------------------->配置工具链
# 查看工具链
rustup toolchain list
# 安装工具链
rustup toolchain install stable-x86_64-pc-windows-gnu
# 卸载工具链
rustup toolchain uninstall stable-x86_64-pc-windows-gnu
# 设置自定义工具链
rustup toolchain link <toolchain-name> "<toolchain-path>"
# -------------------------->配置一个目录以及其子目录的默认工具链
# 查看已设置的默认工具链
rustup override list
# 设置该目录以及其子目录的默认工具链
rustup override set <toolchain> --path <path>
# 取消目录以及其子目录的默认工具链
rustup override unset --path <path>
# -------------------------->配置工具链的可用目标
# 查看目标列表
rustup target list
# 安装目标
rustup target add <target>
# 卸载目标
rustup target remove <target>
# 为特定工具链安装目标
rustup target add --toolchain <toolchain> <target>
# -------------------------->配置 rustup 安装的组件
# 查看可用组件
rustup component list
# 安装组件
rustup component add <component>
# 卸载组件
rustup component remove <component>

cargo 相关

# 查看cargo版本
cargo --version
# 新建项目
cargo new <project_name>
# 构建项目
cargo build
# 运行项目
cargo run
# 检查项目
cargo check
# 安装Rust二进制文件
cargo install
# 卸载Rust二进制文件
cargo uninstall
# 查看帮助
cargo -h