Help with [E0599] when implementing trait

⚓ rust    📅 2025-06-11    👤 surdeus    👁️ 1      

surdeus

I am trying to implement a fixed size hash map with DoubleEndedIterator and Index traits in order to learn Rust concepts of traits, iterators, ownership etc. (p.s. it's toy code!!) and have a error I can't see to get around.

pub struct FixedSizeHashMap <K, V, const C: usize>  {
    _data: Vec<Option<Entry<K, V>>>,
    _size: usize,

   //... ommited .. code
}

#[guard(is_prime(C as u64))]
#[guard(C <= 25013)]
impl <K: Hash + std::cmp::Eq, V, const C: usize> FixedSizeHashMap <K, V, C>  {
    // ... ommited code ....
    pub fn get(&self, key: &K) -> Option<&V> {
        let i = self._find_index(&key);
        match self._get_at(i) {
            Some(key_val_pair) => Some(key_val_pair.1),
            None => None
        }
    }
    // ... omited code
}

impl <K: Hash + std::cmp::Eq, V, const C: usize> Index<&K> for FixedSizeHashMap <K, V, C> {
    type Output = V;
    fn index(&self, key: &K) -> &Self::Output {
        self.get(key).except("not found in map") //!!! <<= COMPILER ERROR HERE!!
    }
}

I get a compiler error in my fn index:

error[E0599]: no method named `get` found for reference `&hash_map::FixedSizeHashMap<K, V, C>` in the current scope
   --> libs/hash_map/src/hash_map.rs:220:14
    |
220 |         self.get(key).expect("Panic! not in map")
    |              ^^^
    |
    = help: items from traits can only be used if the trait is implemented and in scope
    = note: the following trait defines an item `get`, perhaps you need to implement it:
            candidate #1: `SliceIndex`
  • The get method is pub and works
  • I am able to access _data, _usize etc from impl Index block.
  • Similar situation for or there impl Iterator block as well.
  • My implementation is similar to std::HashMap so I think I'm doing something stupid.
  • It maybe something to do with ... candidate #1: ``SliceIndex`` and requires some where bound to be satisfied but I can't figure out.

Any help appreciated!!!

7 posts - 2 participants

Read full topic

🏷️ rust_feed