Skip to content

Commit e0a15ae

Browse files
committed
trait_exerci = "0.3.0"
1 parent 2ad2af4 commit e0a15ae

File tree

16 files changed

+171
-151
lines changed

16 files changed

+171
-151
lines changed

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

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,6 @@
55
- 了解和学习动态调度关键词`dyn`
66
- 学习和理解动态与静态调度(Static vs Dynamic Dispatch)
77
- 衔接类型关键词`trait`作用
8-
-
9-
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-
}
488

499
## 参考资料
5010
- [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)

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

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,56 @@
2929
## 项目目录文件结构
3030

3131
```bash
32-
$ tree -L 2
32+
$ tree -L 3
3333
.
3434
├── Cargo.lock
3535
├── Cargo.toml
3636
├── 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
37+
├── bin-hello
38+
│ ├── Cargo.lock
39+
│ ├── Cargo.toml
40+
│ ├── src
41+
│ │ ├── bin
42+
│ │ └── main.rs
43+
│ └── target
44+
│ └── debug
45+
├── bin-local-hello
46+
│ ├── Cargo.lock
47+
│ ├── Cargo.toml
48+
│ ├── src
49+
│ │ └── main.rs
50+
│ ├── target
51+
│ │ └── debug
52+
│ └── tests
53+
│ └── type_both_hello.rs
54+
└── lib-hello
55+
├── Cargo.lock
56+
├── Cargo.toml
57+
├── README.md
58+
├── examples
59+
│ ├── bare_hello.rs
60+
│ ├── box_dynamic_hello.rs
61+
│ ├── box_static_hello.rs
62+
│ ├── generics_fn_hello.rs
63+
│ ├── generics_impl_hello.rs
64+
│ ├── generics_trait_hello.rs
65+
│ ├── generics_type_hello.rs
66+
│ ├── simple_dynamic_dispatch.rs
67+
│ ├── simple_static_dispatch.rs
68+
│ ├── trait_dispatch_abstract.rs
69+
│ ├── trait_dispatch_concrete.rs
70+
│ ├── trait_fn_hello.rs
71+
│ ├── trait_instance_hello.rs
72+
│ └── trait_where_hello.rs
73+
├── src
74+
│ ├── lib.rs
75+
│ ├── mod_bare
76+
│ ├── mod_dynamic_fn.rs
77+
│ ├── mod_static_fn.rs
78+
│ └── mod_where_fn.rs
79+
└── tests
80+
├── box_dynamic_hello.rs
81+
├── box_static_hello.rs
82+
├── mod_bare.rs
83+
└── mod_trait.rs
5184
```

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@
4141

4242
  这里将说明基于衔接特质关键性`trait`的静态和动态函数实现,但是这静态函数与之前的也是完全不一样的概念。另外将会看到调用这种动态函数,它们看起来是一些更复杂的静态和动态函数。
4343

44-
   下面程序第二段代码的两个函数`static_dispatch()``dynamic_dispatch()`,它们的目的是解决代码重复的相同问题。但是其手段是不同的,静态函数`static_dispatch()`使用了泛型编程方法,关于泛型编程将有另外专题说明。而动态函数`dynamic_dispatch()`使用了动态编程方法。从代码上看,它们达到相同的功能。
44+
  下面程序第二段代码的两个函数`static_dispatch()``dynamic_dispatch()`,它们的目的是解决代码重复的相同问题。但是其手段是不同的。
45+
46+
  Rust语言没有继承概念,继承编程不再是软件开发的思想。通过关键词`trait`定义函数,借助于关键词impl实现函数及其泛型编程方法,以实现多态式编程方法。静态函数`static_dispatch()`使用了泛型编程方法,关于泛型编程将有另外专题说明。
47+
48+
  Rust语言也通过衔接特质对象及动态调度编程方法,来实现多态式编程方法。动态函数`dynamic_dispatch()`使用了动态编程方法。特性对象是不限类型的,动态绑定类型是通过实时运行时具体地匹配类型。
49+
50+
  从代码上看,静态函数`static_dispatch()`和动态函数`dynamic_dispatch()`都是实现相同的功能。
4551

4652
  下面程序第三段代码里,无论是类型`NormalStruct`,还是类型`TupleStruct`,它们都是以同一方式分别调用函数`static_dispatch()`和函数`dynamic_dispatch()`
4753

docs/zh-volumn-one/hello-mod-trait/about.html

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,25 +173,58 @@ <h2><a class="header" href="#软件篋类型清单" id="软件篋类型清单">
173173
<tr><td>可执行程序</td><td>bin-hello</td><td>./hello-mod-trait/bin-hello</td></tr>
174174
</tbody></table>
175175
<h2><a class="header" href="#项目目录文件结构" id="项目目录文件结构">项目目录文件结构</a></h2>
176-
<pre><code class="language-bash">$ tree -L 2
176+
<pre><code class="language-bash">$ tree -L 3
177177
.
178178
├── Cargo.lock
179179
├── Cargo.toml
180180
├── README.md
181-
├── examples
182-
│ ├── trait_dispatch_abstract.rs
183-
│ ├── trait_dispatch_concrete.rs
184-
│ └── trait_where_hello.rs
185-
└── src
186-
├── lib.rs
187-
├── mod_bare
188-
├── mod_deref.rs
189-
├── mod_deref_fn.rs
190-
├── mod_dynamic_fn.rs
191-
├── mod_generics.rs
192-
├── mod_generics_fn.rs
193-
├── mod_static_fn.rs
194-
└── mod_where_fn.rs
181+
├── bin-hello
182+
│ ├── Cargo.lock
183+
│ ├── Cargo.toml
184+
│ ├── src
185+
│ │ ├── bin
186+
│ │ └── main.rs
187+
│ └── target
188+
│ └── debug
189+
├── bin-local-hello
190+
│ ├── Cargo.lock
191+
│ ├── Cargo.toml
192+
│ ├── src
193+
│ │ └── main.rs
194+
│ ├── target
195+
│ │ └── debug
196+
│ └── tests
197+
│ └── type_both_hello.rs
198+
└── lib-hello
199+
├── Cargo.lock
200+
├── Cargo.toml
201+
├── README.md
202+
├── examples
203+
│ ├── bare_hello.rs
204+
│ ├── box_dynamic_hello.rs
205+
│ ├── box_static_hello.rs
206+
│ ├── generics_fn_hello.rs
207+
│ ├── generics_impl_hello.rs
208+
│ ├── generics_trait_hello.rs
209+
│ ├── generics_type_hello.rs
210+
│ ├── simple_dynamic_dispatch.rs
211+
│ ├── simple_static_dispatch.rs
212+
│ ├── trait_dispatch_abstract.rs
213+
│ ├── trait_dispatch_concrete.rs
214+
│ ├── trait_fn_hello.rs
215+
│ ├── trait_instance_hello.rs
216+
│ └── trait_where_hello.rs
217+
├── src
218+
│ ├── lib.rs
219+
│ ├── mod_bare
220+
│ ├── mod_dynamic_fn.rs
221+
│ ├── mod_static_fn.rs
222+
│ └── mod_where_fn.rs
223+
└── tests
224+
├── box_dynamic_hello.rs
225+
├── box_static_hello.rs
226+
├── mod_bare.rs
227+
└── mod_trait.rs
195228
</code></pre>
196229

197230
</main>

docs/zh-volumn-one/hello-mod-trait/examples-dispatch.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ <h2><a class="header" href="#静态函数的动态调度实例" id="静态函数
230230
</code></pre></pre>
231231
<h2><a class="header" href="#调用动态函数的实例" id="调用动态函数的实例">调用动态函数的实例</a></h2>
232232
<p>  这里将说明基于衔接特质关键性<code>trait</code>的静态和动态函数实现,但是这静态函数与之前的也是完全不一样的概念。另外将会看到调用这种动态函数,它们看起来是一些更复杂的静态和动态函数。</p>
233-
<p>   下面程序第二段代码的两个函数<code>static_dispatch()</code><code>dynamic_dispatch()</code>,它们的目的是解决代码重复的相同问题。但是其手段是不同的,静态函数<code>static_dispatch()</code>使用了泛型编程方法,关于泛型编程将有另外专题说明。而动态函数<code>dynamic_dispatch()</code>使用了动态编程方法。从代码上看,它们达到相同的功能。</p>
233+
<p>  下面程序第二段代码的两个函数<code>static_dispatch()</code><code>dynamic_dispatch()</code>,它们的目的是解决代码重复的相同问题。但是其手段是不同的。</p>
234+
<p>  Rust语言没有继承概念,继承编程不再是软件开发的思想。通过关键词<code>trait</code>定义函数,借助于关键词impl实现函数及其泛型编程方法,以实现多态式编程方法。静态函数<code>static_dispatch()</code>使用了泛型编程方法,关于泛型编程将有另外专题说明。</p>
235+
<p>  Rust语言也通过衔接特质对象及动态调度编程方法,来实现多态式编程方法。动态函数<code>dynamic_dispatch()</code>使用了动态编程方法。特性对象是不限类型的,动态绑定类型是通过实时运行时具体地匹配类型。</p>
236+
<p>  从代码上看,静态函数<code>static_dispatch()</code>和动态函数<code>dynamic_dispatch()</code>都是实现相同的功能。</p>
234237
<p>  下面程序第三段代码里,无论是类型<code>NormalStruct</code>,还是类型<code>TupleStruct</code>,它们都是以同一方式分别调用函数<code>static_dispatch()</code>和函数<code>dynamic_dispatch()</code></p>
235238
<p>  不管是静态函数,还是动态函数,它们都是基于衔接特质对象的指针实现,这一点是非常重要的。</p>
236239
<p>  下面程序是Rust语言非常经典的代码结构。</p>

docs/zh-volumn-one/hello-mod-trait/index.html

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -155,34 +155,7 @@ <h2><a class="header" href="#学习内容" id="学习内容">学习内容</a></h
155155
<li>了解和学习动态调度关键词<code>dyn</code></li>
156156
<li>学习和理解动态与静态调度(Static vs Dynamic Dispatch)</li>
157157
<li>衔接类型关键词<code>trait</code>作用</li>
158-
<li></li>
159158
</ul>
160-
<p>rust没有继承,继承不流行了。通过trait缺省函数和覆盖来重用代码,泛型来实现多态。</p>
161-
<p>rust也通过trait object, 特性对象来实现多态。</p>
162-
<p>特性对象,本质上就是不限类型的,动态的特性绑定,run time匹配具体类型</p>
163-
<p>  
164-
You can only make object-safe traits into trait objects.
165-
您只能将对象安全的特征变成特征对象。</p>
166-
<p>Traits that do not resolve to concrete type of implementation.
167-
不能解决具体实现类型的特征。</p>
168-
<p>In practice two rules govern if a trait is object-safe.
169-
实际上,有两个规则控制特征是否是对象安全的。</p>
170-
<p>The return type isn’t Self.
171-
There are no generic type parameters.
172-
返回类型不是Self。
173-
没有通用类型参数。</p>
174-
<p>Any trait satisfying these two rules can be used as trait objects.
175-
任何东西都可以用作特质对象。</p>
176-
<p>Example of trait that is object-safe can be used as trait object:
177-
可以将对象安全的特征示例用作特征对象:</p>
178-
<p>trait Draw {
179-
fn draw(&amp;self);
180-
}</p>
181-
<p>Example of trait that cannot be used as trait object:
182-
不能用作特征对象的特征示例:</p>
183-
<p>trait Draw {
184-
fn draw(&amp;self) -&gt; Self;
185-
}</p>
186159
<h2><a class="header" href="#参考资料" id="参考资料">参考资料</a></h2>
187160
<ul>
188161
<li><a href="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">rust_testing_mocking/slides/2113/testing_in_rust_by_donald_whyte.pdf</a></li>

docs/zh-volumn-one/print.html

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2322,34 +2322,7 @@ <h2><a class="header" href="#学习内容-17" id="学习内容-17">学习内容<
23222322
<li>了解和学习动态调度关键词<code>dyn</code></li>
23232323
<li>学习和理解动态与静态调度(Static vs Dynamic Dispatch)</li>
23242324
<li>衔接类型关键词<code>trait</code>作用</li>
2325-
<li></li>
23262325
</ul>
2327-
<p>rust没有继承,继承不流行了。通过trait缺省函数和覆盖来重用代码,泛型来实现多态。</p>
2328-
<p>rust也通过trait object, 特性对象来实现多态。</p>
2329-
<p>特性对象,本质上就是不限类型的,动态的特性绑定,run time匹配具体类型</p>
2330-
<p>  
2331-
You can only make object-safe traits into trait objects.
2332-
您只能将对象安全的特征变成特征对象。</p>
2333-
<p>Traits that do not resolve to concrete type of implementation.
2334-
不能解决具体实现类型的特征。</p>
2335-
<p>In practice two rules govern if a trait is object-safe.
2336-
实际上,有两个规则控制特征是否是对象安全的。</p>
2337-
<p>The return type isn’t Self.
2338-
There are no generic type parameters.
2339-
返回类型不是Self。
2340-
没有通用类型参数。</p>
2341-
<p>Any trait satisfying these two rules can be used as trait objects.
2342-
任何东西都可以用作特质对象。</p>
2343-
<p>Example of trait that is object-safe can be used as trait object:
2344-
可以将对象安全的特征示例用作特征对象:</p>
2345-
<p>trait Draw {
2346-
fn draw(&amp;self);
2347-
}</p>
2348-
<p>Example of trait that cannot be used as trait object:
2349-
不能用作特征对象的特征示例:</p>
2350-
<p>trait Draw {
2351-
fn draw(&amp;self) -&gt; Self;
2352-
}</p>
23532326
<h2><a class="header" href="#参考资料-16" id="参考资料-16">参考资料</a></h2>
23542327
<ul>
23552328
<li><a href="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">rust_testing_mocking/slides/2113/testing_in_rust_by_donald_whyte.pdf</a></li>
@@ -2393,25 +2366,58 @@ <h2><a class="header" href="#软件篋类型清单-2" id="软件篋类型清单-
23932366
<tr><td>可执行程序</td><td>bin-hello</td><td>./hello-mod-trait/bin-hello</td></tr>
23942367
</tbody></table>
23952368
<h2><a class="header" href="#项目目录文件结构" id="项目目录文件结构">项目目录文件结构</a></h2>
2396-
<pre><code class="language-bash">$ tree -L 2
2369+
<pre><code class="language-bash">$ tree -L 3
23972370
.
23982371
├── Cargo.lock
23992372
├── Cargo.toml
24002373
├── README.md
2401-
├── examples
2402-
│ ├── trait_dispatch_abstract.rs
2403-
│ ├── trait_dispatch_concrete.rs
2404-
│ └── trait_where_hello.rs
2405-
└── src
2406-
├── lib.rs
2407-
├── mod_bare
2408-
├── mod_deref.rs
2409-
├── mod_deref_fn.rs
2410-
├── mod_dynamic_fn.rs
2411-
├── mod_generics.rs
2412-
├── mod_generics_fn.rs
2413-
├── mod_static_fn.rs
2414-
└── mod_where_fn.rs
2374+
├── bin-hello
2375+
│ ├── Cargo.lock
2376+
│ ├── Cargo.toml
2377+
│ ├── src
2378+
│ │ ├── bin
2379+
│ │ └── main.rs
2380+
│ └── target
2381+
│ └── debug
2382+
├── bin-local-hello
2383+
│ ├── Cargo.lock
2384+
│ ├── Cargo.toml
2385+
│ ├── src
2386+
│ │ └── main.rs
2387+
│ ├── target
2388+
│ │ └── debug
2389+
│ └── tests
2390+
│ └── type_both_hello.rs
2391+
└── lib-hello
2392+
├── Cargo.lock
2393+
├── Cargo.toml
2394+
├── README.md
2395+
├── examples
2396+
│ ├── bare_hello.rs
2397+
│ ├── box_dynamic_hello.rs
2398+
│ ├── box_static_hello.rs
2399+
│ ├── generics_fn_hello.rs
2400+
│ ├── generics_impl_hello.rs
2401+
│ ├── generics_trait_hello.rs
2402+
│ ├── generics_type_hello.rs
2403+
│ ├── simple_dynamic_dispatch.rs
2404+
│ ├── simple_static_dispatch.rs
2405+
│ ├── trait_dispatch_abstract.rs
2406+
│ ├── trait_dispatch_concrete.rs
2407+
│ ├── trait_fn_hello.rs
2408+
│ ├── trait_instance_hello.rs
2409+
│ └── trait_where_hello.rs
2410+
├── src
2411+
│ ├── lib.rs
2412+
│ ├── mod_bare
2413+
│ ├── mod_dynamic_fn.rs
2414+
│ ├── mod_static_fn.rs
2415+
│ └── mod_where_fn.rs
2416+
└── tests
2417+
├── box_dynamic_hello.rs
2418+
├── box_static_hello.rs
2419+
├── mod_bare.rs
2420+
└── mod_trait.rs
24152421
</code></pre>
24162422
<h1><a class="header" href="#文件与模块" id="文件与模块">文件与模块</a></h1>
24172423
<p>  在这一节里,学习Cargo项目文件、目录与模块相互关系。Rust语言表达模块的三种方式。</p>
@@ -2760,7 +2766,10 @@ <h2><a class="header" href="#静态函数的动态调度实例" id="静态函数
27602766
</code></pre></pre>
27612767
<h2><a class="header" href="#调用动态函数的实例" id="调用动态函数的实例">调用动态函数的实例</a></h2>
27622768
<p>  这里将说明基于衔接特质关键性<code>trait</code>的静态和动态函数实现,但是这静态函数与之前的也是完全不一样的概念。另外将会看到调用这种动态函数,它们看起来是一些更复杂的静态和动态函数。</p>
2763-
<p>   下面程序第二段代码的两个函数<code>static_dispatch()</code><code>dynamic_dispatch()</code>,它们的目的是解决代码重复的相同问题。但是其手段是不同的,静态函数<code>static_dispatch()</code>使用了泛型编程方法,关于泛型编程将有另外专题说明。而动态函数<code>dynamic_dispatch()</code>使用了动态编程方法。从代码上看,它们达到相同的功能。</p>
2769+
<p>  下面程序第二段代码的两个函数<code>static_dispatch()</code><code>dynamic_dispatch()</code>,它们的目的是解决代码重复的相同问题。但是其手段是不同的。</p>
2770+
<p>  Rust语言没有继承概念,继承编程不再是软件开发的思想。通过关键词<code>trait</code>定义函数,借助于关键词impl实现函数及其泛型编程方法,以实现多态式编程方法。静态函数<code>static_dispatch()</code>使用了泛型编程方法,关于泛型编程将有另外专题说明。</p>
2771+
<p>  Rust语言也通过衔接特质对象及动态调度编程方法,来实现多态式编程方法。动态函数<code>dynamic_dispatch()</code>使用了动态编程方法。特性对象是不限类型的,动态绑定类型是通过实时运行时具体地匹配类型。</p>
2772+
<p>  从代码上看,静态函数<code>static_dispatch()</code>和动态函数<code>dynamic_dispatch()</code>都是实现相同的功能。</p>
27642773
<p>  下面程序第三段代码里,无论是类型<code>NormalStruct</code>,还是类型<code>TupleStruct</code>,它们都是以同一方式分别调用函数<code>static_dispatch()</code>和函数<code>dynamic_dispatch()</code></p>
27652774
<p>  不管是静态函数,还是动态函数,它们都是基于衔接特质对象的指针实现,这一点是非常重要的。</p>
27662775
<p>  下面程序是Rust语言非常经典的代码结构。</p>

docs/zh-volumn-one/searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/zh-volumn-one/searchindex.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

hello-mod-trait/Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)