From 6569a2ec1bef59bd409b13967278c95afb4bec28 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 7 Apr 2020 14:07:57 +0200 Subject: Clean up E0507 explanation --- src/librustc_error_codes/error_codes/E0507.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/librustc_error_codes/error_codes') diff --git a/src/librustc_error_codes/error_codes/E0507.md b/src/librustc_error_codes/error_codes/E0507.md index 1e3457e96c5..254751fc45e 100644 --- a/src/librustc_error_codes/error_codes/E0507.md +++ b/src/librustc_error_codes/error_codes/E0507.md @@ -1,9 +1,4 @@ -You tried to move out of a value which was borrowed. - -This can also happen when using a type implementing `Fn` or `FnMut`, as neither -allows moving out of them (they usually represent closures which can be called -more than once). Much of the text following applies equally well to non-`FnOnce` -closure bodies. +A borrowed value was moved out. Erroneous code example: @@ -32,6 +27,11 @@ you have three choices: * Somehow reclaim the ownership. * Implement the `Copy` trait on the type. +This can also happen when using a type implementing `Fn` or `FnMut`, as neither +allows moving out of them (they usually represent closures which can be called +more than once). Much of the text following applies equally well to non-`FnOnce` +closure bodies. + Examples: ``` -- cgit 1.4.1-3-g733a5 From 80e3126ec1134d81dca7c698d88156307a50a3ba Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 8 Apr 2020 15:05:52 +0200 Subject: Clean up E0510 explanation --- src/librustc_error_codes/error_codes/E0510.md | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'src/librustc_error_codes/error_codes') diff --git a/src/librustc_error_codes/error_codes/E0510.md b/src/librustc_error_codes/error_codes/E0510.md index d5be417888b..e045e04bdbe 100644 --- a/src/librustc_error_codes/error_codes/E0510.md +++ b/src/librustc_error_codes/error_codes/E0510.md @@ -1,16 +1,29 @@ -Cannot mutate place in this match guard. +The matched value was assigned in a match guard. -When matching on a variable it cannot be mutated in the match guards, as this -could cause the match to be non-exhaustive: +Erroneous code example: ```compile_fail,E0510 let mut x = Some(0); match x { - None => (), - Some(_) if { x = None; false } => (), - Some(v) => (), // No longer matches + None => {} + Some(_) if { x = None; false } => {} // error! + Some(_) => {} } ``` +When matching on a variable it cannot be mutated in the match guards, as this +could cause the match to be non-exhaustive. + Here executing `x = None` would modify the value being matched and require us -to go "back in time" to the `None` arm. +to go "back in time" to the `None` arm. To fix it, change the value in the match +arm: + +``` +let mut x = Some(0); +match x { + None => {} + Some(_) => { + x = None; // ok! + } +} +``` -- cgit 1.4.1-3-g733a5