Rust, abstracted: specialization

What is specialization?

Specialization in Rust is a proposed feature that is already ad-hoc implemented for several standard library functions. Specialization provides a type of polymorphism that is common in other languages such as C++, wherein it is often referred to more generally as "subtype polymorphism".

How do I use specialization?

General specialization is currently available as a nightly only feature in Rust. To enable it we must add "#![feature(specialization)]" to the top of our project files.

#![feature(specialization)]

trait Dispatch {
   fn do(s: u64) -> u64;
}

impl Dispatch for u64 {
   fn do(s: u64) -> u64 {
      1
   }
}

impl<T: MyTrait> Dispatch for T {
   fn do(s: u64) -> u64 {
      2
   }
}