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
{{ message }}
Repository navigation
Calling and_then on Option<String> with a function that takes &str could provide a more helpful diagnostic #106342
fnproduces_string() -> Option<String>{Some("my cool string".to_owned())}fntakes_str(x:&str) -> Option<()>{Some(())}fnmain(){let x = produces_string().and_then(takes_str);}
The current output is:
error[[E0631]](https://doc.rust-lang.org/stable/error-index.html#E0631): type mismatch in function arguments
--> src/main.rs:10:40
|
5 | fn takes_str(x: &str) -> Option<()> {
| ----------------------------------- found signature defined here
...
10 | let x = produces_string().and_then(takes_str);
| -------- ^^^^^^^^^ expected due to this
| |
| required by a bound introduced by this call
|
= note: expected function signature `fn(String) -> _`
found function signature `for<'a> fn(&'a str) -> _`
note: required by a bound in `Option::<T>::and_then`
The solution is to change:
-let x = produces_string().and_then(takes_str);+let x = produces_string().as_deref().and_then(takes_str);
Activity
y21 commentedon May 14, 2023
@rustbot claim
Rollup merge of rust-lang#111659 - y21:suggest-as-deref, r=cjgillot
Rollup merge of rust-lang#111659 - y21:suggest-as-deref, r=cjgillot