@@ -633,6 +633,62 @@ fn main() {
633
633
```
634
634
"## ,
635
635
636
+ E0503 : r##"
637
+ A value was used after it was mutably borrowed.
638
+
639
+ Example of erroneous code:
640
+
641
+ ```compile_fail
642
+ fn main() {
643
+ let mut value = 3;
644
+ // Create a mutable borrow of `value`. This borrow
645
+ // lives until the end of this function.
646
+ let _borrow = &mut value;
647
+ let _sum = value + 1; // error: cannot use `value` because
648
+ // it was mutably borrowed
649
+ }
650
+ ```
651
+
652
+ In this example, `value` is mutably borrowed by `borrow` and cannot be
653
+ used to calculate `sum`. This is not possible because this would violate
654
+ Rust's mutability rules.
655
+
656
+ You can fix this error by limiting the scope of the borrow:
657
+
658
+ ```
659
+ fn main() {
660
+ let mut value = 3;
661
+ // By creating a new block, you can limit the scope
662
+ // of the reference.
663
+ {
664
+ let _borrow = &mut value; // Use `_borrow` inside this block.
665
+ }
666
+ // The block has ended and with it the borrow.
667
+ // You can now use `value` again.
668
+ let _sum = value + 1;
669
+ }
670
+ ```
671
+
672
+ Or by cloning `value` before borrowing it:
673
+
674
+ ```
675
+ fn main() {
676
+ let mut value = 3;
677
+ // We clone `value`, creating a copy.
678
+ let value_cloned = value.cloned();
679
+ // The mutable borrow is a reference to `value` and
680
+ // not to `value_cloned`...
681
+ let _borrow = &mut value;
682
+ // ... which means we can still use `value_cloned`,
683
+ let _sum = value_cloned + 1;
684
+ // even though the borrow only ends here.
685
+ }
686
+ ```
687
+
688
+ You can find more information about borrowing in the rust-book:
689
+ http://doc.rust-lang.org/stable/book/references-and-borrowing.html
690
+ "## ,
691
+
636
692
E0506 : r##"
637
693
This error occurs when an attempt is made to assign to a borrowed value.
638
694
@@ -911,6 +967,50 @@ You can find more information about borrowing in the rust-book:
911
967
http://doc.rust-lang.org/stable/book/references-and-borrowing.html
912
968
"## ,
913
969
970
+ E0508 : r##"
971
+ A value was moved out of a non-copy fixed-size array.
972
+
973
+ Example of erroneous code:
974
+
975
+ ```compile_fail
976
+ struct NonCopy;
977
+
978
+ fn main() {
979
+ let array = [NonCopy; 1];
980
+ let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
981
+ // a non-copy fixed-size array
982
+ }
983
+ ```
984
+
985
+ The first element was moved out of the array, but this is not
986
+ possible because `NonCopy` does not implement the `Copy` trait.
987
+
988
+ Consider borrowing the element instead of moving it:
989
+
990
+ ```
991
+ struct NonCopy;
992
+
993
+ fn main() {
994
+ let array = [NonCopy; 1];
995
+ let _value = &array[0]; // Borrowing is allowed, unlike moving.
996
+ }
997
+ ```
998
+
999
+ Alternatively, if your type implements `Clone` and you need to own the value,
1000
+ consider borrowing and then cloning:
1001
+
1002
+ ```
1003
+ #[derive(Clone)]
1004
+ struct NonCopy;
1005
+
1006
+ fn main() {
1007
+ let array = [NonCopy; 1];
1008
+ // Now you can clone the array element.
1009
+ let _value = array[0].clone();
1010
+ }
1011
+ ```
1012
+ "## ,
1013
+
914
1014
E0509 : r##"
915
1015
This error occurs when an attempt is made to move out of a value whose type
916
1016
implements the `Drop` trait.
@@ -1011,7 +1111,5 @@ fn main() {
1011
1111
register_diagnostics ! {
1012
1112
E0385 , // {} in an aliasable location
1013
1113
E0388 , // {} in a static location
1014
- E0503 , // cannot use `..` because it was mutably borrowed
1015
- E0508 , // cannot move out of type `..`, a non-copy fixed-size array
1016
1114
E0524 , // two closures require unique access to `..` at the same time
1017
1115
}
0 commit comments