This commit is contained in:
Davirain
2023-05-17 16:48:56 +08:00
parent d3aee1ea95
commit 0612b0c954
68 changed files with 14 additions and 14 deletions

View File

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 46 KiB

View File

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

View File

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

View File

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 46 KiB

View File

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

Before

Width:  |  Height:  |  Size: 9.9 KiB

After

Width:  |  Height:  |  Size: 9.9 KiB

View File

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

Before

Width:  |  Height:  |  Size: 4.9 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB

View File

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View File

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

Before

Width:  |  Height:  |  Size: 6.8 KiB

After

Width:  |  Height:  |  Size: 6.8 KiB

View File

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View File

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 45 KiB

View File

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View File

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 35 KiB

View File

Before

Width:  |  Height:  |  Size: 43 KiB

After

Width:  |  Height:  |  Size: 43 KiB

View File

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

View File

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 29 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 45 KiB

View File

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 30 KiB

View File

Before

Width:  |  Height:  |  Size: 39 KiB

After

Width:  |  Height:  |  Size: 39 KiB

View File

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 51 KiB

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View File

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 38 KiB

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 23 KiB

View File

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 34 KiB

View File

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

Before

Width:  |  Height:  |  Size: 49 KiB

After

Width:  |  Height:  |  Size: 49 KiB

View File

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 35 KiB

View File

Before

Width:  |  Height:  |  Size: 48 KiB

After

Width:  |  Height:  |  Size: 48 KiB

View File

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 33 KiB

View File

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View File

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View File

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View File

@@ -149,7 +149,7 @@ cargo new hello_world
创建后,可以看到整个目录的结构如下:
![目录结构](./assets/62.png)
![目录结构](../assets/62.png)
其中Cargo.toml是项目的配置文件src为源代码目录main.rs为主程序文件。
@@ -172,7 +172,7 @@ cargo run
```
运行结果如下:
![运行结果](./assets/63.png)
![运行结果](../assets/63.png)
### 2.3.4 理解Rust源码的基本结构
下面简单介绍Rust源码的结构。

View File

@@ -1,6 +1,6 @@
## 3.6 Rust内存模型
### 3.6.1 Rust程序内存布局
![注释](../assets/2.png)
![注释](../../assets/2.png)
上图是一张linux系统上Rust程序的内存布局图。在linux操作系统中会划分固定的区域给内核使用即上图中的内核空间应用程序使用的是用户空间。
@@ -38,7 +38,7 @@ fn main() {
}
```
对于上面的代码在执行第17行和第18行的栈帧示意图如下
![注释](../assets/3.png)
![注释](../../assets/3.png)
这里需要注意的是两个帧对应同样的内存地址这是因为在调用完f1函数后其对应的栈帧释放释放的实际意义就是这段内存可以被重新分配了然后调用f2函数为其分配栈帧时从同样的地址进行分配。
@@ -51,4 +51,4 @@ Rust没有GC但通过其独特的机制管理内存程序员不用手动
栈中存储的所有数据都必须占用(在编译时就)已知且固定的大小。编译时大小未知或可能变化的数据,存储在堆上。
数据存放到栈上时,是直接将数据放到栈内存。
当数据需要存放到堆上时,内存分配器则是根据数据的大小,在堆内存找到合适大小的空区域存放,把它标记为已使用,并返回一个表示该位置地址的指针。该指针存储在栈上,当需要访问具体的数据时,必须先访问指针,然后通过指针找到堆上的位置,从而访问数据。这个过程可以用下图表示:
![注释](../assets/4.png)
![注释](../../assets/4.png)

View File

@@ -139,7 +139,7 @@ fn main() {
```
在第2行定义String类型时并不能确定最终字符串的大小所以字符串内容本身应该存储在堆上。结合什么String类型的本质的内容可以得到String类型的存储如下
![注释](../assets/5.png)
![注释](../../assets/5.png)
String类型本身是三个字段指针、长度、容量在编译时是已知的大小存储在栈上String类型绑定的字符串在上面代码中是“AB”在编译时大小未知是运行时在堆上分配内存分配后的内存地址保存在String类型的指针字段中内存大小保存在cap字段中内存上存储的字符串长度保存在len字段中。
@@ -171,11 +171,11 @@ fn main() {
```
s是String类型字符串“Hello world”是存储在堆内存上的其内存布局如下
![注释](../assets/6.png)
![注释](../../assets/6.png)
当执行let s1 = s后内存布局如下
![注释](../assets/7.png)
![注释](../../assets/7.png)
当let s1 = s执行后就发生了所有权的转移String类型值的所有权从s转移到了s1。此时Rust认为原来的s不再有效。因此上面代码第4行打开编译将会出错。
@@ -185,14 +185,14 @@ s是String类型字符串“Hello world”是存储在堆内存上的
只拷贝栈上的内容,就叫做浅拷贝。
对于上面的String类型执行let s1 = s后只把s的ptr、len、cap中的值拷贝给s1的ptr、len、cap的值这种就叫做浅拷贝。浅拷贝发生后s的ptr和s1的ptr都指向同样的堆内存。内存布局如下
![注释](../assets/8.png)
![注释](../../assets/8.png)
- 深拷贝
除了拷贝栈上的内容外,还拷贝堆内存中的内容,就叫做深拷贝。
对于上面的String类型执行let s1 = s后除了把s的len、cap中的值拷贝给s1的len、cap外还在堆上重新分配一块内存将s的ptr指向的堆内存的内容拷贝到这块内存然后s1的ptr指向这块内存这种拷贝就叫做深拷贝。深拷贝发生后s的ptr和s1的ptr指向不同的堆内存但是堆内存中存储的内容一样。深拷贝发生后的内存布局如下
![注释](../assets/9.png)
![注释](../../assets/9.png)
显然,**Rust中变量赋值Rust中叫所有权转移使用的是浅拷贝**。

View File

@@ -40,7 +40,7 @@ fn print(s: String) -> String {
```
上面代码中变量a、b、c、d的内存布局如下
![注释](../assets/10.png)
![注释](../../assets/10.png)
**获取变量的引用,称之为借用** 。通过借用,允许使用被引用变量绑定的值,同时又没有移动该变量的所有权。前面的示例代码可以变成如下:
```Rust
@@ -240,11 +240,11 @@ int main()
```
在执行第14行前其内存布局为
![注释](../assets/11.png)
![注释](../../assets/11.png)
当执行第14行后变成如下
![注释](../assets/12.png)
![注释](../../assets/12.png)
第14行执行后ptr就变成了一个悬垂指针或者交悬垂引用然后在第16行继续使用ptr则会发生错误。

View File

@@ -44,7 +44,7 @@ fn main() {
&str和&String的内存布局如下
![注释](../assets/13.png)
![注释](../../assets/13.png)
#### 3. 其它Slice
数组的Slice如下