|
1 | | -[[questions]] |
2 | | -id = "b2dd44fc-2782-495e-bb54-65af8442c388" |
3 | | -type = "MultipleChoice" |
4 | | -prompt.prompt = """ |
5 | | -**Program 1:** |
| 1 | +# [[questions]] |
| 2 | +# id = "b2dd44fc-2782-495e-bb54-65af8442c388" |
| 3 | +# type = "MultipleChoice" |
| 4 | +# prompt.prompt = """ |
| 5 | +# **Program 1:** |
6 | 6 |
|
7 | | -```ide |
8 | | -/// Adds the string `s` to all elements of |
9 | | -/// the input iterator |
10 | | -fn concat_all( |
11 | | - iter: impl Iterator<Item = String>, |
12 | | - s: &str |
13 | | -) -> impl Iterator<Item = String> { |
14 | | - iter.map(move |s2| s2 + s) |
15 | | -} |
16 | | -``` |
| 7 | +# ```ide |
| 8 | +# /// Adds the string `s` to all elements of |
| 9 | +# /// the input iterator |
| 10 | +# fn concat_all( |
| 11 | +# iter: impl Iterator<Item = String>, |
| 12 | +# s: &str |
| 13 | +# ) -> impl Iterator<Item = String> { |
| 14 | +# iter.map(move |s2| s2 + s) |
| 15 | +# } |
| 16 | +# ``` |
17 | 17 |
|
18 | | -If you tried to compile this program, which of the following best describes the compiler error you would get? |
19 | | -""" |
20 | | -answer.answer = "the returned iterator captures a lifetime that isn't in the bounds of `impl Iterator<Item = String>`" |
21 | | -prompt.distractors = [ |
22 | | - "`s2 + s` is not valid because `s` and `s2` have different lifetimes", |
23 | | - "the closure `|s2| s2 + s` cannot move out of a shared reference", |
24 | | - "`iter.map(..)` does not implement the trait `Iterator<Item = String>`" |
25 | | -] |
26 | | -context = """ |
27 | | -The string reference `s` has an implicit lifetime `'a`. The closure passed to `iter.map(..)` captures this string reference, but the type `impl Iterator<Item = String>` does not mention `'a`. Therefore the iterator captures a lifetime not in its bounds. |
28 | | -""" |
| 18 | +# If you tried to compile this program, which of the following best describes the compiler error you would get? |
| 19 | +# """ |
| 20 | +# answer.answer = "the returned iterator captures a lifetime that isn't in the bounds of `impl Iterator<Item = String>`" |
| 21 | +# prompt.distractors = [ |
| 22 | +# "`s2 + s` is not valid because `s` and `s2` have different lifetimes", |
| 23 | +# "the closure `|s2| s2 + s` cannot move out of a shared reference", |
| 24 | +# "`iter.map(..)` does not implement the trait `Iterator<Item = String>`" |
| 25 | +# ] |
| 26 | +# context = """ |
| 27 | +# The string reference `s` has an implicit lifetime `'a`. The closure passed to `iter.map(..)` captures this string reference, but the type `impl Iterator<Item = String>` does not mention `'a`. Therefore the iterator captures a lifetime not in its bounds. |
| 28 | +# """ |
29 | 29 |
|
30 | 30 |
|
31 | | -[[questions]] |
32 | | -id = "816e9376-7161-4f41-bc04-9dba8413d7a7" |
33 | | -type = "MultipleChoice" |
34 | | -prompt.prompt = """ |
35 | | -**Program 1:** |
| 31 | +# [[questions]] |
| 32 | +# id = "816e9376-7161-4f41-bc04-9dba8413d7a7" |
| 33 | +# type = "MultipleChoice" |
| 34 | +# prompt.prompt = """ |
| 35 | +# **Program 1:** |
36 | 36 |
|
37 | | -```ide |
38 | | -/// Adds the string `s` to all elements of |
39 | | -/// the input iterator |
40 | | -fn concat_all( |
41 | | - iter: impl Iterator<Item = String>, |
42 | | - s: &str |
43 | | -) -> impl Iterator<Item = String> { |
44 | | - iter.map(move |s2| s2 + s) |
45 | | -} |
46 | | -``` |
| 37 | +# ```ide |
| 38 | +# /// Adds the string `s` to all elements of |
| 39 | +# /// the input iterator |
| 40 | +# fn concat_all( |
| 41 | +# iter: impl Iterator<Item = String>, |
| 42 | +# s: &str |
| 43 | +# ) -> impl Iterator<Item = String> { |
| 44 | +# iter.map(move |s2| s2 + s) |
| 45 | +# } |
| 46 | +# ``` |
47 | 47 |
|
48 | | -Normally if you try to compile this function, the compiler returns the following error: |
| 48 | +# Normally if you try to compile this function, the compiler returns the following error: |
49 | 49 |
|
50 | | -```text |
51 | | -error[E0700]: hidden type for `impl Iterator<Item = String>` captures lifetime that does not appear in bounds |
52 | | - --> test.rs:7:5 |
53 | | - | |
54 | | -5 | s: &str |
55 | | - | ---- hidden type `Map<impl Iterator<Item = String>, [closure@test.rs:7:14: 7:23]>` captures the anonymous lifetime defined here |
56 | | -6 | ) -> impl Iterator<Item = String> { |
57 | | -7 | iter.map(move |s2| s2 + s) |
58 | | -``` |
| 50 | +# ```text |
| 51 | +# error[E0700]: hidden type for `impl Iterator<Item = String>` captures lifetime that does not appear in bounds |
| 52 | +# --> test.rs:7:5 |
| 53 | +# | |
| 54 | +# 5 | s: &str |
| 55 | +# | ---- hidden type `Map<impl Iterator<Item = String>, [closure@test.rs:7:14: 7:23]>` captures the anonymous lifetime defined here |
| 56 | +# 6 | ) -> impl Iterator<Item = String> { |
| 57 | +# 7 | iter.map(move |s2| s2 + s) |
| 58 | +# ``` |
59 | 59 |
|
60 | | -Assume that the compiler did NOT reject this function. Which (if any) of the following programs would |
61 | | -(1) pass the compiler, and (2) possibly cause undefined behavior if executed? |
62 | | -Check each program that satisfies both criteria, OR check "None of these programs" if none are satisfying. |
63 | | -""" |
64 | | -answer.answer = [ |
65 | | -""" |
66 | | -``` |
67 | | -let v = vec![String::from("Rust")]; |
68 | | -let it = { |
69 | | - let s = String::from("Yes"); |
70 | | - concat_all(v.into_iter(), &s) |
71 | | -}; |
72 | | -it.collect::<Vec<_>>(); |
73 | | -``` |
74 | | -""" |
75 | | -] |
76 | | -prompt.distractors = [ |
77 | | -""" |
78 | | -``` |
79 | | -let v = vec![String::from("Rust")]; |
80 | | -concat_all(v.into_iter(), "Yes") |
81 | | - .collect::<Vec<_>>(); |
82 | | -``` |
83 | | -""", |
84 | | -""" |
85 | | -``` |
86 | | -let v = vec![String::from("Rust")]; |
87 | | -let s = String::from("Yes"); |
88 | | -concat_all(v.into_iter(), &s); |
89 | | -println!("{}", s); |
90 | | -``` |
91 | | -""", |
92 | | -"None of these programs" |
93 | | -] |
94 | | -context = """ |
95 | | -If the returned iterator isn't related to the lifetime of `s`, then memory safety is violated if: |
96 | | -1. `s` does not live forever (i.e. it's not `&'static str` like a string literal) |
97 | | -2. the return value of `concat_all` is used after `s` is dropped |
| 60 | +# Assume that the compiler did NOT reject this function. Which (if any) of the following programs would |
| 61 | +# (1) pass the compiler, and (2) possibly cause undefined behavior if executed? |
| 62 | +# Check each program that satisfies both criteria, OR check "None of these programs" if none are satisfying. |
| 63 | +# """ |
| 64 | +# answer.answer = [ |
| 65 | +# """ |
| 66 | +# ``` |
| 67 | +# let v = vec![String::from("Rust")]; |
| 68 | +# let it = { |
| 69 | +# let s = String::from("Yes"); |
| 70 | +# concat_all(v.into_iter(), &s) |
| 71 | +# }; |
| 72 | +# it.collect::<Vec<_>>(); |
| 73 | +# ``` |
| 74 | +# """ |
| 75 | +# ] |
| 76 | +# prompt.distractors = [ |
| 77 | +# """ |
| 78 | +# ``` |
| 79 | +# let v = vec![String::from("Rust")]; |
| 80 | +# concat_all(v.into_iter(), "Yes") |
| 81 | +# .collect::<Vec<_>>(); |
| 82 | +# ``` |
| 83 | +# """, |
| 84 | +# """ |
| 85 | +# ``` |
| 86 | +# let v = vec![String::from("Rust")]; |
| 87 | +# let s = String::from("Yes"); |
| 88 | +# concat_all(v.into_iter(), &s); |
| 89 | +# println!("{}", s); |
| 90 | +# ``` |
| 91 | +# """, |
| 92 | +# "None of these programs" |
| 93 | +# ] |
| 94 | +# context = """ |
| 95 | +# If the returned iterator isn't related to the lifetime of `s`, then memory safety is violated if: |
| 96 | +# 1. `s` does not live forever (i.e. it's not `&'static str` like a string literal) |
| 97 | +# 2. the return value of `concat_all` is used after `s` is dropped |
98 | 98 |
|
99 | | -Then the expression `s2 + s` would dereference `s`, a read of deallocated memory. |
100 | | -""" |
| 99 | +# Then the expression `s2 + s` would dereference `s`, a read of deallocated memory. |
| 100 | +# """ |
101 | 101 |
|
102 | | -[[questions]] |
103 | | -id = "404b2cf7-7d29-45d7-86c4-7c806a071d7b" |
104 | | -type = "MultipleChoice" |
105 | | -prompt.prompt = """ |
106 | | -**Program 1:** |
| 102 | +# [[questions]] |
| 103 | +# id = "404b2cf7-7d29-45d7-86c4-7c806a071d7b" |
| 104 | +# type = "MultipleChoice" |
| 105 | +# prompt.prompt = """ |
| 106 | +# **Program 1:** |
107 | 107 |
|
108 | | -```ide |
109 | | -/// Adds the string `s` to all elements of |
110 | | -/// the input iterator |
111 | | -fn concat_all( |
112 | | - iter: impl Iterator<Item = String>, |
113 | | - s: &str |
114 | | -) -> impl Iterator<Item = String> { |
115 | | - iter.map(move |s2| s2 + s) |
116 | | -} |
117 | | -``` |
| 108 | +# ```ide |
| 109 | +# /// Adds the string `s` to all elements of |
| 110 | +# /// the input iterator |
| 111 | +# fn concat_all( |
| 112 | +# iter: impl Iterator<Item = String>, |
| 113 | +# s: &str |
| 114 | +# ) -> impl Iterator<Item = String> { |
| 115 | +# iter.map(move |s2| s2 + s) |
| 116 | +# } |
| 117 | +# ``` |
118 | 118 |
|
119 | | -Of the following fixes (highlighted in yellow), which fix best satisfies these three criteria: |
120 | | -1. The fixed function passes the Rust compiler, |
121 | | -2. The fixed function preserves the intention of the original code, and |
122 | | -3. The fixed function does not introduce unnecessary inefficiencies |
123 | | -""" |
124 | | -answer.answer = """ |
125 | | -```ide |
126 | | -fn concat_all`[<'a>]`( |
127 | | - iter: impl Iterator<Item = String> `[+ 'a]`, |
128 | | - s: &`['a]` str |
129 | | -) -> impl Iterator<Item = String> `[+ 'a]` { |
130 | | - iter.map(move |s2| s2 + s) |
131 | | -} |
132 | | -``` |
133 | | -""" |
134 | | -prompt.distractors = [ |
135 | | -""" |
136 | | -```ide |
137 | | -fn concat_all( |
138 | | - iter: impl Iterator<Item = String>, |
139 | | - s: &`['static]` str |
140 | | -) -> impl Iterator<Item = String> { |
141 | | - iter.map(move |s2| s2 + s) |
142 | | -} |
143 | | -``` |
144 | | -""", |
145 | | -""" |
146 | | -```ide |
147 | | -fn concat_all`[<'a, 'b>]`( |
148 | | - iter: impl Iterator<Item = String> `[+ 'a]`, |
149 | | - s: &`['b]` str |
150 | | -) -> impl Iterator<Item = String> `[+ 'a + 'b]` { |
151 | | - iter.map(move |s2| s2 + s) |
152 | | -} |
153 | | -``` |
154 | | -""", |
155 | | -""" |
156 | | -```ide |
157 | | -fn concat_all( |
158 | | - iter: impl Iterator<Item = String>, |
159 | | - s: `[String]` |
160 | | -) -> impl Iterator<Item = String> { |
161 | | - iter.map(move |s2| s2 + `[&s]`) |
162 | | -} |
163 | | -``` |
164 | | -""", |
165 | | -] |
166 | | -context = """ |
167 | | -To fix this issue, we need to express the relationship between the lifetimes of `iter`, `s`, and the return type. The best way to do this is a lifetime variable `<'a>` and require that `iter` and `s` both live for `'a`, and that the output type also lives for `'a`. |
168 | | -""" |
| 119 | +# Of the following fixes (highlighted in yellow), which fix best satisfies these three criteria: |
| 120 | +# 1. The fixed function passes the Rust compiler, |
| 121 | +# 2. The fixed function preserves the intention of the original code, and |
| 122 | +# 3. The fixed function does not introduce unnecessary inefficiencies |
| 123 | +# """ |
| 124 | +# answer.answer = """ |
| 125 | +# ```ide |
| 126 | +# fn concat_all`[<'a>]`( |
| 127 | +# iter: impl Iterator<Item = String> `[+ 'a]`, |
| 128 | +# s: &`['a]` str |
| 129 | +# ) -> impl Iterator<Item = String> `[+ 'a]` { |
| 130 | +# iter.map(move |s2| s2 + s) |
| 131 | +# } |
| 132 | +# ``` |
| 133 | +# """ |
| 134 | +# prompt.distractors = [ |
| 135 | +# """ |
| 136 | +# ```ide |
| 137 | +# fn concat_all( |
| 138 | +# iter: impl Iterator<Item = String>, |
| 139 | +# s: &`['static]` str |
| 140 | +# ) -> impl Iterator<Item = String> { |
| 141 | +# iter.map(move |s2| s2 + s) |
| 142 | +# } |
| 143 | +# ``` |
| 144 | +# """, |
| 145 | +# """ |
| 146 | +# ```ide |
| 147 | +# fn concat_all`[<'a, 'b>]`( |
| 148 | +# iter: impl Iterator<Item = String> `[+ 'a]`, |
| 149 | +# s: &`['b]` str |
| 150 | +# ) -> impl Iterator<Item = String> `[+ 'a + 'b]` { |
| 151 | +# iter.map(move |s2| s2 + s) |
| 152 | +# } |
| 153 | +# ``` |
| 154 | +# """, |
| 155 | +# """ |
| 156 | +# ```ide |
| 157 | +# fn concat_all( |
| 158 | +# iter: impl Iterator<Item = String>, |
| 159 | +# s: `[String]` |
| 160 | +# ) -> impl Iterator<Item = String> { |
| 161 | +# iter.map(move |s2| s2 + `[&s]`) |
| 162 | +# } |
| 163 | +# ``` |
| 164 | +# """, |
| 165 | +# ] |
| 166 | +# context = """ |
| 167 | +# To fix this issue, we need to express the relationship between the lifetimes of `iter`, `s`, and the return type. The best way to do this is a lifetime variable `<'a>` and require that `iter` and `s` both live for `'a`, and that the output type also lives for `'a`. |
| 168 | +# """ |
169 | 169 |
|
170 | 170 |
|
171 | 171 | [[questions]] |
172 | 172 | id = "2c6d6e2e-ad6e-4e98-bf88-8cada144ebee" |
173 | 173 | type = "MultipleChoice" |
174 | 174 | prompt.prompt = """ |
175 | | -**Program 2:** |
| 175 | +**Program 1:** |
176 | 176 |
|
177 | 177 | ```ide |
178 | 178 | /// Adds a Display-able object into a vector of |
@@ -204,7 +204,7 @@ must outlive the vector. However, the lifetime of `T` is unspecified, so `T` may |
204 | 204 | id = "36da4652-baeb-47a6-9ee1-1448b02cbe8e" |
205 | 205 | type = "MultipleChoice" |
206 | 206 | prompt.prompt = """ |
207 | | -**Program 2:** |
| 207 | +**Program 1:** |
208 | 208 |
|
209 | 209 | ```ide |
210 | 210 | /// Adds a Display-able object into a vector of |
@@ -273,7 +273,7 @@ the later use of `v[0]` is a read of deallocated memory. |
273 | 273 | id = "2da21d0e-5722-4908-b528-dc87bbce1faf" |
274 | 274 | type = "MultipleChoice" |
275 | 275 | prompt.prompt = """ |
276 | | -**Program 2:** |
| 276 | +**Program 1:** |
277 | 277 |
|
278 | 278 | ```ide |
279 | 279 | /// Adds a Display-able object into a vector of |
|
0 commit comments