Rust for C++

Generics

Rust
trait Hash {
    fn hash(&self) -> u64;
}

impl Hash for bool {
    fn hash(&self) -> u64 {
        if *self { 0 } else { 1 }
    }
}

impl Hash for i64 {
    fn hash(&self) -> u64 {
        *self as u64
    }
}

fn print_hash<T: Hash>(t: &T) {
    println!("The hash is {}", t.hash())
}

fn main() {
    print_hash(&true);      // instantiates T = bool
    print_hash(&12_i64);    // instantiates T = i64
}
Compiled
// The compiled code:
__print_hash_bool(&true);  // invoke specialized bool version directly
__print_hash_i64(&12_i64);   // invoke specialized i64 version directly