Skip to content

Commit a724895

Browse files
committed
mod_trait_exerci = "0.2.1"
1 parent a8ae799 commit a724895

File tree

85 files changed

+1346
-411
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1346
-411
lines changed

docs/zh-first-volumn/src/SUMMARY.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
- [使用软件篋borrowing_exerci](./hello-borrowing/apply.md)
2323
- [软件篋trait_exerci](./hello-trait/README.md)
2424
- [类型关键词`struct`](./hello-trait/examples-struct.md)
25-
- [实现关键词`impl`与方法代码实现](./hello-trait/examples-impl.md)
26-
- [方法代码实现](./hello-trait/examples-methods.md)
27-
- [衔接关键词trait与修饰关键词mut](./hello-trait/examples-trait.md)
25+
- [关键词`impl`与方法代码实现](./hello-trait/examples-impl.md)
26+
- [函数与方法代码实现](./hello-trait/examples-methods.md)
27+
- [衔接关键词trait](./hello-trait/examples-trait.md)
28+
- [特质实现及其对象](./hello-trait/examples-trait-impl.md)
2829
- [题外话:默认衔接特质Default](./hello-trait/examples-trait-default.md)
2930
- [程序代码结构](./hello-trait/lib-structure.md)
3031
- [关于软件篋项目](./hello-trait/about.md)
@@ -42,7 +43,7 @@
4243
- [共享篋:简单三层结构实现](./hello-mod-trait/lib-fn.md)
4344
- [共享篋:基于封装的静态调度实现](./hello-mod-trait/lib-static.md)
4445
- [共享篋:基于封装的动态调度实现](./hello-mod-trait/lib-dynamic.md)
45-
- [共享篋:目录测试代码解释](./hello-mod-trait/lib-tests.md)
46+
- [共享篋:单元测试代码解释](./hello-mod-trait/lib-tests.md)
4647
- [题外话:泛型实例](./hello-mod-trait/off-topic.md)
4748
- [软件篋deref_exerci](./hello-deref/README.md)
4849
- [关于软件篋deref_exerci](./hello-deref/about.md)

docs/zh-first-volumn/src/hello-mod-trait/examples-miri.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [与Miri相关的Cargo工具命令](#与Miri相关的Cargo工具命令)
1313
- [打开Miri代码文件命令](#打开Miri代码文件命令)
1414
- [Miri代码实例说明](#Miri代码实例说明)
15+
- [类型泛型参数与特质对象](#类型泛型参数与特质对象)
1516
- [参考资料](#参考资料)
1617

1718
## 什么是Miri
@@ -87,5 +88,9 @@ fn dynamic_dispatch(_1: &dyn Trait) -> () {
8788
}
8889
```
8990

91+
## 类型泛型参数与特质对象
92+
93+
  类型泛型参数(generics type parameters)与特质对象(trait objects)
94+
9095
## 参考资料
9196
- [An interpreter for Rust's mid-level intermediate representation](https://github.com/rust-lang/miri)
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
# 共享篋:基于封装的动态调度实现
22

3+
  在这一节里,列出基于指针类型`Box`封装的动态调度实现。
34

5+
## 学习内容
6+
- 了解和学习基于指针类型`Box`封装的动态函数实现方法
7+
8+
## 篇目
9+
- [动态模块实现](#动态模块实现)
10+
- [动态模块应用](#动态模块应用)
11+
- [参考资料](#参考资料)
12+
13+
## 动态模块实现
414

515
{{#playpen ../../../../hello-mod-trait/lib-hello/src/mod_box_dynamic_fn.rs editable}}
616

7-
{{#playpen ../../../../hello-mod-trait/lib-hello/examples/box_dynamic_hello.rs editable}}
17+
## 动态模块应用
18+
19+
{{#playpen ../../../../hello-mod-trait/lib-hello/examples/box_dynamic_hello.rs editable}}
20+
21+
## 参考资料
22+
-

docs/zh-first-volumn/src/hello-mod-trait/lib-fn.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,4 @@
4040

4141
{{#playpen ../../../../hello-mod-trait/lib-hello/examples/trait_fn_hello.rs editable}}
4242

43-
44-
45-
4643
## 参考资料
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
# 共享篋:基于封装的静态调度实现
22

3-
  在这一节里,学习Cargo项目文件、目录与模块相互关系。Rust语言表达模块的三种方式
3+
  在这一节里,列出基于指针类型`Box`封装的静态调度实现
44

55
## 学习内容
6-
- 了解和学习本软件篋模块文件结构
7-
- 理解和掌握Cargo项目目录与文件关系
8-
- 理解和掌握Cargo项目模块与文件关系
9-
- 理解和掌握Cargo项目文件与文件关系
6+
- 了解和学习基于指针类型`Box`封装的静态函数实现方法
107

118
## 篇目
12-
- [文件本身表达模块方式](#文件本身表达模块方式)
13-
- [文件名称表达模块方式](#文件名称表达模块方式)
14-
- [目录名称表达模块方式](#目录名称表达模块方式)
9+
- [静态模块实现](#静态模块实现)
10+
- [静态模块应用](#静态模块应用)
1511
- [参考资料](#参考资料)
1612

13+
## 静态模块实现
1714

18-
15+
  
1916

2017
{{#playpen ../../../../hello-mod-trait/lib-hello/src/mod_box_static_fn.rs editable}}
2118

19+
## 静态模块应用
20+
21+
{{#playpen ../../../../hello-mod-trait/lib-hello/examples/box_static_hello.rs editable}}
22+
2223

23-
{{#playpen ../../../../hello-mod-trait/lib-hello/examples/box_static_hello.rs editable}}
24+
## 参考资料
25+
- [std Box](https://doc.rust-lang.org/std/boxed/struct.Box.html)
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
1-
# 共享篋:目录tests的测试代码解释
1+
# 共享篋:单元测试代码解释
2+
3+
  在这一节里,实现共享篋每一个模块的单元测试代码。
4+
5+
## 学习内容
6+
- 了解和学习实现基于不同类型的单元测试方法
7+
8+
## 篇目
9+
- [模块`mod_bare`单元测试](#模块mod_bare单元测试)
10+
- [基于实例的模块`mod_trait`单元测试](#基于实例的模块mod_trait单元测试)
11+
- [基于中间模块的实例单元测试](#基于中间模块的实例单元测试)
12+
- [基于中间模块静态函数的类型`Box`单元测试](#基于中间模块静态函数的类型Box单元测试)
13+
- [基于中间模块动态函数的类型`Box`单元测试](#基于中间模块动态函数的类型Box单元测试)
14+
- [参考资料](#参考资料)
15+
16+
## 模块`mod_bare`单元测试
17+
18+
{{#playpen ../../../../hello-mod-trait/lib-hello/tests/u_for_mod_bare.rs editable}}
19+
20+
## 基于实例的模块`mod_trait`单元测试
21+
22+
{{#playpen ../../../../hello-mod-trait/lib-hello/tests/u_for_mod_trait_instance.rs editable}}
23+
24+
## 基于中间模块的实例单元测试
25+
26+
{{#playpen ../../../../hello-mod-trait/lib-hello/tests/u_for_mod_trait_fn.rs editable}}
27+
28+
## 基于中间模块静态函数的类型`Box`单元测试
29+
30+
{{#playpen ../../../../hello-mod-trait/lib-hello/tests/u_for_box_static_hello.rs editable}}
31+
32+
## 基于中间模块动态函数的类型`Box`单元测试
33+
34+
{{#playpen ../../../../hello-mod-trait/lib-hello/tests/u_for_box_dynamic_hello.rs editable}}
35+
36+
## 参考资料
37+
-

docs/zh-first-volumn/src/hello-trait/README.md

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,17 @@
11
# 第二章 软件篋trait_exerci
22

3-
### 学习内容
3+
### 本章学习内容
44
- 什么是Rust语言衔接关键词trait
55
- 为什么需要衔接关键词trait
66
- 怎么样实现Rust语言衔接关键词trait
77
- 实现关键词impl与衔接关键词trait是什么关系
88

9-
### 篇目
9+
### 本节篇目
1010

11-
1. [关键词trait概念表述之一](#关键词trait概念表述之一)
12-
1. [关键词trait概念表述之二](#关键词trait概念表述之二)
13-
1. [关键词trait概念表述之三](#关键词trait概念表述之三)
14-
1. [解释关键词trait](#解释关键词trait)
15-
1. [题外话](#题外话)
16-
1. [怎么样使用Rust语言nightly版本](#怎么样使用rust语言nightly版本)
17-
1. [参考资料](#参考资料)
18-
1. [本章参考资料](#本章参考资料)
19-
20-
## 关键词trait概念表述之一
21-
22-
  [关键词trait][id_01]是Rust语言的一项功能,可以告诉Rust编译器一种类型必须提供的功能。
23-
24-
## 关键词trait概念表述之二
25-
26-
  [关键词trait][id_02]是为任何未知类型定义方法的集合。
27-
28-
## 关键词trait概念表述之三
29-
30-
  [关键词trait][id_03]告诉Rust编译器一种特定的类型具有且可与其他类型共享的功效性质。
31-
32-
## 解释关键词trait
33-
34-
  关键词trait提供了一种类型或者几种类型之间的衔接方式。它应该包含下面内容:
35-
36-
- 存在一种类型或者几种类型
37-
- 使用关键词trait定义衔接名称
38-
- 使用关键词trait代码块定义方法和函数
39-
- 使用关键词"impl"和"for"组合,实现针对这一种类型或者这几种类型的方法和函数
11+
- [题外话](#题外话)
12+
- [怎么样使用Rust语言nightly版本](#怎么样使用rust语言nightly版本)
13+
- [参考资料](#参考资料)
14+
- [本章参考资料](#本章参考资料)
4015

4116
## 题外话
4217

docs/zh-first-volumn/src/hello-trait/examples-impl.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 实现关键词`impl`与方法代码实现
1+
# 关键词`impl`与方法代码实现
22

33
## 学习内容
44
- 阐述关键词`impl`基本概念

docs/zh-first-volumn/src/hello-trait/examples-methods.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
# 方法代码实现
1+
# 函数与方法代码实现
22

33
## 学习内容
44
- 阐述Rust语言函数与方法概念区别
55
- 理解实现函数与方法手段
66

77
## 篇目
88

9-
1. [修饰实例关键词`mut`概念](#修饰实例关键词mut概念)
10-
1. [函数与方法概念](#函数与方法概念)
11-
1. [实现方法代码](#实现方法代码)
12-
1. [结构类型属性的私有性](#结构类型属性的私有性)
13-
1. [题外话](#题外话)
14-
1. [在终端里怎么样使用表格形式](#在终端里怎么样使用表格形式)
15-
1. [参考资料](#参考资料)
9+
- [修饰实例关键词`mut`概念](#修饰实例关键词mut概念)
10+
- [函数与方法概念](#函数与方法概念)
11+
- [实现方法代码](#实现方法代码)
12+
- [结构类型属性的私有性](#结构类型属性的私有性)
13+
- [题外话](#题外话)
14+
- [在终端里怎么样使用表格形式](#在终端里怎么样使用表格形式)
15+
- [参考资料](#参考资料)
1616

1717
## 修饰实例关键词`mut`概念
1818

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 特质实现及其对象
2+
3+
## 学习内容
4+
- 理解衔接类型关键词`trait`的实现
5+
6+
## 篇目
7+
8+
- [程序结构图与衔接类型关键词`trait`](#程序结构图与衔接类型关键词trait)
9+
- [实现基于默认方法的关键词`trait`代码](#实现基于默认方法的关键词trait代码)
10+
- [特质对象解释](#特质对象解释)
11+
- [题外话](#题外话)
12+
- [什么是可衍生特质](#什么是可衍生特质)
13+
- [参考资料](#参考资料)
14+
15+
## 程序结构图与衔接类型关键词trait
16+
17+
![image](../../images/hello-trait-03-trait-impl.png)
18+
19+
## 实现基于默认方法的关键词trait代码
20+
21+
  通过下面的代码,可以学习到这些知识:
22+
23+
- 使用关键词`trait`,定义了特质`TraitCanal`的默认实例化函数`init()`
24+
- 使用关键词`impl``for`,基于结构类型`StructType`,为特质`TraitCanal`实现了方法`new()`、get_data()``set_data()`;
25+
- 借助于特质`TraitCanal`的默认实例化函数`init()`,实现该结构类型的实例化方式;
26+
27+
{{#playpen ../../../../hello-trait/lib-hello/examples/trait_with_default_method.rs editable}}
28+
29+
## 特质对象解释
30+
31+
  特质本身不能定义特质对象,而是通过类型的对象,进行强制转换得到的特质对象。特质对象可以访问类型的公共数据和公共特质的行为。
32+
33+
  Rust语言把面向对象编程的思想更加深化了。把类的数据与行为分散化定义,而把类的实例集成化使用。不仅如此,而且Rust语言在代码里完全把这种过程都隐藏起来了。
34+
35+
![image](../../images/hello-trait-31-trait-object.png)
36+
37+
## 题外话
38+
39+
### 什么是可衍生特质
40+
41+
  Rust语言标准库或者第三方提供了一些非常有用的特质,称之为可衍生特质(Derivable Trait)。通过注释`#[derive(特质名称)]`,编译器能够为这些特质提供实现。比如,要求类型实现是可打印的,可以使用特质std::fmt::Debug。具体说,使用可衍生特质#[derive(Debug)],所有类型都可以自动创建地实现std::fmt::Debug。
42+
43+
  下面的代码里第一行就是注释可衍生特质`Debug`,为类型`Person`实现了特质`Debug`,这些后面的宏`println!()`就可以使用了这个特质。
44+
45+
  注意,使用注释`#[derive(特质名称)]`,必须紧挨着类型定义之上。
46+
47+
```rust
48+
#[derive(Debug)]
49+
struct Person {
50+
name: String,
51+
age: u32,
52+
}
53+
54+
impl Person {
55+
fn init() -> Person {
56+
Person {
57+
name: String::new(),
58+
age: 0,
59+
}
60+
}
61+
}
62+
63+
fn main() {
64+
let person = Person::init();
65+
println!("{:?}", person);
66+
}
67+
```
68+
69+
## 参考资料
70+
- [trait std::fmt::Debug](https://doc.rust-lang.org/std/fmt/trait.Debug.html)
71+
- [rust-by-example derive](https://doc.rust-lang.org/rust-by-example/trait/derive.html)
72+
- [rust-by-example print_debug](https://doc.rust-lang.org/rust-by-example/hello/print/print_debug.html)
73+
- [appendix-03-derivable-traits](https://doc.rust-lang.org/book/appendix-03-derivable-traits.html#appendix-c-derivable-traits)

0 commit comments

Comments
 (0)