@@ -390,6 +390,90 @@ fn foo(a: &mut i32) {
390390```
391391"## ,
392392
393+ E0506 : r##"
394+ This error occurs when an attempt is made to assign to a borrowed value.
395+
396+ Example of erroneous code:
397+
398+ ```compile_fail
399+ struct FancyNum {
400+ num: u8
401+ }
402+
403+ fn main() {
404+ let mut fancy_num = FancyNum { num: 5 };
405+ let fancy_ref = &fancy_num;
406+ fancy_num = FancyNum { num: 6 };
407+ // error: cannot assign to `fancy_num` because it is borrowed
408+
409+ println!("Num: {}, Ref: {}", fancy_num.num, fancy_ref.num);
410+ }
411+ ```
412+
413+ Because `fancy_ref` still holds a reference to `fancy_num`, `fancy_num` can't
414+ be assigned to a new value as it would invalidate the reference.
415+
416+ Alternatively, we can move out of `fancy_num` into a second `fancy_num`:
417+
418+ ```
419+ struct FancyNum {
420+ num: u8
421+ }
422+
423+ fn main() {
424+ let mut fancy_num = FancyNum { num: 5 };
425+ let moved_num = fancy_num;
426+ fancy_num = FancyNum { num: 6 };
427+
428+ println!("Num: {}, Moved num: {}", fancy_num.num, moved_num.num);
429+ }
430+ ```
431+
432+ If the value has to be borrowed, try limiting the lifetime of the borrow using
433+ a scoped block:
434+
435+ ```
436+ struct FancyNum {
437+ num: u8
438+ }
439+
440+ fn main() {
441+ let mut fancy_num = FancyNum { num: 5 };
442+
443+ {
444+ let fancy_ref = &fancy_num;
445+ println!("Ref: {}", fancy_ref.num);
446+ }
447+
448+ // Works because `fancy_ref` is no longer in scope
449+ fancy_num = FancyNum { num: 6 };
450+ println!("Num: {}", fancy_num.num);
451+ }
452+ ```
453+
454+ Or by moving the reference into a function:
455+
456+ ```
457+ struct FancyNum {
458+ num: u8
459+ }
460+
461+ fn main() {
462+ let mut fancy_num = FancyNum { num: 5 };
463+
464+ print_fancy_ref(&fancy_num);
465+
466+ // Works because function borrow has ended
467+ fancy_num = FancyNum { num: 6 };
468+ println!("Num: {}", fancy_num.num);
469+ }
470+
471+ fn print_fancy_ref(fancy_ref: &FancyNum){
472+ println!("Ref: {}", fancy_ref.num);
473+ }
474+ ```
475+ "## ,
476+
393477E0507 : r##"
394478You tried to move out of a value which was borrowed. Erroneous code example:
395479
@@ -516,7 +600,6 @@ register_diagnostics! {
516600 E0503 , // cannot use `..` because it was mutably borrowed
517601 E0504 , // cannot move `..` into closure because it is borrowed
518602 E0505 , // cannot move out of `..` because it is borrowed
519- E0506 , // cannot assign to `..` because it is borrowed
520603 E0508 , // cannot move out of type `..`, a non-copy fixed-size array
521604 E0509 , // cannot move out of type `..`, which defines the `Drop` trait
522605 E0524 , // two closures require unique access to `..` at the same time
0 commit comments