Here is a Godbolt link: https://godbolt.org/z/q9bsnMn7q
And here is what I am trying to solve:
use std::ops::{Add, Sub};
fn f(a: i32, b: i32, binop: &dyn Fn(i32, i32) -> i32) -> i32 {
binop(a, b) as i32
}
fn main() {
// Examples using Sub::sub and Add::add
println!("{}", f(1, 2, &Add::add));
println!("{}", f(1, 2, &Sub::sub));
// This works (but seems non-idiomatic) compared to above
let eq1 = |a, b| if a == b {1} else {0};
let eq2 = |a, b| (a == b).into();
println!("{}", f(1, 2, &eq1));
println!("{}", f(1, 2, &eq2));
// Is there something like this?
// println!("{}", f(1, 2, &PartialEq::eq as i32));
}