You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pubstructTest{pubdata:Option<u32>,pubother_data:u32,}implTest{pubfnpreload(&mutself) -> &u32{ifletSome(data) = &self.data{return data;}// Why `self.data` is still borrowed here?// Manually adding scope around `if {}` doesn't help.self.data = Some(123);&self.other_data}}
I expected to see this happen: it compiles
Instead, this happened:
error[E0506]: cannot assign to `self.data` because it is borrowed
--> <source>:12:9
|
7 | pub fn preload(&mut self) -> &u32 {
| - let's call the lifetime of this reference `'1`
8 | if let Some(data) = &self.data {
| ---------- borrow of `self.data` occurs here
9 | return data
| ---- returning this value requires that `self.data` is borrowed for `'1`
...
12 | self.data = Some(123);
| ^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `self.data` occurs here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0506`.
Compiler returned: 1
Thanks for the report! As mentioned, this is a known limitation of the borrow checker. Closing as a duplicate of #54663 (or one of the other issues linked there).
I tried this code:
I expected to see this happen: it compiles
Instead, this happened:
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: