Skip to content

Commit 2ad2af4

Browse files
committed
mod_trait_exerci = "0.1.2"
1 parent f6db741 commit 2ad2af4

File tree

170 files changed

+5972
-397
lines changed

Some content is hidden

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

170 files changed

+5972
-397
lines changed

appveyor.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,8 @@ test_script:
5656
- cd ..
5757
- cd hello-trait
5858
- cargo build --verbose
59-
- cargo test
59+
- cargo test
60+
- cd ..
61+
- cd hello-mod-trait
62+
- cargo build --verbose
63+
- cargo test

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,28 @@
2121
- [开发软件篋borrowing_exerci](./hello-borrowing/develop.md)
2222
- [使用软件篋borrowing_exerci](./hello-borrowing/apply.md)
2323
- [软件篋trait_exerci](./hello-trait/README.md)
24-
- [类型关键词struct](./hello-trait/examples-struct.md)
25-
- [实现关键词impl与方法代码实现](./hello-trait/examples-impl.md)
24+
- [类型关键词`struct`](./hello-trait/examples-struct.md)
25+
- [实现关键词`impl`与方法代码实现](./hello-trait/examples-impl.md)
2626
- [方法代码实现](./hello-trait/examples-methods.md)
2727
- [衔接关键词trait与修饰关键词mut](./hello-trait/examples-trait.md)
28-
- [题外话:默认衔接特质Default](./hello-trait/examples-traitdefault.md)
28+
- [题外话:默认衔接特质Default](./hello-trait/examples-trait-default.md)
2929
- [程序代码结构](./hello-trait/lib-structure.md)
3030
- [关于软件篋项目](./hello-trait/about.md)
3131
- [共享篋:开发命令](./hello-trait/commands-lib.md)
3232
- [共享篋:程序代码解释](./hello-trait/lib-codes.md)
33-
- [共享篋:目录tests的测试代码解释](./hello-trait/lib-tests.md)
33+
- [共享篋:目录测试代码解释](./hello-trait/lib-tests.md)
3434
- [题外话](./hello-trait/off-topic.md)
3535
- [软件篋mod_trait_exerci](./hello-mod-trait/README.md)
3636
- [关于软件篋mod_trait_exerci](./hello-mod-trait/about.md)
37-
- [开发软件篋mod_trait_exerci](./hello-mod-trait/develop.md)
38-
- [使用软件篋mod_trait_exerci](./hello-mod-trait/apply.md)
37+
- [文件与模块](./hello-mod-trait/crate-structure.md)
38+
- [共享篋:程序结构和代码解释](./hello-mod-trait/lib-codes.md)
39+
- [共享篋:三种调用方式解释](./hello-mod-trait/exampels-codes.md)
40+
- [理解动态与静态调度实现](./hello-mod-trait/examples-dispatch.md)
41+
- [深度解析动态与静态调度实现](./hello-mod-trait/examples-miri.md)
42+
- [共享篋:静态调度实现](./hello-mod-trait/lib-static.md)
43+
- [共享篋:动态调度实现](./hello-mod-trait/lib-dynamic.md)
44+
- [共享篋:目录测试代码解释](./hello-mod-trait/lib-tests.md)
45+
- [题外话:泛型实例](./hello-mod-trait/off-topic.md)
3946
- [软件篋deref_exerci](./hello-deref/README.md)
4047
- [关于软件篋deref_exerci](./hello-deref/about.md)
4148
- [开发软件篋deref_exerci](./hello-deref/develop.md)

docs/zh-first-volumn/src/foreword/README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,53 @@
1515

1616
  Crate.io是Rust语言软件篋的官方仓库,负责软件篋登记、托管和存储等功能服务。
1717

18-
## 关于本书
18+
## Rust语言编程方法
19+
20+
  Rust语言是一门多编程方法的计算机语言,它包括了:
21+
22+
- 面向对象式编程
23+
- 命令式编程
24+
- 函数式编程
25+
- 声明式编程
26+
- 响应式编程
27+
- ...
28+
29+
## 关于本书内容
1930

2031
  以开发软件篋过程,学习和理解Rust语言的全新设计思路及其重要概念。
2132

22-
## 重要Rust语言链接
33+
## 关于本书读者
34+
35+
  本书面向的读者是,致在了解Rust语言概念和设计思想,同时也学习具体程序开发的方法。
36+
37+
## Rust语言重要网站
2338
- [Rust官方网站](https://www.rust-lang.org/)
2439
- [rustup官方网站](https://rustup.rs/)
2540
- [Cargo项目源代码](https://github.com/rust-lang/cargo)
2641
- [Crates s.io官方网站](https://crates.io/)
2742

28-
## 重要工具链接链接
43+
## Rust语言入门在线图书
44+
- [官方网站: The Rust Programming Language](https://doc.rust-lang.org/book/)
45+
- [官方网站: Rust by Example](https://doc.rust-lang.org/stable/rust-by-example/)
46+
- [Learning Rust With Entirely Too Many Linked Lists](https://rust-unofficial.github.io/too-many-lists/index.html)
47+
48+
## Rust语言在线图书
49+
- [官方网站: The Rustonomicon](https://doc.rust-lang.org/nightly/nomicon/)
50+
- [A Gentle Introduction To Rust](https://stevedonovan.github.io/rust-gentle-intro/readme.html#a-gentle-introduction-to-rust)
51+
- [24 days of Rust](https://zsiciarz.github.io/24daysofrust/index.html)
52+
- [Rust-101](https://www.ralfj.de/projects/rust-101/main.html)
53+
54+
## Rust语言开发参考图书
55+
- [官方网站: The Rust Standard Library](https://doc.rust-lang.org/std/index.html)
56+
- [官方网站: The Rust Reference](https://doc.rust-lang.org/reference/)
57+
58+
## 重要工具网站
2959
- [Tables Generator](https://www.tablesgenerator.com/)
3060
- [Liste der Unicode-Zeichen der Kategorie „Sonstiges Symbol“](https://www.compart.com/de/unicode/category/So)
3161
- [Sketchpad - Draw, Create, Share!](https://sketch.io/sketchpad/)
3262
- [Quality metadata badges](https://shields.io/)
3363
- [Continuous Integration and Deployment service for Windows and Linux | AppVeyor](https://www.appveyor.com/)
3464
- [Nu Shell](https://github.com/nushell/nushell)
3565
- [Markdown: Syntax](https://markdown.de/)
36-
- [category_slugs from crates.io](https://crates.io/category_slugs)
66+
- [category_slugs from crates.io](https://crates.io/category_slugs)
67+
- [Compress That Address](https://is.gd)
Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,64 @@
1-
# 第二章
1+
# 第三章 软件篋mod_trait_exerci
22

3+
## 学习内容
4+
- 学习软件篋文件、模块与程序结构
5+
- 了解和学习动态调度关键词`dyn`
6+
- 学习和理解动态与静态调度(Static vs Dynamic Dispatch)
7+
- 衔接类型关键词`trait`作用
8+
-
39

4-
[rust_testing_mocking/slides/2113/testing_in_rust_by_donald_whyte.pdf](https://archive.fosdem.org/2018/schedule/event/rust_testing_mocking/attachments/slides/2113/export/events/attachments/rust_testing_mocking/slides/2113/testing_in_rust_by_donald_whyte.pdf)
10+
rust没有继承,继承不流行了。通过trait缺省函数和覆盖来重用代码,泛型来实现多态。
11+
12+
rust也通过trait object, 特性对象来实现多态。
13+
14+
特性对象,本质上就是不限类型的,动态的特性绑定,run time匹配具体类型
15+
16+
  
17+
You can only make object-safe traits into trait objects.
18+
您只能将对象安全的特征变成特征对象。
19+
20+
Traits that do not resolve to concrete type of implementation.
21+
不能解决具体实现类型的特征。
22+
23+
In practice two rules govern if a trait is object-safe.
24+
实际上,有两个规则控制特征是否是对象安全的。
25+
26+
The return type isn’t Self.
27+
There are no generic type parameters.
28+
返回类型不是Self。
29+
没有通用类型参数。
30+
31+
32+
Any trait satisfying these two rules can be used as trait objects.
33+
任何东西都可以用作特质对象。
34+
35+
Example of trait that is object-safe can be used as trait object:
36+
可以将对象安全的特征示例用作特征对象:
37+
38+
trait Draw {
39+
fn draw(&self);
40+
}
41+
42+
Example of trait that cannot be used as trait object:
43+
不能用作特征对象的特征示例:
44+
45+
trait Draw {
46+
fn draw(&self) -> Self;
47+
}
48+
49+
## 参考资料
50+
- [rust_testing_mocking/slides/2113/testing_in_rust_by_donald_whyte.pdf](https://archive.fosdem.org/2018/schedule/event/rust_testing_mocking/attachments/slides/2113/export/events/attachments/rust_testing_mocking/slides/2113/testing_in_rust_by_donald_whyte.pdf)
51+
- [std keyword dyn](https://doc.rust-lang.org/std/keyword.dyn.html)
52+
- [dyn-trait-for-trait-objects](https://doc.rust-lang.org/edition-guide/rust-2018/trait-system/dyn-trait-for-trait-objects.html)
53+
- [rust-traits-and-trait-objects](https://joshleeb.com/posts/rust-traits-and-trait-objects/)
54+
- [a_quick_look_at_trait_objects_in_rust](https://tratt.net/laurie/blog/entries/a_quick_look_at_trait_objects_in_rust.html)
55+
- [Code](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5d573e667aac08f960557b79d6385f6e)
56+
- [what-makes-something-a-trait-object](https://stackoverflow.com/questions/27567849/what-makes-something-a-trait-object)
57+
- [Dynamic_dispatch](https://en.wikipedia.org/wiki/Dynamic_dispatch)
58+
- [reference identifiers](https://doc.rust-lang.org/reference/identifiers.html)
59+
- [rust-traits-and-trait-objects](https://joshleeb.com/posts/rust-traits-and-trait-objects/)
60+
- [traits-on-generics](https://blog.theenginerd.com/blog/2015/06/27/traits-on-generics/)
61+
- [learning-generics-in-rust](https://tutorialedge.net/rust/learning-generics-in-rust/)
62+
- [generic-returns-in-rust](https://blog.jcoglan.com/2019/04/22/generic-returns-in-rust/)
63+
- [writing-a-hashmap-to-struct-procedural-macro-in-rust](https://cprimozic.net/blog/writing-a-hashmap-to-struct-procedural-macro-in-rust/)
64+
- [impls_and_traits](https://learning-rust.github.io/docs/b5.impls_and_traits.html)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
11
# 关于软件篋mod_trait_exerci
2+
3+
## 学习内容
4+
- 了解项目名称和目录
5+
6+
## 篇目
7+
8+
1. [项目名称清单](#项目名称清单)
9+
1. [软件篋类型清单](#软件篋类型清单)
10+
1. [项目目录文件结构](#项目目录文件结构)
11+
12+
## 项目名称清单
13+
14+
| 项目类型 | 项目名称 | 相对路径 | 项目说明 |
15+
|---|---|---|---|
16+
| 作业区 | **hello-mod-trait** | ./hello-mod-trait | 开发共享软件篋工作区 |
17+
| 共享篋 | lib-hello | ./hello-mod-trait/lib-hello | 开发共享软件篋实例 |
18+
| 本地程序 | bin-local-hello | ./hello-mod-trait/bin-local-hello | 使用在本地的共享篋 |
19+
| 仓库程序 | bin-hello | ./hello-mod-trait/bin-hello | 使用在crates.io上共享篋 |
20+
21+
## 软件篋类型清单
22+
23+
| 篋类型 | 篋名称 | 相对路径 |
24+
|---|---|---|
25+
| 共享软件篋 | **mod_trait_exerci** | ./hello-mod-trait/lib-hello |
26+
| 可执行程序 | bin-local-hello | ./hello-mod-trait/bin-local-hello |
27+
| 可执行程序 | bin-hello | ./hello-mod-trait/bin-hello |
28+
29+
## 项目目录文件结构
30+
31+
```bash
32+
$ tree -L 2
33+
.
34+
├── Cargo.lock
35+
├── Cargo.toml
36+
├── README.md
37+
├── examples
38+
│ ├── trait_dispatch_abstract.rs
39+
│ ├── trait_dispatch_concrete.rs
40+
│ └── trait_where_hello.rs
41+
└── src
42+
├── lib.rs
43+
├── mod_bare
44+
├── mod_deref.rs
45+
├── mod_deref_fn.rs
46+
├── mod_dynamic_fn.rs
47+
├── mod_generics.rs
48+
├── mod_generics_fn.rs
49+
├── mod_static_fn.rs
50+
└── mod_where_fn.rs
51+
```

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

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 文件与模块
2+
3+
  在这一节里,学习Cargo项目文件、目录与模块相互关系。Rust语言表达模块的三种方式。
4+
5+
## 学习内容
6+
- 了解和学习本软件篋模块文件结构
7+
- 理解和掌握Cargo项目目录与文件关系
8+
- 理解和掌握Cargo项目模块与文件关系
9+
- 理解和掌握Cargo项目文件与文件关系
10+
11+
## 篇目
12+
1. [文件本身表达模块方式](#文件本身表达模块方式)
13+
1. [文件名称表达模块方式](#文件名称表达模块方式)
14+
1. [目录名称表达模块方式](#目录名称表达模块方式)
15+
1. [参考资料](#参考资料)
16+
17+
![image](../../images/hello_mod_trait_00_structure.png)
18+
19+
## 文件本身表达模块方式
20+
21+
  Ⓓ 使用模块关键词`mod`和代码块`{}`的表达式,在Rust语言程序文件里,可以定义任何一个或者多个不同名称的模块。
22+
23+
  在文件lib.rs里,创建了称之为`mod_trait`模块,该模块实现代码也在该文件里。
24+
25+
## 文件名称表达模块方式
26+
27+
  Ⓓ 使用模块关键词`mod`语句,在Rust语言程序文件里,可以定义任何一个或者多个不同的模块。
28+
29+
  使用关键词`mod`语句,实现模块代码存在两种形式:以文件名称作为模块名称方式和以目录名称为模块名称方式。这里先解释前面一种情况,如程序文件`mod_generics.rs`
30+
31+
  实现以文件名称作为模块名称具体方法是,在文件`lib.rs`里,使用语句`pub mod mod_generics;`,且在与文件`lib.rs`相同的目录下,创建名称为`mod_generics.rs`模块程序文件,为了其自身模块,该文件不需要使用模块关键词了。
32+
33+
## 目录名称表达模块方式
34+
35+
  程序文件`mod.rs`是第三种表达模块方式,即以目录名称为模块名称方式。
36+
37+
  在程序文件`lib.rs`里,使用语句`pub mod mod_bare;`,说明了该模块是外部文件实现模块代码,但是从中微分确认其实现方式。我们看到在与文件`lib.rs`相同目录下存在目录`mod_bare`,说明了该实现在该目录下的文件,默认情况下就是程序文件`mod.rs`,所有模块目录的入口文件默认情况下都是该文件名称。
38+
39+
## 参考资料
40+
- [how-to-use-one-module-from-another-module-in-a-rust-cargo-project](https://stackoverflow.com/questions/48071513/how-to-use-one-module-from-another-module-in-a-rust-cargo-project)
41+
- [modules-in-rust-programming-language](https://dev.to/saiumesh/modules-in-rust-programming-language-495m)
42+
- [rust-gentle-intro/modules](https://stevedonovan.github.io/rust-gentle-intro/4-modules.html)
43+
- [ch07-01-mod-and-the-filesystem](https://doc.rust-lang.org/1.29.2/book/2018-edition/ch07-01-mod-and-the-filesystem.html)
44+
- [how-to-use-one-module-from-another-module-in-a-rust-cargo-project](https://stackoverflow.com/questions/48071513/how-to-use-one-module-from-another-module-in-a-rust-cargo-project)

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

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 共享篋:三种调用方式解释
2+
3+
  在本节里,通过两个模块`mod_bare``mod_trait`实现及其三种调用方式比较,详细说明衔接关键词`trait`作用。
4+
5+
## 学习内容
6+
- 理解衔接关键词`trait`的重要性
7+
- 掌握衔接关键词`trait`实现的应用方法
8+
9+
## 篇目
10+
1. [基于结构类型的实现:使用实例调用](#基于结构类型的实现:使用实例调用)
11+
1. [基于衔接特质的实现:使用实例调用](#基于衔接特质的实现:使用实例调用)
12+
1. [基于衔接特质的模块](#基于衔接特质的模块)
13+
1. [基于衔接特质的实现:作为参数调用](#基于衔接特质的实现:作为参数调用)
14+
1. [题外话](#题外话)
15+
1. [浅说面向对象编程、命令式编程和声明式编程](#浅说面向对象编程、命令式编程和声明式编程)
16+
1. [数据类型数组](#数据类型数组)
17+
2. [泛型函数与方法](#泛型函数与方法)
18+
3. [类型范围关键词`where`](#类型范围关键词where)
19+
4. [参考资料](#参考资料)
20+
21+
![image](../../images/hello_mod_trait_04_apply_ways.png)
22+
23+
## 基于结构类型的实现:使用实例调用
24+
25+
  从上面图示,可以了解到下面程序文件直接访问模块`mod_bare`,该模块是基于结构类型的实现,不存在衔接特质。
26+
27+
{{#playpen ../../../../hello-mod-trait/lib-hello/examples/bare_hello.rs editable}}
28+
29+
## 基于衔接特质的实现:使用实例调用
30+
31+
  从上面图示,下面程序文件也是直接访问模块`mod_trait`本身,与上面应用实例不同,这里通过衔接特质方式实现代码,从代码的关键词`use`语句也可以了解到这一点。
32+
33+
  比较上面程序文件`bare_hello.rs`和下面文件`trait_instance_hello.rs`的关键词`use`语句,可以看到,上面程序文件里的关键词`use`语句访问模块,采用了绝对路径方式,而下面文件既有绝对路径方式,也有相对路径方式。
34+
35+
{{#playpen ../../../../hello-mod-trait/lib-hello/examples/trait_instance_hello.rs editable}}
36+
37+
## 基于衔接特质的模块
38+
39+
  从上面图示,可以了解到,下面程序文件也是一个模块`mod_where_fn`,该模块应用了模块`mod_trait`的功能,注意该文件关键词`use`语句只使用了衔接特质`TraitCanal`,这也是一种访问其父模块的相对路径方式。它的存在意义是什么?这是为用户提供更加灵活的实现而设计的。这里提供了怎么样使用模块,而不必用户完成实现。
40+
41+
  下面代码的方法是一种泛型方法的表达方式,其概念参考下面说明。
42+
43+
  下面将会说明关键词`where`。在文件`mod_where_fn.rs`里的两个函数是两个完全相同的实现,只是语法表达方式不同而已。
44+
45+
{{#playpen ../../../../hello-mod-trait/lib-hello/src/mod_where_fn.rs editable}}
46+
47+
## 基于衔接特质的实现:作为参数调用
48+
49+
  从上面图示,可以了解到下面程序文件并不是直接访问模块`mod_trait`本身,而是访问中间模块`mod_where_fn`完成的,这是一种更加方便和灵活的三层程序设计结构。
50+
51+
  无论是什么类型结构,为了获取不同的结构类型属性,都不需要访问其属性名称。
52+
53+
  在使用模块`mod_where_fn`的函数时,结构类型的实例是作为参数传递给该模块的函数。我们仅仅告诉它我们是‘谁’,我们需要做什么。
54+
55+
  模块`mod_trait`告诉我们是什么功能,这好比是产品生产原料。模块`mod_where_fn`解决了怎么做问题,这好比是为用户预先打造的特定产品,当然用户也可以自己制造自己需要的产品。这里程序文件说明了要做什么事情,这好比作为用户使用现成的产品。
56+
57+
{{#playpen ../../../../hello-mod-trait/lib-hello/examples/trait_where_hello.rs editable}}
58+
59+
## 题外话
60+
### 浅说面向对象编程、命令式编程和声明式编程
61+
62+
  面向对象编程回答了是什么,命令式编程回答了怎么做,而声明式编程回答了做什么。
63+
64+
### 数据类型数组
65+
66+
  在Rust语言里,数组是一种基本数据类型。
67+
68+
## 泛型函数与方法
69+
70+
  在类型理论中,泛型称之为参数多态(parametric polymorphism),对于给定参数(parametric)能够有多种形式(poly是多,morph是形态)的函数或类型。
71+
72+
  在Rust语言里,泛型是一种非常广泛采用的技术,不仅应用函数与方法关键词`fn`,也应用衔接特质关键词`trait`等等。其目的是,减少代码重复。
73+
74+
### 类型范围关键词`where`
75+
76+
  关键词`where`用于向泛型类型添加约束,并为编译器提供解决问题所需的信息!
77+
78+
## 参考资料
79+
- [understanding-where-clauses](https://mgattozzi.github.io/2016/09/13/understanding-where-clauses.html)
80+

0 commit comments

Comments
 (0)