move rust-codes into rust-course

pull/136/head
sunface 3 years ago
parent e148d9faf3
commit 6195ebd75c

@ -2,7 +2,7 @@
authors = ["sunface"] authors = ["sunface"]
language = "zh-CN" language = "zh-CN"
title = "Rust语言圣经(Rust教程 Rust Course)" title = "Rust语言圣经(Rust教程 Rust Course)"
src = "course-book/contents" src = "contents"
[output.html] [output.html]
additional-css = ["assets/ferris.css", "assets/theme/2018-edition.css"] additional-css = ["assets/ferris.css", "assets/theme/2018-edition.css"]

@ -117,7 +117,21 @@
- [自定义错误](errors/user-define.md) - [自定义错误](errors/user-define.md)
- [让错误输出更优雅](errors/pretty-format.md) - [让错误输出更优雅](errors/pretty-format.md)
- [会导致panic的代码](errors/panic-codes.md) - [会导致panic的代码](errors/panic-codes.md)
- [Cargo详解 todo](cargo/intro.md)
- [常用命令](cargo/commands.md)
- [项目结构](cargo/layout.md)
- [Cargo.toml和Cargo.lock](cargo/cargo-toml-lock.md)
- [依赖管理](cargo/dependency.md)
- [构建缓存](cargo/cache.md)
- [版本管理](cargo/version.md)
- [工作空间](cargo/workspace.md)
- [条件编译、条件依赖](cargo/feature.md)
- [配置参数(todo)](cargo/manifest.md)
- [自定义构建脚本](cargo/build-js.md)
- [Cargo profile](cargo/profile.md)
- [测试 todo](test/intro.md) - [测试 todo](test/intro.md)
- [单元测试](test/unit.md) - [单元测试](test/unit.md)
- [集成测试](test/intergration.md) - [集成测试](test/intergration.md)
@ -214,19 +228,6 @@
- [命令行解析](libraries/command/intro.md) - [命令行解析](libraries/command/intro.md)
- [structopt(todo)](libraries/command/structopt.md) - [structopt(todo)](libraries/command/structopt.md)
- [Cargo详解 todo](cargo/intro.md)
- [常用命令](cargo/commands.md)
- [项目结构](cargo/layout.md)
- [Cargo.toml和Cargo.lock](cargo/cargo-toml-lock.md)
- [依赖管理](cargo/dependency.md)
- [构建缓存](cargo/cache.md)
- [版本管理](cargo/version.md)
- [工作空间](cargo/workspace.md)
- [条件编译、条件依赖](cargo/feature.md)
- [配置参数(todo)](cargo/manifest.md)
- [自定义构建脚本](cargo/build-js.md)
- [Cargo profile](cargo/profile.md)
## 附录 ## 附录
- [附录](appendix/intro.md) - [附录](appendix/intro.md)
- [A-关键字](appendix/keywords.md) - [A-关键字](appendix/keywords.md)

@ -1,7 +1,67 @@
## 结构体自引用 ## 结构体自引用
结构体自引用在Rust中是一个众所周知的难题而且众说纷纭也没有一篇文章能把相关的话题讲透那本文就王婆卖瓜来试试看能不能讲透这一块儿内容让读者大大们舒心。 结构体自引用在Rust中是一个众所周知的难题而且众说纷纭也没有一篇文章能把相关的话题讲透那本文就王婆卖瓜来试试看能不能讲透这一块儿内容让读者大大们舒心。
> 这章内容足足花了半个月的时间来准备! - 来自作者的诉苦 ## 平平无奇的自引用
可能也有不少人第一次听说自引用结构体,那咱们先来看看它们长啥样。
```rust
struct RefWithinMe<'a> {
value: String,
// 该引用指向上面的value
pointer_to_value: &'a str,
}
```
以上就是一个很简单的自引用结构体,看上去好像没什么,那来试着运行下:
```rust
fn main(){
let s = "aaa".to_string();
let v = SelfRef {
value: s,
pointer_to_value: &s
};
}
```
运行后报错:
```console
let v = SelfRef {
12 | value: s,
| - value moved here
13 | pointer_to_value: &s
| ^^ value borrowed here after move
```
因为我们试图同时使用值和值的引用,最终所有权转移和借用一起发生了。所以,这个问题貌似并没有那么好解决,不信你可以回想下自己具有的知识,是否可以解决?
#### 使用ouroboros
## 玉树临风的自引用
```rust
use std::str;
struct MyStruct<'a>{
Buf: Vec<u8>,
repr: Parsed<'a>
}
struct Parsed<'a>{
name:&'a str
}
fn main(){
let v = vec!(0065,0066,0067,0068,0069);
let s = str::from_utf8(&v).unwrap();
println!("{}",s);
let p = &v[1..=3];
let s1 = str::from_utf8(p).unwrap();
println!("{}",s1);
let par = Parsed{name:s1};
let new1 = MyStruct{Buf:v,repr:par};
}
```
## 使用Pin来解决自引用 ## 使用Pin来解决自引用
Pin在后续章节会深入讲解目前你只需要知道它可以固定住一个值防止该值的所有权被转移。通过Pin也可以实现自引用的数据结构: Pin在后续章节会深入讲解目前你只需要知道它可以固定住一个值防止该值的所有权被转移。通过Pin也可以实现自引用的数据结构:

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 6.1 KiB

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save