Skip to content

Instantly share code, notes, and snippets.

@9999years
Created March 15, 2019 16:27
Show Gist options
  • Save 9999years/f1b7ad4e02c504fc43bc0843d59301c9 to your computer and use it in GitHub Desktop.
Save 9999years/f1b7ad4e02c504fc43bc0843d59301c9 to your computer and use it in GitHub Desktop.
Build fails when using type parameter for function in Rust
// this compiles
fn validator<F, T>(f: F) -> Box<Fn(String) -> Result<(), String> + 'static>
where F: Fn(String) -> Result<T, String> + 'static {
Box::new(move |s| f(s).map(|_| ()))
}
// this fails with:
// error[E0308]: mismatched types
// --> src\main.rs:17:14
// |
// 17 | Box::new(move |s| f(s).map(|_| ()))
// | ^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter, found closure
// |
// = note: expected type `G`
// found type `[closure@src\main.rs:17:14: 17:39 f:_]`
//
// error: aborting due to previous error
//
// For more information about this error, try `rustc --explain E0308`.
fn validator_bad<F, G, T>(f: F) -> Box<G>
where F: Fn(String) -> Result<T, String> + 'static,
G: Fn(String) -> Result<(), String> + 'static {
Box::new(move |s| f(s).map(|_| ()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment