2022-05-04 21:38:00 +08:00
|
|
|
|
[#]: subject: "Learn Rust in 2022"
|
|
|
|
|
[#]: via: "https://opensource.com/article/22/1/rust-cheat-sheet"
|
|
|
|
|
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
|
|
|
|
|
[#]: collector: "lujun9972"
|
|
|
|
|
[#]: translator: "hanszhao80"
|
2022-05-08 16:18:18 +08:00
|
|
|
|
[#]: reviewer: "wxy"
|
|
|
|
|
[#]: publisher: "wxy"
|
|
|
|
|
[#]: url: "https://linux.cn/article-14563-1.html"
|
2022-05-04 21:38:00 +08:00
|
|
|
|
|
2022-05-08 16:18:18 +08:00
|
|
|
|
2022 Rust 入门指南
|
2022-05-04 21:38:00 +08:00
|
|
|
|
======
|
|
|
|
|
|
2022-05-08 16:18:18 +08:00
|
|
|
|
> 如果你打算在今年探索 Rust,请下载我们的免费 Rust 速查表,以供快速参考基础知识。
|
2022-05-04 21:38:00 +08:00
|
|
|
|
|
2022-05-08 16:18:18 +08:00
|
|
|
|
![](https://img.linux.net.cn/data/attachment/album/202205/08/161625lvo8v82ell9l3xmm.jpg)
|
|
|
|
|
|
|
|
|
|
Rust 是一门相对较新的编程语言,受到各个企业的 [程序员的欢迎][2]。尽管如此,它仍是一门建立在之前所有事物之上的语言。毕竟,Rust 不是一天做出来的,所以即便 Rust 中的一些概念看起来与你从 Python、Java、C++ 等编程语言学到的东西大不相同,但它们都是基于同一个基础,那就是你一直与之交互(无论你是否知道)的 CPU 和 NUMA(<ruby>非统一内存访问<rt>Non Uniform Memory Access</rt></ruby>)架构,因此 Rust 中的一些新功能让人感觉有些熟悉。
|
|
|
|
|
|
|
|
|
|
现在,我的职业不是程序员。我没耐心但我又有点儿强迫症。当我需要完成某件事时,如果一门语言不能帮助我相对较快地获得想要的结果,那么我很少会受到鼓舞而使用它。Rust 试图平衡两个矛盾:现代计算机对安全和结构化代码的需求,和现代程序员对编码工作事半功倍的渴望。
|
2022-05-04 21:38:00 +08:00
|
|
|
|
|
|
|
|
|
### 安装 Rust
|
|
|
|
|
|
|
|
|
|
[rust-lang.org][3] 网站有丰富的的文档指导如何安装 Rust,但通常,它就像下载 `sh.rustup.rs` 脚本并运行它一样简单。
|
|
|
|
|
|
|
|
|
|
```
|
2022-05-08 16:18:18 +08:00
|
|
|
|
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs
|
2022-05-04 21:38:00 +08:00
|
|
|
|
$ less sh.rustup.sh
|
|
|
|
|
$ sh ./sh.rustup.rs
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 没有类
|
|
|
|
|
|
|
|
|
|
Rust 没有类,也不使用 `class` 关键字。Rust 确实有 `struct` 数据类型,但它的作用是充当数据集合的一种模板。因此,你可以使用<ruby>结构体<rt>struct</rt></ruby>,而不是创建一个类来表示虚拟对象:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
struct Penguin {
|
2022-05-08 16:18:18 +08:00
|
|
|
|
genus: String,
|
|
|
|
|
species: String,
|
|
|
|
|
extinct: bool,
|
|
|
|
|
classified: u64,
|
2022-05-04 21:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
你可以像使用类一样使用它。例如,当定义完 `Penguin` 结构,你就可以创建它的实例,并与该实例进行交互:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
struct Penguin {
|
2022-05-08 16:18:18 +08:00
|
|
|
|
genus: String,
|
|
|
|
|
species: String,
|
|
|
|
|
extinct: bool,
|
|
|
|
|
classified: u64,
|
2022-05-04 21:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2022-05-08 16:18:18 +08:00
|
|
|
|
let p = Penguin { genus: "Pygoscelis".to_owned(),
|
|
|
|
|
species: "R adeliæ".to_owned(),
|
|
|
|
|
extinct: false,
|
|
|
|
|
classified: 1841 };
|
|
|
|
|
|
|
|
|
|
println!("Species: {}", p.species);
|
|
|
|
|
println!("Genus: {}", p.genus);
|
|
|
|
|
println!("Classified in {}", p.classified);
|
|
|
|
|
if p.extinct == true {
|
|
|
|
|
println!("Sadly this penguin has been made extinct.");
|
|
|
|
|
}
|
2022-05-04 21:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
将 `impl` 数据类型与 `struct` 数据类型结合使用,你可以实现一个包含函数的结构体,并且可以添加继承和其他与类相似的特性。
|
|
|
|
|
|
|
|
|
|
### 函数
|
|
|
|
|
|
|
|
|
|
Rust 中的函数很像其他语言中的函数。每个函数都代表一组严谨的任务,你可以在需要时调用它们。主函数名必须是 `main`。
|
|
|
|
|
|
|
|
|
|
用 `fn` 关键字声明函数,后跟函数名称和函数接受的所有参数。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
fn foo() {
|
|
|
|
|
let n = 8;
|
|
|
|
|
println!("Eight is written as {}", n);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-05-08 16:18:18 +08:00
|
|
|
|
通过参数,将信息从一个函数传递到另一个函数。例如,我已经创建了一个 `Penguin` 类(结构),并且我有一个 `Penguin` 的实例为 `p`,将目标函数的参数指定为 `Penguin` 类型,就可把 `p` 的属性从一个函数传递到另一个函数。
|
2022-05-04 21:38:00 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
fn main() {
|
|
|
|
|
let p = Penguin { genus: "Pygoscelis".to_owned(),
|
|
|
|
|
species: "R adeliæ".to_owned(),
|
|
|
|
|
extinct: false, classified: 1841 };
|
|
|
|
|
printer(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn printer(p: Penguin) {
|
|
|
|
|
println!("Species: {}", p.species);
|
|
|
|
|
println!("Genus: {}", p.genus);
|
|
|
|
|
println!("Classified in {}", p.classified);
|
|
|
|
|
if p.extinct == true {
|
2022-05-08 16:18:18 +08:00
|
|
|
|
println!("Sadly this penguin has been made extinct.");
|
2022-05-04 21:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 变量
|
|
|
|
|
|
|
|
|
|
Rust 默认创建的为<ruby>不可变<rt>immutable</rt></ruby>变量。这意味着你创建的变量以后无法更改。这段代码虽然看起来没问题,但无法编译:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
fn main() {
|
2022-05-08 16:18:18 +08:00
|
|
|
|
let n = 6;
|
|
|
|
|
let n = 5;
|
|
|
|
|
}
|
2022-05-04 21:38:00 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
但你可以使用关键字 `mut` 声明一个<ruby>可变<rt>mutable</rt></ruby>变量,因此下面这段代码可以编译成功:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
fn main() {
|
2022-05-08 16:18:18 +08:00
|
|
|
|
let mut n = 6;
|
|
|
|
|
println!("Value is {}", n);
|
|
|
|
|
n = 5;
|
|
|
|
|
println!("Value is {}", n);
|
2022-05-04 21:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 编译
|
|
|
|
|
|
|
|
|
|
Rust 编译器,至少就其报错信息而言,是可用的最好的编译器之一。当你在 Rust 中出错时,编译器会真诚地告诉你做错了什么。实际上,仅通过从编译器错误消息中学习,我就了解了 Rust 的许多细微差别(就我理解到的 Rust 的任何细微差别而言)。即便有时错误消息太过于模糊,而不知所以然,互联网搜索几乎总能得到解释。
|
|
|
|
|
|
2022-05-08 16:18:18 +08:00
|
|
|
|
启动 Rust 程序的最简单方法是使用 `cargo`,它是 Rust 的包管理和构建系统。
|
2022-05-04 21:38:00 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ mkdir myproject
|
|
|
|
|
$ cd myproject
|
|
|
|
|
$ cargo init
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
以上命令为项目创建了基本的基础架构,最值得注意的是 `src` 子目录中的 `main.rs` 文件。打开此文件,把我为本文生成的示例代码粘贴进去:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
struct Penguin {
|
2022-05-08 16:18:18 +08:00
|
|
|
|
genus: String,
|
|
|
|
|
species: String,
|
|
|
|
|
extinct: bool,
|
|
|
|
|
classified: u64,
|
2022-05-04 21:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2022-05-08 16:18:18 +08:00
|
|
|
|
let p = Penguin { genus: "Pygoscelis".to_owned(), species: "R adeliæ".to_owned(), extinct: false, classified: 1841 };
|
|
|
|
|
printer(p);
|
|
|
|
|
foo();
|
2022-05-04 21:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn printer(p: Penguin) {
|
2022-05-08 16:18:18 +08:00
|
|
|
|
println!("Species: {}", p.species);
|
|
|
|
|
println!("Genus: {}", p.genus);
|
|
|
|
|
println!("Classified in {}", p.classified);
|
|
|
|
|
if p.extinct == true {
|
|
|
|
|
println!("Sadly this penguin has been made extinct.");
|
|
|
|
|
}
|
2022-05-04 21:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn foo() {
|
2022-05-08 16:18:18 +08:00
|
|
|
|
let mut n = 6;
|
|
|
|
|
println!("Value is {}", n);
|
|
|
|
|
n = 8;
|
2022-05-04 21:38:00 +08:00
|
|
|
|
println!("Eight is written as {}", n);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
使用 `cargo build` 命令进行编译:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cargo build
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
执行 `target` 子目录下的二进制程序,或者直接运行 `cargo run` 命令来运行你的项目:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cargo run
|
|
|
|
|
Species: R adeliæ
|
|
|
|
|
Genus: Pygoscelis
|
|
|
|
|
Classified in 1841
|
|
|
|
|
Value is 6
|
|
|
|
|
Eight is written as 8
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Crates
|
|
|
|
|
|
2022-05-08 16:18:18 +08:00
|
|
|
|
任何语言的大部分便利都来自于它的库或模块。在 Rust 中,进行分发和跟踪的库称为 “crate”(箱子)。[crates.io][4] 是一个很好的社区 crate 注册网站。
|
2022-05-04 21:38:00 +08:00
|
|
|
|
|
2022-05-08 16:18:18 +08:00
|
|
|
|
把一个 crate 添加到你的 Rust 项目,首先要在 `Cargo.toml` 文件中添加这个 crate。例如,要安装随机数函数,我使用名为 `rand` 的 crate,使用 `*` 作为通配符,以确保在编译时获得最新版本:
|
2022-05-04 21:38:00 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
[package]
|
|
|
|
|
name = "myproject"
|
|
|
|
|
version = "0.1.0"
|
2022-05-08 16:18:18 +08:00
|
|
|
|
authors = ["Seth <seth@opensource.com>"]
|
2022-05-04 21:38:00 +08:00
|
|
|
|
edition = "2022"
|
|
|
|
|
|
|
|
|
|
[dependencies]
|
|
|
|
|
rand = "*"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
在 Rust 代码中使用它需要在最顶行使用 `use` 语句:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
use rand::Rng;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
以下是一些创建随机种子和随机范围的示例代码:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
fn foo() {
|
2022-05-08 16:18:18 +08:00
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
let mut n = rng.gen_range(1..99);
|
2022-05-04 21:38:00 +08:00
|
|
|
|
|
2022-05-08 16:18:18 +08:00
|
|
|
|
println!("Value is {}", n);
|
|
|
|
|
n = rng.gen_range(1..99);
|
|
|
|
|
println!("Value is {}", n);
|
2022-05-04 21:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
你可以使用 `cargo run` 来运行它,它会检测代码是否被更改并触发一个新的构建。构建过程中下载名为 `rand` 的 `crete` 和它依赖的所有 `crate`,编译代码,然后运行它:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cargo run
|
|
|
|
|
Updating crates.io index
|
|
|
|
|
Downloaded ppv-lite86 v0.2.16
|
|
|
|
|
Downloaded 1 crate (22.2 KB) in 1.40s
|
|
|
|
|
Compiling libc v0.2.112
|
|
|
|
|
Compiling cfg-if v1.0.0
|
|
|
|
|
Compiling ppv-lite86 v0.2.16
|
|
|
|
|
Compiling getrandom v0.2.3
|
|
|
|
|
Compiling rand_core v0.6.3
|
|
|
|
|
Compiling rand_chacha v0.3.1
|
|
|
|
|
Compiling rand v0.8.4
|
|
|
|
|
Compiling rustpenguin v0.1.0 (/home/sek/Demo/rustpenguin)
|
|
|
|
|
Finished dev [unoptimized + debuginfo] target(s) in 13.97s
|
|
|
|
|
Running `target/debug/rustpenguin`
|
|
|
|
|
|
|
|
|
|
Species: R adeliæ
|
|
|
|
|
Genus: Pygoscelis
|
|
|
|
|
Classified in 1841
|
|
|
|
|
Value is 70
|
|
|
|
|
Value is 35
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Rust 速查表
|
|
|
|
|
|
2022-05-08 16:18:18 +08:00
|
|
|
|
Rust 是一门令人非常愉快的语言。集成了在线注册网站、有用的编译器和几乎直观的语法,它给人的适当的现代感。
|
|
|
|
|
|
|
|
|
|
但请不要误会,Rust 仍是一门复杂的语言,它具有严格的数据类型、强作用域变量和许多内置方法。Rust 值得一看,如果你要探索它,那么你应该下载我们的免费 [Rust 速查表][6],以便快速了解基础知识。越早开始,就越早了解 Rust。当然,你应该经常练习以避免生疏。
|
2022-05-04 21:38:00 +08:00
|
|
|
|
|
2022-05-08 16:18:18 +08:00
|
|
|
|
> **[Rust 速查表][6]**
|
2022-05-04 21:38:00 +08:00
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
via: https://opensource.com/article/22/1/rust-cheat-sheet
|
|
|
|
|
|
|
|
|
|
作者:[Seth Kenlon][a]
|
|
|
|
|
选题:[lujun9972][b]
|
|
|
|
|
译者:[hanszhao80](https://github.com/hanszhao80)
|
2022-05-08 16:18:18 +08:00
|
|
|
|
校对:[wxy](https://github.com/wxy)
|
2022-05-04 21:38:00 +08:00
|
|
|
|
|
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
|
|
|
|
|
|
[a]: https://opensource.com/users/seth
|
|
|
|
|
[b]: https://github.com/lujun9972
|
|
|
|
|
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coverimage_cheat_sheet.png?itok=lYkNKieP (Cheat Sheet cover image)
|
|
|
|
|
[2]: https://opensource.com/article/20/5/rust-java
|
|
|
|
|
[3]: http://rust-lang.org
|
|
|
|
|
[4]: https://crates.io/
|
|
|
|
|
[5]: mailto:seth@opensource.com
|
|
|
|
|
[6]: https://opensource.com/downloads/rust-cheat-sheet
|