Hide generic arguments which can be inferred

I have a function like this:

fn test<F: FnOnce()>(f: F) {
    f()
}

and I can call this without offering any generic argument:

test(|| {
        println!("It works.")
    })

And now I change the function to this:

fn test<const C: bool, F: FnOnce()>(f: F) {
    f()
}

And now I have to offer both generic arguments, even though the type of F can be inffered:

fn main() {
    test::<true, _>(|| {
        println!("It works.")
    })
}

It doesn't look nice to me, and I barely saw any API like this. So is there anyway I can write like this (without the _), or any kind that allow me to offer only one argument:

test::<true>(|| {
        println!("It works.")
    })

?

Since 1.63 you can make them impl FnOnce() and the user will be able to specify the other parameters.

5 Likes

Thanks to @chrefr, the code can be written like this:

fn test<const C: bool>(f: impl FnOnce()) {
    f()
}


fn main() {
    test::<true>(|| {
        println!("It works.")
    })
}