Open
Description
openedon Jul 25, 2023
Code
fn main() {
let string = String::from("foo");
let a = Some(string.clone());
let b = Some(&string);
if a == b {
println!("do stuff");
}
}
Current output
error[E0308]: mismatched types
--> src/main.rs:5:13
|
5 | if a == b {
| ^ expected `Option<String>`, found `Option<&String>`
|
= note: expected enum `Option<String>`
found enum `Option<&String>`
help: use `Option::cloned` to clone the value inside the `Option`
|
5 | if a == b.cloned() {
| +++++++++
Desired output
error[E0308]: mismatched types
--> src/main.rs:5:13
|
5 | if a == b {
| ^ expected `Option<String>`, found `Option<&String>`
|
= note: expected enum `Option<String>`
found enum `Option<&String>`
help: try using `.as_ref()` to convert `Option<String>` to `Option<&String>`
|
5 | if a.as_ref() == b {
| +++++++++
Rationale and extra context
This works as expected when a
and b
are swapped, but with this ordering, cloned
is suggested, which leads to an allocation/copy which will be immediately freed and is entirely avoidable.
Other cases
a
and b
swapped leads to the correct output.
Anything else?
Tested on nightly 2023-07-24 31395ec38250b60b380f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment