这是【零基础 Rust 入门】系列的第 1 章。本系列由前端技术专家零弌分享。想要探索前端技术的无限可能,就请关注我们吧!🤗
- 🧑💻 我们是谁:支付宝体验技术部-基础服务团队
- 📖 我们的专栏:前沿视点
安装 rustup
注意自备梯子。
# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
确认安装效果,查看 rust 版本。
rustc --version
# rustc 1.75.0 (82e1608df 2023-12-21)
安装&切换工具链
Rust 提供有 stable 和 nightly 版本工具链,大量新特性都通过 nightly,一般推荐使用这个。
安装 nightly 版本。
rustup toolchain install nightly
默认使用 nightly 版本。
rustup default nightly
查看工具链版本。
rustup show
# Default host: x86_64-apple-darwin
# rustup home: /Users/killa/.rustup
#
# installed toolchains
# --------------------
#
# stable-x86_64-apple-darwin
# nightly-2022-12-12-x86_64-apple-darwin
# nightly-x86_64-apple-darwin (default)
# 1.49.0-x86_64-apple-darwin
# 1.52.1-x86_64-apple-darwin
# 1.61.0-x86_64-apple-darwin
# 1.68.2-x86_64-apple-darwin
#
# active toolchain
# ----------------
#
# nightly-x86_64-apple-darwin (default)
# rustc 1.77.0-nightly (635124704 2024-01-27)
更新工具链。
rustup update
可通过 rustup --help
查看更多命令。
初始化项目
使用 cargo new
进行项目初始化。
# 初始化 executable 项目
cargo new hello-rust
# 初始化 lib 项目
cargo new hello-rust-lib --lib
目录介绍
.
├── Cargo.toml
└── src
└── main.rs
Cargo.toml
项目描述文件,可设置依赖、编译参数等等。
[package]
name = "hello-rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
main.rs
Hello, world 源码。
fn main() {
println!("Hello, world!");
}
安装依赖
通过 cargo add
添加依赖。
cargo add log env_logger
更新后的 Cargo.toml,并且新增了 Cargo.lock 文件。
[package]
name = "hello-rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
env_logger = "0.11.1"
log = "0.4.20"
查看文档
此时我们大概率仍不知道 log 以及 env_logger 是如何使用的,需要查看文档。
cargo doc --open
IDE 使用
强烈推荐使用 CLion 以及 Rust 插件。
配置 Run/Debug 方式。
开始奇幻的 Rust 之旅!
运行项目
run
下面是一个简单 rust 代码例子,看不懂没关系,本文主要介绍工具链。
use log::{debug, error, log_enabled, info, Level};
use env_logger;
#[derive(Debug)]
enum AppleColor {
Red,
Green,
}
#[derive(Debug)]
struct Apple {
pub color: AppleColor,
}
impl Apple {
fn new(color: AppleColor) -> Self {
Apple {
color,
}
}
}
fn main() {
env_logger::init();
let green_apple = Apple::new(AppleColor::Green);
let red_apple = Apple::new(AppleColor::Red);
info!("apple color {:?}", green_apple.color);
info!("apple {:?} {:?}", green_apple, red_apple);
}
插播 100 种 rust 中格式化对象的方式,语法详见手册
format!("Hello"); // => "Hello"
format!("Hello, {}!", "world"); // => "Hello, world!"
format!("The number is {}", 1); // => "The number is 1"
format!("{:?}", (3, 4)); // => "(3, 4)"
format!("{value}", value=4); // => "4"
let people = "Rustaceans";
format!("Hello {people}!"); // => "Hello Rustaceans!"
format!("{} {}", 1, 2); // => "1 2"
format!("{:04}", 42); // => "0042" with leading zeros
format!("{:#?}", (100, 200)); // => "(
// 100,
// 200,
// )"
运行我们的代码。
cargo run
一堆 warning,以及没有输出。
再次查看 env_logger 的文档,原来是需要增加环境变量。
运行成功。
clippy
clippy 手册 clippy 即 rust 中的 lint 工具。
安装。
rustup component add clippy
检查语法。
cargo clippy -- -D warnings
自动修复。
cargo clippy --fix
format
安装
rustup component add rustfmt
格式化
cargo fmt