@@ -317,272 +317,6 @@ fn main() {
317317```
318318"## ,
319319
320- E0507 : r##"
321- You tried to move out of a value which was borrowed. Erroneous code example:
322-
323- ```compile_fail,E0507
324- use std::cell::RefCell;
325-
326- struct TheDarkKnight;
327-
328- impl TheDarkKnight {
329- fn nothing_is_true(self) {}
330- }
331-
332- fn main() {
333- let x = RefCell::new(TheDarkKnight);
334-
335- x.borrow().nothing_is_true(); // error: cannot move out of borrowed content
336- }
337- ```
338-
339- Here, the `nothing_is_true` method takes the ownership of `self`. However,
340- `self` cannot be moved because `.borrow()` only provides an `&TheDarkKnight`,
341- which is a borrow of the content owned by the `RefCell`. To fix this error,
342- you have three choices:
343-
344- * Try to avoid moving the variable.
345- * Somehow reclaim the ownership.
346- * Implement the `Copy` trait on the type.
347-
348- Examples:
349-
350- ```
351- use std::cell::RefCell;
352-
353- struct TheDarkKnight;
354-
355- impl TheDarkKnight {
356- fn nothing_is_true(&self) {} // First case, we don't take ownership
357- }
358-
359- fn main() {
360- let x = RefCell::new(TheDarkKnight);
361-
362- x.borrow().nothing_is_true(); // ok!
363- }
364- ```
365-
366- Or:
367-
368- ```
369- use std::cell::RefCell;
370-
371- struct TheDarkKnight;
372-
373- impl TheDarkKnight {
374- fn nothing_is_true(self) {}
375- }
376-
377- fn main() {
378- let x = RefCell::new(TheDarkKnight);
379- let x = x.into_inner(); // we get back ownership
380-
381- x.nothing_is_true(); // ok!
382- }
383- ```
384-
385- Or:
386-
387- ```
388- use std::cell::RefCell;
389-
390- #[derive(Clone, Copy)] // we implement the Copy trait
391- struct TheDarkKnight;
392-
393- impl TheDarkKnight {
394- fn nothing_is_true(self) {}
395- }
396-
397- fn main() {
398- let x = RefCell::new(TheDarkKnight);
399-
400- x.borrow().nothing_is_true(); // ok!
401- }
402- ```
403-
404- Moving a member out of a mutably borrowed struct will also cause E0507 error:
405-
406- ```compile_fail,E0507
407- struct TheDarkKnight;
408-
409- impl TheDarkKnight {
410- fn nothing_is_true(self) {}
411- }
412-
413- struct Batcave {
414- knight: TheDarkKnight
415- }
416-
417- fn main() {
418- let mut cave = Batcave {
419- knight: TheDarkKnight
420- };
421- let borrowed = &mut cave;
422-
423- borrowed.knight.nothing_is_true(); // E0507
424- }
425- ```
426-
427- It is fine only if you put something back. `mem::replace` can be used for that:
428-
429- ```
430- # struct TheDarkKnight;
431- # impl TheDarkKnight { fn nothing_is_true(self) {} }
432- # struct Batcave { knight: TheDarkKnight }
433- use std::mem;
434-
435- let mut cave = Batcave {
436- knight: TheDarkKnight
437- };
438- let borrowed = &mut cave;
439-
440- mem::replace(&mut borrowed.knight, TheDarkKnight).nothing_is_true(); // ok!
441- ```
442-
443- You can find more information about borrowing in the rust-book:
444- http://doc.rust-lang.org/book/first-edition/references-and-borrowing.html
445- "## ,
446-
447- E0508 : r##"
448- A value was moved out of a non-copy fixed-size array.
449-
450- Example of erroneous code:
451-
452- ```compile_fail,E0508
453- struct NonCopy;
454-
455- fn main() {
456- let array = [NonCopy; 1];
457- let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
458- // a non-copy fixed-size array
459- }
460- ```
461-
462- The first element was moved out of the array, but this is not
463- possible because `NonCopy` does not implement the `Copy` trait.
464-
465- Consider borrowing the element instead of moving it:
466-
467- ```
468- struct NonCopy;
469-
470- fn main() {
471- let array = [NonCopy; 1];
472- let _value = &array[0]; // Borrowing is allowed, unlike moving.
473- }
474- ```
475-
476- Alternatively, if your type implements `Clone` and you need to own the value,
477- consider borrowing and then cloning:
478-
479- ```
480- #[derive(Clone)]
481- struct NonCopy;
482-
483- fn main() {
484- let array = [NonCopy; 1];
485- // Now you can clone the array element.
486- let _value = array[0].clone();
487- }
488- ```
489- "## ,
490-
491- E0509 : r##"
492- This error occurs when an attempt is made to move out of a value whose type
493- implements the `Drop` trait.
494-
495- Example of erroneous code:
496-
497- ```compile_fail,E0509
498- struct FancyNum {
499- num: usize
500- }
501-
502- struct DropStruct {
503- fancy: FancyNum
504- }
505-
506- impl Drop for DropStruct {
507- fn drop(&mut self) {
508- // Destruct DropStruct, possibly using FancyNum
509- }
510- }
511-
512- fn main() {
513- let drop_struct = DropStruct{fancy: FancyNum{num: 5}};
514- let fancy_field = drop_struct.fancy; // Error E0509
515- println!("Fancy: {}", fancy_field.num);
516- // implicit call to `drop_struct.drop()` as drop_struct goes out of scope
517- }
518- ```
519-
520- Here, we tried to move a field out of a struct of type `DropStruct` which
521- implements the `Drop` trait. However, a struct cannot be dropped if one or
522- more of its fields have been moved.
523-
524- Structs implementing the `Drop` trait have an implicit destructor that gets
525- called when they go out of scope. This destructor may use the fields of the
526- struct, so moving out of the struct could make it impossible to run the
527- destructor. Therefore, we must think of all values whose type implements the
528- `Drop` trait as single units whose fields cannot be moved.
529-
530- This error can be fixed by creating a reference to the fields of a struct,
531- enum, or tuple using the `ref` keyword:
532-
533- ```
534- struct FancyNum {
535- num: usize
536- }
537-
538- struct DropStruct {
539- fancy: FancyNum
540- }
541-
542- impl Drop for DropStruct {
543- fn drop(&mut self) {
544- // Destruct DropStruct, possibly using FancyNum
545- }
546- }
547-
548- fn main() {
549- let drop_struct = DropStruct{fancy: FancyNum{num: 5}};
550- let ref fancy_field = drop_struct.fancy; // No more errors!
551- println!("Fancy: {}", fancy_field.num);
552- // implicit call to `drop_struct.drop()` as drop_struct goes out of scope
553- }
554- ```
555-
556- Note that this technique can also be used in the arms of a match expression:
557-
558- ```
559- struct FancyNum {
560- num: usize
561- }
562-
563- enum DropEnum {
564- Fancy(FancyNum)
565- }
566-
567- impl Drop for DropEnum {
568- fn drop(&mut self) {
569- // Destruct DropEnum, possibly using FancyNum
570- }
571- }
572-
573- fn main() {
574- // Creates and enum of type `DropEnum`, which implements `Drop`
575- let drop_enum = DropEnum::Fancy(FancyNum{num: 10});
576- match drop_enum {
577- // Creates a reference to the inside of `DropEnum::Fancy`
578- DropEnum::Fancy(ref fancy_field) => // No error!
579- println!("It was fancy-- {}!", fancy_field.num),
580- }
581- // implicit call to `drop_enum.drop()` as drop_enum goes out of scope
582- }
583- ```
584- "## ,
585-
586320E0595 : r##"
587321Closures cannot mutate immutable captured variables.
588322
0 commit comments