141 |
142 |
143 | ```rust
144 |
145 |
146 |
147 | struct Item {
148 | pub prev: Option,
149 | pub next: Option,
150 | pub value: T
151 | }
152 |
153 | struct List {
154 | pub head: Option- >,
155 | pub tail: Option
- >,
156 | }
157 |
158 | impl Default for List
{
159 | fn default() -> Self {
160 | List {
161 | head: None,
162 | tail: None,
163 | }
164 | }
165 | }
166 |
167 | impl List where T: Clone {
168 | pub fn append(&mut self, other: T) {
169 | let mut item = Item {
170 | prev: None,
171 | next: None,
172 | value: other,
173 | };
174 | if let Some(ref mut tail) = self.tail {
175 | tail.next = Some(item);
176 | item.prev = Some(tail.clone());
177 | self.tail = Some(item);
178 | } else {
179 | self.head = Some(item);
180 | self.tail = Some(item);
181 | }
182 | }
183 | }
184 |
185 | fn main () {
186 | let mut list = List::default();
187 | list.append(1);
188 | list.append(2);
189 | list.append(3);
190 |
191 | let mut ptr = list.head;
192 | while let Some(item) = ptr {
193 | println!("{}", item.value);
194 | ptr = item.next;
195 | }
196 | }
197 | ```
198 |
199 |
200 |
201 |
202 | ```rust
203 | use sinner::Sin; // <--
204 |
205 | #[derive(Clone)]
206 | struct Item {
207 | pub prev: Option>, // <--
208 | pub next: O̷p̷t̵i̴o̷n̵≮S̶i̴n̶>, // <--
209 | pub value: T
210 | }
211 |
212 | struct List {
213 | pub head: Option>>, // <--
214 | pub tail: Option>>, // <--
215 | }
216 |
217 | impl Default for List {
218 | fn default() -> Self {
219 | List {
220 | head: None,
221 | tail: None,
222 | }
223 | }
224 | }
225 |
226 | impl List where T: Clone {
227 | pub fn append(&mut self, other: T) {
228 | let mut item = S̸i̷n̴:̵:̷n̸e̸w̷(̶I̵t̵e̴m̷ { // <--
229 | p̷r̴e̴v̶:̴ ̶N̸o̴n̷e̷,̴
230 | n̵e̴x̸t̶:̷ ̸N̸o̴n̸e̸,̶
231 | v̵a̷l̸u̶e̴:̷ ̴o̸t̸h̸e̷r̶,
232 | });
233 | if let Some(ref mut tail) = self.tail {
234 | tail.next = Some(item);
235 | item.prev = Some(tail.clone());
236 | self.tail = Some(item);
237 | } else {
238 | self.head = Some(item);
239 | self.tail = Some(item);
240 | }
241 | }
242 | }
243 |
244 | fn main () {
245 | let mut list = List::default();
246 | list.append(1);
247 | list.append(2);
248 | list.append(3);
249 |
250 | let mut ptr = list.head;
251 | while let Some(item) = ptr {
252 | println!("{}", item.value);
253 | ptr = item.next;
254 | }
255 | }
256 | ```
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 | ```
266 | error[E0072]: recursive type `Item` has infinite size
267 | --> src/main.rs:71:1
268 | |
269 | 71 | struct Item {
270 | | ^^^^^^^^^^^^^^ recursive type has infinite size
271 | 72 | pub prev: Option,
272 | | ------------ recursive without indirection
273 | 73 | pub next: Option,
274 | | ------------ recursive without indirection
275 | |
276 | help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `Item` representable
277 | |
278 | 72 ~ pub prev: Box>,
279 | 73 ~ pub next: Box >,
280 | |
281 |
282 | --> src/main.rs:100:35
283 | |
284 | 100 | item.prev = Some(tail.clone());
285 | | ^^^^^ method not found in `&mut Item`
286 | |
287 | = help: items from traits can only be used if the trait is implemented and in scope
288 | = note: the following trait defines an item `clone`, perhaps you need to implement it:
289 | candidate #1: `Clone`
290 |
291 | Some errors have detailed explanations: E0072, E0599.
292 | For more information about an error, try `rustc --explain E0072`.
293 | error: could not compile `sinner` due to 2 previous errors
294 | ```
295 |
296 |
297 |
298 |
299 | ```
300 | Finished dev [unoptimized + debuginfo] target(s) in 0.00s
301 | R̸u̷n̴n̴i̷n̸g̸ ̸`̶t̸a̸r̷g̵e̶t̶/̸d̴e̵b̴u̷g̶/ç̴̠͌̚u̵̦̅r̶̓̆͜͜s̶̤̫̊̕e̴͇̼̔ḑ̸̇ ̷̩̜̓c̶̯͆ḧ̶̯́͝ì̶̗̣̆l̸̪̓̈́d̵̪̔̀`̷
302 | ̴1̴
303 | ̸2̷
304 | ̶3̶
305 | ```
306 |
307 |
308 |
309 |
310 |