-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcd-kickoff-structs.html
118 lines (109 loc) · 8.8 KB
/
cd-kickoff-structs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title></title>
<style type="text/css">code{white-space: pre;}</style>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<link rel="stylesheet" href="exercise-style.css">
</head>
<body>
<h1 id="structs">Structs</h1>
<p>(Time: 10 minutes)</p>
<p>Goal: Implement the <code>total_price</code> method (<a href="https://play.rust-lang.org/?code=%23%21%5Ballow%28dead_code%29%5D%0A%0Astruct%20Store%20%7B%0A%20%20%20%20name%3A%20String%2C%0A%20%20%20%20items%3A%20Vec%3CItem%3E%2C%0A%7D%0A%0A%23%5Bderive%28Debug%29%5D%0Astruct%20Item%20%7B%0A%20%20%20%20name%3A%20%26%27static%20str%2C%0A%20%20%20%20price%3A%20f32%2C%0A%7D%0A%0Aimpl%20Store%20%7B%0A%20%20%20%20fn%20new%28name%3A%20String%29%20-%3E%20Store%20%7B%0A%20%20%20%20%20%20%20%20Store%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3A%20name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20items%3A%20vec%21%5B%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20fn%20add_item%28%26mut%20self%2C%20item%3A%20Item%29%20%7B%0A%20%20%20%20%20%20%20%20self.items.push%28item%29%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20fn%20price%28%26self%2C%20item_name%3A%20%26str%29%20-%3E%20Option%3Cf32%3E%20%7B%0A%20%20%20%20%20%20%20%20for%20item%20in%20%26self.items%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20item.name%20%3D%3D%20item_name%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20Some%28item.price%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20None%0A%20%20%20%20%7D%0A%0A%20%20%20%20fn%20total_price%28%26self%2C%20shopping_list%3A%20%26%5B%26str%5D%29%20-%3E%20Option%3Cf32%3E%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Goal%3A%20compute%20the%20total%20price%20of%20all%20items%20in%20the%20shopping%0A%20%20%20%20%20%20%20%20%2F%2F%20list.%20If%20any%20of%20the%20options%20are%20not%20present%2C%20return%20%60None%60.%0A%20%20%20%20%20%20%20%20unimplemented%21%28%29%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20build_store%28%29%20-%3E%20Store%20%7B%0A%20%20%20%20let%20mut%20store%20%3D%20Store%3A%3Anew%28format%21%28%22Rustmart%22%29%29%3B%0A%20%20%20%20store.add_item%28Item%20%7B%20name%3A%20%22chocolate%22%2C%20price%3A%205.0%20%7D%29%3B%0A%20%20%20%20store.add_item%28Item%20%7B%20name%3A%20%22socks%22%2C%20price%3A%2023.0%20%7D%29%3B%0A%20%20%20%20store.add_item%28Item%20%7B%20name%3A%20%22plush%20Mozilla%20dinosaur%22%2C%20price%3A%2013.0%20%7D%29%3B%0A%20%20%20%20store%0A%7D%0A%0A%23%5Btest%5D%0Afn%20total_price%28%29%20%7B%0A%20%20%20%20let%20store%20%3D%20build_store%28%29%3B%0A%20%20%20%20let%20list%20%3D%20vec%21%5B%22chocolate%22%2C%20%22plush%20Mozilla%20dinosaur%22%5D%3B%0A%20%20%20%20assert_eq%21%28store.total_price%28%26list%29%2C%20Some%2818.0%29%29%3B%0A%7D%0A%0A%23%5Btest%5D%0Afn%20total_price_missing%28%29%20%7B%0A%20%20%20%20let%20store%20%3D%20build_store%28%29%3B%0A%20%20%20%20let%20list%20%3D%20vec%21%5B%22chocolate%22%2C%20%22plush%20Mozilla%20dinosaur%22%2C%20%22fork%20and%20knife%22%5D%3B%0A%20%20%20%20assert_eq%21%28store.total_price%28%26list%29%2C%20None%29%3B%0A%7D&version=nightly">playpen</a>).</p>
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="at">#![</span>allow<span class="at">(</span>dead_code<span class="at">)]</span>
<span class="kw">struct</span> Store {
name: <span class="dt">String</span>,
items: <span class="dt">Vec</span><Item>,
}
<span class="at">#[</span>derive<span class="at">(</span><span class="bu">Debug</span><span class="at">)]</span>
<span class="kw">struct</span> Item {
name: &<span class="ot">'static</span> <span class="dt">str</span>,
price: <span class="dt">f32</span>,
}
<span class="kw">impl</span> Store {
<span class="kw">fn</span> new(name: <span class="dt">String</span>) -> Store {
Store {
name: name,
items: <span class="pp">vec!</span>[],
}
}
<span class="kw">fn</span> add_item(&<span class="kw">mut</span> <span class="kw">self</span>, item: Item) {
<span class="kw">self</span>.items.push(item);
}
<span class="kw">fn</span> price(&<span class="kw">self</span>, item_name: &<span class="dt">str</span>) -> <span class="dt">Option</span><<span class="dt">f32</span>> {
<span class="kw">for</span> item <span class="kw">in</span> &<span class="kw">self</span>.items {
<span class="kw">if</span> item.name == item_name {
<span class="kw">return</span> <span class="cn">Some</span>(item.price);
}
}
<span class="cn">None</span>
}
<span class="kw">fn</span> total_price(&<span class="kw">self</span>, shopping_list: &[&<span class="dt">str</span>]) -> <span class="dt">Option</span><<span class="dt">f32</span>> {
<span class="co">// Goal: compute the total price of all items in the shopping</span>
<span class="co">// list. If any of the options are not present, return `None`.</span>
<span class="pp">unimplemented!</span>()
}
}
<span class="kw">fn</span> build_store() -> Store {
<span class="kw">let</span> <span class="kw">mut</span> store = Store::new(<span class="pp">format!</span>(<span class="st">"Rustmart"</span>));
store.add_item(Item { name: <span class="st">"chocolate"</span>, price: <span class="dv">5.0</span> });
store.add_item(Item { name: <span class="st">"socks"</span>, price: <span class="dv">23.0</span> });
store.add_item(Item { name: <span class="st">"plush Mozilla dinosaur"</span>, price: <span class="dv">13.0</span> });
store
}
<span class="at">#[</span>test<span class="at">]</span>
<span class="kw">fn</span> total_price() {
<span class="kw">let</span> store = build_store();
<span class="kw">let</span> list = <span class="pp">vec!</span>[<span class="st">"chocolate"</span>, <span class="st">"plush Mozilla dinosaur"</span>];
<span class="pp">assert_eq!</span>(store.total_price(&list), <span class="cn">Some</span>(<span class="dv">18.0</span>));
}
<span class="at">#[</span>test<span class="at">]</span>
<span class="kw">fn</span> total_price_missing() {
<span class="kw">let</span> store = build_store();
<span class="kw">let</span> list = <span class="pp">vec!</span>[<span class="st">"chocolate"</span>, <span class="st">"plush Mozilla dinosaur"</span>, <span class="st">"fork and knife"</span>];
<span class="pp">assert_eq!</span>(store.total_price(&list), <span class="cn">None</span>);
}</code></pre></div>
</body>
</html>