Function rust_examples::dispatch::gradient_descent_static[][src]

pub fn gradient_descent_static<F>(f: &F, max_iters: usize, eta: f64) -> f64 where
    F: Differentiable
Expand description

Gradient Descent that finds a minimum of a statically defined function f on given interval.

Static dispatch means that this function i monomorphized and thus the type of f is known at compilation time. Monomorphization means that the compiler duplicates this function for every type implementing Differentiable and for each copy substitutes the concrete type for the generic parameter F.

Call f.grad(x) is direct and the method address is explicitly mentioned in the assembly code. Furthermore, trait implementations can benefit from inlining.

There’s, however, also a disadvantage - one can’t put different implementations into let’s say a collection (e.g. Vec) that expects homogeneous types. Such a structure need a different kind
of polymorphism which is provided by dynamic dispatch.