@@ -293,6 +293,94 @@ let c = &i; // still ok!
293
293
```
294
294
"## ,
295
295
296
+ E0507 : r##"
297
+ You tried to move out of a value which was borrowed. Erroneous code example:
298
+
299
+ ```
300
+ use std::cell::RefCell;
301
+
302
+ struct TheDarkKnight;
303
+
304
+ impl TheDarkKnight {
305
+ fn nothing_is_true(self) {}
306
+ }
307
+
308
+ fn main() {
309
+ let x = RefCell::new(TheDarkKnight);
310
+
311
+ x.borrow().nothing_is_true(); // error: cannot move out of borrowed content
312
+ }
313
+ ```
314
+
315
+ Here, the `nothing_is_true` method takes the ownership of `self`. However,
316
+ `self` cannot be moved because `.borrow()` only provides an `&TheDarkKnight`,
317
+ which is a borrow of the content owned by the `RefCell`. To fix this error,
318
+ you have three choices:
319
+
320
+ * Try to avoid moving the variable.
321
+ * Somehow reclaim the ownership.
322
+ * Implement the `Copy` trait on the type.
323
+
324
+ Examples:
325
+
326
+ ```
327
+ use std::cell::RefCell;
328
+
329
+ struct TheDarkKnight;
330
+
331
+ impl TheDarkKnight {
332
+ fn nothing_is_true(&self) {} // First case, we don't take ownership
333
+ }
334
+
335
+ fn main() {
336
+ let x = RefCell::new(TheDarkKnight);
337
+
338
+ x.borrow().nothing_is_true(); // ok!
339
+ }
340
+ ```
341
+
342
+ Or:
343
+
344
+ ```
345
+ use std::cell::RefCell;
346
+
347
+ struct TheDarkKnight;
348
+
349
+ impl TheDarkKnight {
350
+ fn nothing_is_true(self) {}
351
+ }
352
+
353
+ fn main() {
354
+ let x = RefCell::new(TheDarkKnight);
355
+ let x = x.into_inner(); // we get back ownership
356
+
357
+ x.nothing_is_true(); // ok!
358
+ }
359
+ ```
360
+
361
+ Or:
362
+
363
+ ```
364
+ use std::cell::RefCell;
365
+
366
+ #[derive(Clone, Copy)] // we implement the Copy trait
367
+ struct TheDarkKnight;
368
+
369
+ impl TheDarkKnight {
370
+ fn nothing_is_true(self) {}
371
+ }
372
+
373
+ fn main() {
374
+ let x = RefCell::new(TheDarkKnight);
375
+
376
+ x.borrow().nothing_is_true(); // ok!
377
+ }
378
+ ```
379
+
380
+ You can find more information about borrowing in the rust-book:
381
+ http://doc.rust-lang.org/stable/book/references-and-borrowing.html
382
+ "## ,
383
+
296
384
}
297
385
298
386
register_diagnostics ! {
@@ -306,7 +394,6 @@ register_diagnostics! {
306
394
E0504 , // cannot move `..` into closure because it is borrowed
307
395
E0505 , // cannot move out of `..` because it is borrowed
308
396
E0506 , // cannot assign to `..` because it is borrowed
309
- E0507 , // cannot move out of ..
310
397
E0508 , // cannot move out of type `..`, a non-copy fixed-size array
311
398
E0509 , // cannot move out of type `..`, which defines the `Drop` trait
312
399
}
0 commit comments