Closed
Description
Code
struct A {
member: Option<String>,
}
fn main() {
let a = A{ member: None};
function(&a);
}
fn function(a: &A) -> Option<()> {
let member = &a.member?;
None
}
Current output
error[E0507]: cannot move out of `a.member` which is behind a shared reference
--> src/main.rs:12:19
|
12 | let member = &a.member?;
| ^^^^^^^^-
| |
| `a.member` moved due to this method call
| move occurs because `a.member` has type `Option<String>`, which does not implement the `Copy` trait
|
note: `branch` takes ownership of the receiver `self`, which moves `a.member`
--> /rustc/1065d876cdbc34a872b9e17c78caaa59ea0c94d4/library/core/src/ops/try_trait.rs:217:15
help: you can `clone` the value and consume it, but this might not be your desired behavior
|
12 | let member = &clone().a.member?;
| ++++++++
For more information about this error, try `rustc --explain E0507`.
Desired output
12 | let member = &a.member.clone()?;
or
12 | let member = a.member.as_ref()?;
Rationale and extra context
The suggestion for cloning the member that cannot be moved puts the clone()
in the wrong place, but only (in this example) when combined with the question mark operator. Without the question mark &a.member
compiles with no errors.
Other cases
No response
Anything else?
No response