E0277 - Trait Bound Not Satisfied

38 Views Asked by At

I am trying to use the most simple methods on a HashMap.

error[E0277]: the trait bound `f32: Eq` is not satisfied
   --> src/exercises/mode_and_median.rs:19:15
    |
19  |         track.entry(*item).or_insert(track.get(*item).unwrap());
    |               ^^^^^ the trait `Eq` is not implemented for `f32`
    |
    = help: the following other types implement trait `Eq`:
              isize
              i8
              i16
              i32
              i64
              i128
              usize
              u8
            and 4 others
note: required by a bound in `HashMap::<K, V, S>::entry`
   --> /home/anish/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:732:8
    |
732 |     K: Eq + Hash,
    |        ^^ required by this bound in `HashMap::<K, V, S>::entry`
...
852 |     pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
    |            ----- required by a bound in this associated function

error[E0277]: the trait bound `f32: Hash` is not satisfied
   --> src/exercises/mode_and_median.rs:19:15
    |
19  |         track.entry(*item).or_insert(track.get(*item).unwrap());
    |               ^^^^^ the trait `Hash` is not implemented for `f32`
    |
    = help: the following other types implement trait `Hash`:
              isize
              i8
              i16
              i32
              i64
              i128
              usize
              u8
            and 4 others
note: required by a bound in `HashMap::<K, V, S>::entry`
   --> /home/anish/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:732:13
    |
732 |     K: Eq + Hash,
    |             ^^^^ required by this bound in `HashMap::<K, V, S>::entry`
...
852 |     pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
    |            ----- required by a bound in this associated function

error[E0599]: the method `get` exists for struct `HashMap<f32, _>`, but its trait bounds were not satisfied
  --> src/exercises/mode_and_median.rs:19:44
   |
19 |         track.entry(*item).or_insert(track.get(*item).unwrap());
   |                                            ^^^ method cannot be called on `HashMap<f32, _>` due to unsatisfied trait bounds
   |
   = note: the following trait bounds were not satisfied:
           `f32: Eq`
           `f32: Hash`

Some errors have detailed explanations: E0277, E0599. For more information about an error, try rustc --explain E0277. error: could not compile learning (bin "learning") due to 3 previous errors

Note --> I am trying to implement mode, and the code is still incomplete.

fn mode(vec: &Vec<f32>) -> f32 {
    let mode;
    
    let mut track: HashMap<f32, usize> = HashMap::new();

    for item in vec {
        track.entry(*item).or_insert(track.get(*item).unwrap());
    }

    return mode;
}

I tried everything I could but nothing worked.

0

There are 0 best solutions below