[SOLVED] Is rust support to impl Generics trait for a special type not trait bound?

like is

use std::collections::BTreeMap;
type ID = [u8; 20];

// Not general form
// trait Close<V> {
//     fn get_closet(&self, key :ID) -> Option<&V>;
// }

// impl<V> Close<V> for  BTreeMap<ID, V> {
//     fn get_closet(&self, key:ID) -> Option<&V> {
//         self.get(&key)
//     }
// }
   
// general form
trait Close<K, V> {
    fn get_closet(&self, key :K) -> Option<&V>;
}

impl<K, V> Close<K, V> for  BTreeMap<K, V>
    where K = ID {              //should be any K: Ord; but just want impl for ID now; 
    fn get_closet(&self, key:K) -> Option<&V> {
        self.get(&key)
    }
}

You can't use equality on a type parameter like that because that would mean the same thing as not having the type parameter at all. Just do this:

impl<V> Close<ID, V> for  BTreeMap<ID, V> {
    fn get_closet(&self, key: ID) -> Option<&V> {
        self.get(&key)
    }
}

Close still takes two parameters in its definition, but the implementation here takes only one.

thanks for your warmhearted help;

1 Like