The second map
statement in this code fails to compile.
fn main() {
let hello = Some("hello");
let _switcheroo = hello.map(str::to_string); //ok
let hello = Some("hello".to_string());
let _switcheroo = hello.map(String::as_str); //not ok
}
The error is:
error[E0631]: type mismatch in function arguments
--> src/main.rs:6:29
|
6 | let _switcheroo = hello.map(String::as_str); //not ok
| ^^^
| |
| expected signature of `fn(std::string::String) -> _`
| found signature of `for<'r> fn(&'r std::string::String) -> _`
I would expect an error about borrowing some moved data. What is this error trying to say?
This does compile:
let hello = Some("hello".to_string());
let _switcheroo = hello.as_ref().map(String::as_str); //ok