-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pengmao
committed
Dec 6, 2024
1 parent
c75a1ca
commit 9ef5a07
Showing
19 changed files
with
2,786 additions
and
1,560 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 深入生命周期 | ||
|
||
https://www.vgter.net/archives/4526 | ||
|
||
## 生命周期省略规则 | ||
|
||
在`Rust`中,生命周期省略规则用于简化代码,不需要显示的声明生命周期。对于普通函数,`Rust`提供了3条默认的生命周期推断规则,它们可以帮助编译器推断函数签名中的生命周期,而不需要手动指定 | ||
|
||
1. 每个输入参数的生命周期都可以隐时地与其函数签名相关联 | ||
2. 如果函数有一个返回值,且只有一个输入引用参数,那么返回值的生命周期会被推断为与输入参数的生命周期相同 | ||
3. 如果函数有多个输入引用参数,并且其中一个时`self`,则返回值的生命周期会与`self`的生命周期相关联、 | ||
|
||
如下例 | ||
|
||
```rust | ||
fn fn_elision(x: &i32) -> &i32 { x } | ||
``` | ||
|
||
会被`Rust`自动推断为 | ||
|
||
```rust | ||
fn fn_elision<'a>(x: &'a i32) -> &'a i32 { x } | ||
``` | ||
|
||
为什么这样推断 | ||
|
||
- `x`的类型时`&i32`,它的生命周期时`'a` | ||
- 函数的返回值类型时`&i32`,没有额外的输入引用,所以`Rust`会假设返回值的生命周期与输入参数`x`相同 | ||
|
||
### 闭包 | ||
|
||
对于闭包,`Rust`会根据使用闭包的环境来推断其生命周期。闭包的生命周期是由闭包本身的参数和返回值的生命周期决定的,而不是像函数那样严格按照省略规则推断 | ||
|
||
闭包的参数生命周期推断有以下几个关键点 | ||
|
||
1. 闭包的参数生命周期 | ||
- 闭包的参数生命周期会由函数的类型决定。闭包的生命周期推断通常与函数类似,但闭包的生命周期时根据它的上下文来决定的 | ||
2. 闭包的返回值生命周期 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# 泛型 | ||
|
||
泛型是一种强大的特性,允许定义具有多个类型参数的函数、结构体、枚举和方法。通过泛型,可以编写出更加通用、灵活和复用的代码。 | ||
|
||
## 泛型函数 | ||
|
||
- `T` 是泛型参数的名称,通常使用大写字母表示 | ||
- `T: PartialOrd` 是一个约束,表示泛型类型`T`必须实现`PartialOrd`(可比较大小) | ||
|
||
```rust | ||
fn max<T: PartialOrd>(a:T ,b:T) => T { | ||
if a > b { | ||
a | ||
}else { | ||
b | ||
} | ||
} | ||
``` | ||
|
||
使用 | ||
|
||
```rust | ||
fn main() { | ||
println!("Max of 1 and 2: {}", max(1, 2)); | ||
println!("Max of 1.0 and 2.0: {}", max(1.0, 2.0)); | ||
println!("Max of 'a' and 'b': {}", max('a', 'b')); | ||
} | ||
``` | ||
|
||
## 泛型结构体 | ||
|
||
```rust | ||
struct Point<T>{ | ||
x: T, | ||
y: T | ||
} | ||
``` | ||
|
||
使用 | ||
|
||
```rust | ||
fn main() { | ||
let integer_point = Point { x: 5, y: 10 }; | ||
let float_point = Point { x: 1.0, y: 4.0 }; | ||
|
||
println!("Integer Point: ({}, {})", integer_point.x, integer_point.y); | ||
println!("Float Point: ({}, {})", float_point.x, float_point.y); | ||
} | ||
``` | ||
|
||
## 泛型枚举 | ||
|
||
<pre v-pre> | ||
```rust | ||
enum GenericEnum<T> { | ||
Value(T), | ||
Empty, | ||
} | ||
|
||
fn main() { | ||
let some_value = GenericEnum::Value(42); | ||
let no_value: GenericEnum<i32> = GenericEnum::Empty; | ||
|
||
match some_value { | ||
GenericEnum::Value(val) => println!("Value: {}", val), | ||
GenericEnum::Empty => println!("No value!"), | ||
} | ||
} | ||
|
||
``` | ||
</pre> | ||
|
||
## 泛型方法 | ||
|
||
使用`<>`加以约束 | ||
|
||
<pre v-pre> | ||
```rust | ||
struct Point<T> { | ||
x: T, | ||
y: T, | ||
} | ||
impl<T> Point<T> { | ||
// 泛型方法 | ||
fn x(&self) -> &T { | ||
&self.x | ||
} | ||
} | ||
impl Point<f64> { | ||
fn distance_from_origin(&self) -> f64 { | ||
(self.x.powi(2) + self.y.powi(2)).sqrt() | ||
} | ||
} | ||
fn main() { | ||
let integer_point = Point { x: 5, y: 10 }; | ||
let float_point = Point { x: 3.0, y: 4.0 }; | ||
|
||
println!("integer_point.x = {}", integer_point.x()); | ||
println!("float_point distance from origin = {}", float_point.distance_from_origin()); | ||
} | ||
``` | ||
</pre> | ||
|
||
## 泛型的约束 | ||
|
||
默认情况下,`Rust`的泛型参数是完全通用的,不会对类型做任何假设。但在某些场景下,我们需要对泛型施加约束。 | ||
|
||
1. 实现某个trait | ||
|
||
```rust | ||
fn print<T: std::fmt::Debug>(x: T){ | ||
print("{:?}", x); | ||
} | ||
``` | ||
|
||
2. 多个 trait 约束 | ||
|
||
```rust | ||
fn compare_and_print<T: PartialOrd + std::fmt::Debug>(a: T, b: T) { | ||
if a > b { | ||
println!("{:?} is greater than {:?}", a, b); | ||
} else { | ||
println!("{:?} is less than or equal to {:?}", a, b); | ||
} | ||
} | ||
|
||
``` | ||
|
||
3. `where` 语法 | ||
|
||
当约束较多时,可以使用 where 提高代码可读性: | ||
|
||
```rust | ||
fn compare_and_print<T>(a: T, b: T) | ||
where | ||
T: PartialOrd + std::fmt::Debug, | ||
{ | ||
if a > b { | ||
println!("{:?} is greater than {:?}", a, b); | ||
} else { | ||
println!("{:?} is less than or equal to {:?}", a, b); | ||
} | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# 注释 | ||
|
||
## 单行注释 | ||
|
||
单行注释以`//`开头,注释内容在其后即可 | ||
|
||
```rust | ||
// 这是一个单行注释 | ||
fn main() { | ||
let x = 42; // 变量 x 被初始化为 42 | ||
println!("Hello, world!"); // 输出文本 | ||
} | ||
|
||
``` | ||
|
||
## 块注释 | ||
|
||
块注释使用`/*`开始,`*/`结束,可以跨越多行 | ||
|
||
```rust | ||
/* | ||
这是一个块注释 | ||
它可以跨越多行 | ||
*/ | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
} | ||
|
||
``` | ||
|
||
## 文档注释 | ||
|
||
### 单行文档注释 | ||
|
||
单行文档注释通常用于对函数、方法、结构体、枚举等进行简短的描述。生成的文档会被格式化成Markdown | ||
|
||
/// 语法会使得 rustdoc 在生成文档时,提取函数的注释并将其格式化成 HTML 页面上的文档。你可以看到,注释中包含了函数的描述、参数说明以及返回值的描述。 | ||
|
||
```rust | ||
/// 这是一个求和函数 | ||
/// 它接受两个整数参数并返回它们的和 | ||
/// | ||
/// # 参数 | ||
/// - `a`: 第一个整数 | ||
/// - `b`: 第二个整数 | ||
/// | ||
/// # 返回值 | ||
/// 返回 `a` 和 `b` 的和 | ||
fn add(a: i32, b: i32) -> i32 { | ||
a + b | ||
} | ||
|
||
``` | ||
|
||
### 多行文档注释 | ||
|
||
多行文档注释以 //\*_ 开始,_/ 结束,适合较长的描述,特别是在处理复杂结构或函数时。 | ||
|
||
多行注释通常用于详细描述函数的行为和细节,可以包含多个段落,并且支持更灵活的格式化。 | ||
|
||
```rust | ||
/** | ||
* 这是一个计算矩形面积的函数 | ||
* | ||
* 该函数接受矩形的宽度和高度作为输入参数,返回矩形的面积。 | ||
* | ||
* # 参数 | ||
* - `width`: 矩形的宽度 | ||
* - `height`: 矩形的高度 | ||
* | ||
* # 返回值 | ||
* 返回矩形的面积,单位是平方单位。 | ||
*/ | ||
fn calculate_area(width: f64, height: f64) -> f64 { | ||
width * height | ||
} | ||
|
||
``` | ||
|
||
也可以在其中写Markdown语法 | ||
|
||
````rust | ||
/// **加粗文本** 和 *斜体文本* | ||
|
||
/// ``` | ||
/// let x = 10; | ||
/// println!("{}", x); | ||
/// ``` | ||
|
||
```` |
Oops, something went wrong.