@@ -378,6 +378,53 @@ let c = &i; // still ok!
378
378
```
379
379
"## ,
380
380
381
+ E0500 : r##"
382
+ A borrowed variable was used in another closure. Example of erroneous code:
383
+
384
+ ```compile_fail
385
+ fn you_know_nothing(jon_snow: &mut i32) {
386
+ let nights_watch = || {
387
+ *jon_snow = 2;
388
+ };
389
+ let starks = || {
390
+ *jon_snow = 3; // error: closure requires unique access to `jon_snow`
391
+ // but it is already borrowed
392
+ };
393
+ }
394
+
395
+ In here, `jon_snow` is already borrowed by the `nights_watch` closure, so it
396
+ cannot be borrowed by the `starks` closure at the same time. To fix this issue,
397
+ you can put the closure in its own scope:
398
+
399
+ ```
400
+ fn you_know_nothing(jon_snow: &mut i32) {
401
+ {
402
+ let nights_watch = || {
403
+ *jon_snow = 2;
404
+ };
405
+ } // At this point, `jon_snow` is free.
406
+ let starks = || {
407
+ *jon_snow = 3;
408
+ };
409
+ }
410
+ ```
411
+
412
+ Or, if the type implements the `Clone` trait, you can clone it between
413
+ closures:
414
+
415
+ ```
416
+ fn you_know_nothing(jon_snow: &mut i32) {
417
+ let mut jon_copy = jon_snow.clone();
418
+ let nights_watch = || {
419
+ jon_copy = 2;
420
+ };
421
+ let starks = || {
422
+ *jon_snow = 3;
423
+ };
424
+ }
425
+ ```
426
+ "## ,
427
+
381
428
E0501 : r##"
382
429
This error indicates that a mutable variable is being used while it is still
383
430
captured by a closure. Because the closure has borrowed the variable, it is not
@@ -642,6 +689,85 @@ fn print_fancy_ref(fancy_ref: &FancyNum){
642
689
```
643
690
"## ,
644
691
692
+ E0505 : r##"
693
+ A value was moved out while it was still borrowed.
694
+ Erroneous code example:
695
+
696
+ ```compile_fail
697
+ struct Value {}
698
+
699
+ fn eat(val: Value) {}
700
+
701
+ fn main() {
702
+ let x = Value{};
703
+ {
704
+ let _ref_to_val: &Value = &x;
705
+ eat(x);
706
+ }
707
+ }
708
+ ```
709
+
710
+ Here, the function `eat` takes the ownership of `x`. However,
711
+ `x` cannot be moved because it was borrowed to `_ref_to_val`.
712
+ To fix that you can do few different things:
713
+
714
+ * Try to avoid moving the variable.
715
+ * Release borrow before move.
716
+ * Implement the `Copy` trait on the type.
717
+
718
+ Examples:
719
+
720
+ ```
721
+ struct Value {}
722
+
723
+ fn eat(val: &Value) {}
724
+
725
+ fn main() {
726
+ let x = Value{};
727
+ {
728
+ let _ref_to_val: &Value = &x;
729
+ eat(&x); // pass by reference, if it's possible
730
+ }
731
+ }
732
+ ```
733
+
734
+ Or:
735
+
736
+ ```
737
+ struct Value {}
738
+
739
+ fn eat(val: Value) {}
740
+
741
+ fn main() {
742
+ let x = Value{};
743
+ {
744
+ let _ref_to_val: &Value = &x;
745
+ }
746
+ eat(x); // release borrow and then move it.
747
+ }
748
+ ```
749
+
750
+ Or:
751
+
752
+ ```
753
+ #[derive(Clone, Copy)] // implement Copy trait
754
+ struct Value {}
755
+
756
+ fn eat(val: Value) {}
757
+
758
+ fn main() {
759
+ let x = Value{};
760
+ {
761
+ let _ref_to_val: &Value = &x;
762
+ eat(x); // it will be copied here.
763
+ }
764
+ }
765
+ ```
766
+
767
+ You can find more information about borrowing in the rust-book:
768
+ http://doc.rust-lang.org/stable/book/references-and-borrowing.html
769
+ "## ,
770
+
645
771
E0507 : r##"
646
772
You tried to move out of a value which was borrowed. Erroneous code example:
647
773
@@ -857,10 +983,8 @@ fn main() {
857
983
register_diagnostics ! {
858
984
E0385 , // {} in an aliasable location
859
985
E0388 , // {} in a static location
860
- E0500 , // closure requires unique access to `..` but .. is already borrowed
861
986
E0502 , // cannot borrow `..`.. as .. because .. is also borrowed as ...
862
987
E0503 , // cannot use `..` because it was mutably borrowed
863
- E0505 , // cannot move out of `..` because it is borrowed
864
988
E0508 , // cannot move out of type `..`, a non-copy fixed-size array
865
989
E0524 , // two closures require unique access to `..` at the same time
866
990
}
0 commit comments