Hello,
I wanted to know if I could implement a Trait for a specific function (not a signature, as here)?
E.g., I would like to be able to define a derivative for an existing function
so that I can later call function.diff
to produce the derivative.
fn affine(x: isize) -> isize {
x * 4 + 2
}
pub trait Differentiable<Args> {
type Output;
fn diff(&self, args: Args) -> Self::Output;
}
impl Differentiable<()> for affine { // expected type, found function `crate::affine`
type Output = isize;
fn diff(&self, _args: (isize,)) -> Self::Output {
4
}
}
pub fn main() {
affine.diff((4,));
}
If Rust automatically implements Fn
trait on closures / functions, shouldn't there be some hidden type for each closure / function?