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
Replacing the code as clippy suggests will result in the following compile error.
#![allow(clippy::disallowed_names)]structFoo{opt_x:Option<String>,}fnbar(foo:&mutFoo) -> Option<String>{letref x = foo.opt_x()?;// ← replaced with ? operatorlet opt_y = Some(x.clone());
std::mem::replace(&mut foo.opt_x, opt_y)}
error[E0507]: cannot move out of `foo.opt_x` which is behind a mutable reference
--> src/main.rs:8:17
|
8 | let ref x = foo.opt_x?;
| ^^^^^^^^^-
| |
| `foo.opt_x` moved due to this method call
| move occurs because `foo.opt_x` has type `Option<String>`, which does not implement the `Copy` trait
|
This is because the ? operator takes ownership of opt_x. Therefore, we can avoid this error by inserting as_ref in the middle. The following code compiles:
fnbar(foo:&mutFoo) -> Option<String>{let x = foo.opt_x.as_ref()?;// ← inserted as_ref() here
...}
Activity
nojima commentedon Sep 19, 2024
Replacing the code as clippy suggests will result in the following compile error.
This is because the
?operator takes ownership ofopt_x. Therefore, we can avoid this error by insertingas_refin the middle. The following code compiles:In general, a sentence of the following form
should be replaced with
sanpii commentedon Oct 26, 2024
And:
with:
question_mark: avoid incorrect suggestion when `ref` binding used (#1…