Dont know the reason ? may be a bug in compiler ? please explain

pub(crate) fn compute<F>(accept: F, s: &str) -> (&str, &str) 
where F: Fn(char) -> bool
    let till = s
        .find_map(|(idx, val)| if accept(val) { None } else { Some(idx) })

    let extracted = &s[..till];
    let rest = &s[till..];

    (rest, extracted)

this code compiles successfully on rust playground but getting error on my system

this code gives an error like this "unexpected argument of type char , help: remove the extra argument " when i invoke accpet(with char val)

but this same code is running successfully on rust playground

am using latest version of cargo
am using rust stable-x86_64-pc-windows-msvc
please explain this error

Your code, as posted here, compiles without error here using 1.77.0-nightly.

I think you have to show how you are calling this.

It's better to present all the code that is causing trouble and all of any error message it produces.

Have you checked that the one number given in the error message is actually in the function you show or elsewhere in your program?


here is the code

And when you run your playground snippet on your machine, what error do you get, exactly? Could copy and paste it here, please?


You might have written

where F: Fn() -> bool

on you end and overlooked the difference.

Note that you did not share the error message. Just the additional information and "help" text. The full error would look like this:

error[E0057]: this function takes 0 arguments but 1 argument was supplied
 --> src/
6 |         .find_map(|(idx, val)| if accept(val) { None } else { Some(idx) })
  |                                   ^^^^^^ ---
  |                                          |
  |                                          unexpected argument of type `char`
  |                                          help: remove the extra argument
note: callable defined here
 --> src/
2 | where F: Fn() -> bool,
  |          ^^^^^^^^^^^^ 

pointing out even the relevant trait bound. Additionally, that's not the only error then either, as the code using compute would also error.

error[E0593]: closure is expected to take 0 arguments, but it takes 1 argument
  --> src/
16 |     compute(|c| c.is_ascii_digit(), s)
   |     ^^^^^^^ --- takes 1 argument
   |     |
   |     expected closure that takes 0 arguments

error[E0593]: closure is expected to take 0 arguments, but it takes 1 argument
  --> src/
29 |     compute(|c| c == ' ', s)
   |     ^^^^^^^ --- takes 1 argument
   |     |
   |     expected closure that takes 0 arguments 

When posting questions about Rust error messages, always use the terminal (cargo build) to get the full error message.

As for why this error could appear in case your code doesn't contain the mistake anymore: Maybe you didn't save your code. If you didn't save, then your text editor might keep pointing out old errors from previous cargo check runs it did. If you haven't enabled to re-check your code on each save, the error might even be displayed for longer, even if you save. If you save your code, and then invoke cargo in the terminal, then you can be relatively sure that it's nothing on your end that you're doing wrong. To be even more sure no error is cached, even though that shouldn't happen usually (so if that's the only thing that helps, there might actually be a bug in Rust's tooling), you could even run cargo clean and then test again if the error message still appears on cargo check or cargo build.


I solved this issue by commenting some code that i really don't need .
am sorry as i forgot to check another file that was causing this issue.

Your screenshot of the error message shows Fn() -> bool, but the screenshot of the source code shows Fn(bool) -> bool. Did you forget to save before running cargo build?

Edit: You already solved your issue.


actually i defined this function in another file without declaring parameter type in Fn() , that is the reason why am getting that error.

thanks for you valuable time in finding the issue. :slightly_smiling_face:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.