doc: update rust.md #70

This commit is contained in:
jaywcjlove 2022-11-15 09:46:55 +08:00
parent aab4d3bcff
commit 0bc31c68c3

View File

@ -9,18 +9,17 @@ Rust 快速参考备忘单,旨在为编写基本语法和方法提供帮助。
### 配置 vscode 调试 ### 配置 vscode 调试
<!--rehype:wrap-class=row-span-2--> <!--rehype:wrap-class=row-span-2-->
参考:<https://github.com/vadimcn/vscode-lldb/blob/master/MANUAL.md#source-path-remapping> [配置参考](https://github.com/vadimcn/vscode-lldb/blob/master/MANUAL.md#source-path-remapping)。下载 CodeLLDB选择 rust 自动生成 launch.json 文件
下载 CodeLLDB选择 rust 自动生成 launch.json 文件
```json ```json
{ {
"configurations": [ "configurations": [
// 添加一下行,使 vechashmap 等类型显示正常 // 添加一下行,使 vec/hashmap 等类型显示正常
"sourceLanguages": ["rust"] "sourceLanguages": ["rust"]
] ]
} }
``` ```
<!--rehype:className=wrap-text -->
---- ----
@ -34,6 +33,7 @@ Rust 快速参考备忘单,旨在为编写基本语法和方法提供帮助。
} }
} }
``` ```
<!--rehype:className=wrap-text -->
### Hello_World.rs ### Hello_World.rs
@ -506,6 +506,7 @@ fn main(){
}; };
} }
``` ```
<!--rehype:className=wrap-text -->
### 枚举的变体 ### 枚举的变体
@ -520,6 +521,7 @@ fn main() {
let loopback = IpAddrKind::V6(String::from("::1")); let loopback = IpAddrKind::V6(String::from("::1"));
} }
``` ```
<!--rehype:className=wrap-text -->
---- ----
@ -537,6 +539,7 @@ fn main(){
let c = Message::ChangeColor(10, 20, 30); let c = Message::ChangeColor(10, 20, 30);
} }
``` ```
<!--rehype:className=wrap-text -->
### 模式匹配结构体 ### 模式匹配结构体
@ -563,6 +566,7 @@ fn main() {
subject_grade(Subject::Math(Grade::A)); subject_grade(Subject::Math(Grade::A));
} }
``` ```
<!--rehype:className=wrap-text -->
Rust 运算符 Rust 运算符
----------- -----------
@ -767,13 +771,13 @@ match 模式匹配,使用 `a | b` 表示匹配 a **或** b使用 `_`,表
```rust ```rust
fn main(){ fn main(){
let grade = Grade::A; let grade = Grade::A;
match grade { match grade {
Grade::A => println!("Good"), Grade::A => println!("Good"),
Grade::B => println!("Not bad"), Grade::B => println!("Not bad"),
Grade::C | Grade::D => println!("Come on"), Grade::C | Grade::D => println!("Come on"),
_ => println!("emmm"), _ => println!("emmm"),
} }
} }
enum Grade { enum Grade {
@ -785,6 +789,7 @@ enum Grade {
F, F,
} }
``` ```
<!--rehype:className=wrap-text -->
#### `matches!` #### `matches!`
@ -794,6 +799,7 @@ enum Grade {
assert!(matches!('x' ',A'..='Z' | 'a'..='z')); assert!(matches!('x' ',A'..='Z' | 'a'..='z'));
assert!(matches!(Some(101), Some(x) if x > 100)); assert!(matches!(Some(101), Some(x) if x > 100));
``` ```
<!--rehype:className=wrap-text -->
### if let 匹配 ### if let 匹配
@ -802,10 +808,11 @@ match 表达式需要匹配所有的枚举才能结束,但通常我们只需
```rust ```rust
let x = 3; let x = 3;
match Some(x) { match Some(x) {
Some(3) => println!("I guess that x is 3"), Some(3) => println!("I guess that x is 3"),
_ => () _ => ()
} }
``` ```
<!--rehype:className=wrap-text -->
使用 `if let` 使用 `if let`
@ -837,6 +844,7 @@ while let Some(top) = stack.pop() {
```rust ```rust
for (i, v) in collection.iter().enumerate(){} for (i, v) in collection.iter().enumerate(){}
``` ```
<!--rehype:className=wrap-text -->
#### let #### let
@ -875,6 +883,7 @@ match origin {
Point { x, .. } => println!("x is {}", x), Point { x, .. } => println!("x is {}", x),
} }
``` ```
<!--rehype:className=wrap-text -->
#### 使用 `_` 忽略部分参数 #### 使用 `_` 忽略部分参数
@ -887,6 +896,7 @@ match hello {
}, },
} }
``` ```
<!--rehype:className=wrap-text -->
### 匹配命名变量 ### 匹配命名变量
@ -913,6 +923,7 @@ match grade {
_ => println!("Come on"), _ => println!("Come on"),
} }
``` ```
<!--rehype:className=wrap-text -->
---- ----
@ -928,6 +939,7 @@ fn main(){
println!("{:?}", p); println!("{:?}", p);
} }
``` ```
<!--rehype:className=wrap-text -->
---- ----
@ -952,6 +964,7 @@ match x {
_ => println!("No match"), _ => println!("No match"),
}// y = 2 }// y = 2
``` ```
<!--rehype:className=wrap-text -->
Rust 函数 Rust 函数
-------- --------