|
1 |
| -======================== |
2 |
| -Exercise: Builder Type |
3 |
| -======================== |
4 |
| - |
5 |
| ------------------------- |
6 |
| -Builder Type Setup |
7 |
| ------------------------- |
8 |
| - |
9 |
| -In this example, we will implement a complex data type that owns all of |
10 |
| -its data. We will use the *builder pattern* to support building a new |
11 |
| -value piece-by-piece, using convenience functions. |
12 |
| - |
13 |
| -.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-package :end-before://ANCHOR-package_solution :code:rust |
14 |
| - |
15 |
| -.. code:: Rust |
16 |
| -
|
17 |
| - impl Package { |
18 |
| - /// Return a representation of this package as a dependency, for use in |
19 |
| - /// building other packages. |
20 |
| - fn as_dependency(&self) -> Dependency { |
21 |
| - todo!("1") |
22 |
| - } |
23 |
| - } |
24 |
| -
|
25 |
| -.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-builder :end-before://ANCHOR-builder_solution :code:rust |
26 |
| - |
27 |
| ------------------------- |
28 |
| -Builder Type Problem |
29 |
| ------------------------- |
30 |
| - |
31 |
| -Fill in the missing pieces. |
32 |
| - |
33 |
| -.. code:: rust |
34 |
| -
|
35 |
| - impl PackageBuilder { |
36 |
| - fn new(name: impl Into<String>) -> Self { |
37 |
| - todo!("2") |
38 |
| - } |
39 |
| -
|
40 |
| - /// Set the package version. |
41 |
| - fn version(mut self, version: impl Into<String>) -> Self { |
42 |
| - self.0.version = version.into(); |
43 |
| - self |
44 |
| - } |
45 |
| -
|
46 |
| - /// Set the package authors. |
47 |
| - fn authors(mut self, authors: Vec<String>) -> Self { |
48 |
| - todo!("3") |
49 |
| - } |
50 |
| -
|
51 |
| - /// Add an additional dependency. |
52 |
| - fn dependency(mut self, dependency: Dependency) -> Self { |
53 |
| - todo!("4") |
54 |
| - } |
55 |
| -
|
56 |
| - /// Set the language. If not set, language defaults to None. |
57 |
| - fn language(mut self, language: Language) -> Self { |
58 |
| - todo!("5") |
59 |
| - } |
60 |
| -
|
61 |
| - fn build(self) -> Package { |
62 |
| - self.0 |
63 |
| - } |
64 |
| - } |
65 |
| -
|
66 |
| ------------------------- |
67 |
| -Builder Type Main |
68 |
| ------------------------- |
69 |
| - |
70 |
| -.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-main :code:rust |
71 |
| - |
72 |
| ------------------------- |
73 |
| -Builder Type Solution |
74 |
| ------------------------- |
75 |
| - |
76 |
| -.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-package_solution :end-before://ANCHOR-builder :code:rust |
77 |
| - |
78 |
| -.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-builder_solution :end-before://ANCHOR-main :code:rust |
| 1 | +======================== |
| 2 | +Exercise: Builder Type |
| 3 | +======================== |
| 4 | + |
| 5 | +------------------------ |
| 6 | +Builder Type Setup |
| 7 | +------------------------ |
| 8 | + |
| 9 | +In this example, we will implement a complex data type that owns all of |
| 10 | +its data. We will use the *builder pattern* to support building a new |
| 11 | +value piece-by-piece, using convenience functions. |
| 12 | + |
| 13 | +.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-package :end-before://ANCHOR-package_solution :code:rust |
| 14 | + |
| 15 | +.. code:: Rust |
| 16 | +
|
| 17 | + impl Package { |
| 18 | + /// Return a representation of this package as a dependency, for use in |
| 19 | + /// building other packages. |
| 20 | + fn as_dependency(&self) -> Dependency { |
| 21 | + todo!("1") |
| 22 | + } |
| 23 | + } |
| 24 | +
|
| 25 | +.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-builder :end-before://ANCHOR-builder_solution :code:rust |
| 26 | + |
| 27 | +------------------------ |
| 28 | +Builder Type Problem |
| 29 | +------------------------ |
| 30 | + |
| 31 | +Fill in the missing pieces. |
| 32 | + |
| 33 | +.. code:: rust |
| 34 | +
|
| 35 | + impl PackageBuilder { |
| 36 | + fn new(name: impl Into<String>) -> Self { |
| 37 | + todo!("2") |
| 38 | + } |
| 39 | +
|
| 40 | + /// Set the package version. |
| 41 | + fn version(mut self, version: impl Into<String>) -> Self { |
| 42 | + self.0.version = version.into(); |
| 43 | + self |
| 44 | + } |
| 45 | +
|
| 46 | + /// Set the package authors. |
| 47 | + fn authors(mut self, authors: Vec<String>) -> Self { |
| 48 | + todo!("3") |
| 49 | + } |
| 50 | +
|
| 51 | + /// Add an additional dependency. |
| 52 | + fn dependency(mut self, dependency: Dependency) -> Self { |
| 53 | + todo!("4") |
| 54 | + } |
| 55 | +
|
| 56 | + /// Set the language. If not set, language defaults to None. |
| 57 | + fn language(mut self, language: Language) -> Self { |
| 58 | + todo!("5") |
| 59 | + } |
| 60 | +
|
| 61 | + fn build(self) -> Package { |
| 62 | + self.0 |
| 63 | + } |
| 64 | + } |
| 65 | +
|
| 66 | +------------------------ |
| 67 | +Builder Type Main |
| 68 | +------------------------ |
| 69 | + |
| 70 | +.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-main :code:rust |
| 71 | + |
| 72 | +------------------------ |
| 73 | +Builder Type Solution |
| 74 | +------------------------ |
| 75 | + |
| 76 | +.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-package_solution :end-before://ANCHOR-builder :code:rust |
| 77 | + |
| 78 | +.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-builder_solution :end-before://ANCHOR-main :code:rust |
0 commit comments