Closed
Description
Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9d89810932e63366d4910a3d8cca200e
fn takes_option(_arg: Option<&String>) {}
fn main() {
takes_option(&None)
}
The current output is:
Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
--> src/main.rs:4:18
|
4 | takes_option(&None)
| ------------ ^^^^^ expected enum `Option`, found `&Option<_>`
| |
| arguments to this function are incorrect
|
= note: expected enum `Option<&String>`
found reference `&Option<_>`
note: function defined here
--> src/main.rs:1:4
|
1 | fn takes_option(_arg: Option<&String>) {}
| ^^^^^^^^^^^^ ---------------------
help: you can convert from `&Option<T>` to `Option<&T>` using `.as_ref()`
|
4 | takes_option(&None.as_ref())
| ~~~~~~~~~~~~~~
help: consider removing the borrow
|
4 - takes_option(&None)
4 + takes_option(None)
|
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
The error is correct, but I think the diagnostic is wrong. The second recommendation to call takes_option(None)
is sound, but the first one (&None.as_ref()
) seems like bad advice.
This reproduces on rustc
1.61 (on my workstation), as well as 1.63 (stable channel in playground), 1.64 (beta channel in playground), and 1.65 (nightly channel in playground).