Can Rust Ignore "Cannot Infer Type" In This Case?

Is there any way to get this to work without having to annotate a type I'm not using?

fn main() {

fn test<C: FnMut()>(callback: Option<C>) {
    if let Some(c) = callback {



   Compiling playground v0.0.1 (/playground)
error[E0282]: type annotations needed
 --> src/
2 |     test(None);
  |     ^^^^ cannot infer type for type parameter `C` declared on the function `test`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

No, but you can fill it in with a function pointer.


One reason why Rust can ignore the type parameter is because it needs to generate the code to call the function even if it never happens. This is because at the time of codegen, it doesn't know which branch will be taken.


Fair enough. Thanks. :+1:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.