W3cubDocs

/Rust

Error code E0631

This error indicates a type mismatch in closure arguments.

Erroneous code example:

fn foo<F: Fn(i32)>(f: F) {
}

fn main() {
    foo(|x: &str| {});
}

The error occurs because foo accepts a closure that takes an i32 argument, but in main, it is passed a closure with a &str argument.

This can be resolved by changing the type annotation or removing it entirely if it can be inferred.

fn foo<F: Fn(i32)>(f: F) {
}

fn main() {
    foo(|x: i32| {});
}

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/error_codes/E0631.html