fork(1) download
  1. use std::collections::HashMap;
  2. use std::io::{self, Error, ErrorKind};
  3.  
  4. pub struct A {
  5. x: i32,
  6. }
  7.  
  8. pub struct B {
  9. a: u32,
  10. }
  11.  
  12. impl std::hash::Hash for A {
  13. fn hash<H>(&self, state: &mut H) -> Result<(), io::Error>
  14. where
  15. H: std::hash::Hasher,
  16. {
  17. if self.x == 5 {
  18. return Error::new(ErrorKind::Other, "oh no!");
  19. }
  20. state.write_i32(self.x);
  21. state.finish();
  22. }
  23. }
  24.  
  25. impl PartialEq for A {
  26. fn eq(&self, other: &A) -> bool {
  27. self.x == other.x
  28. }
  29. }
  30.  
  31. impl Eq for A {}
  32.  
  33. fn main() {
  34. let mut map = HashMap::new();
  35. map.insert(
  36. A {
  37. x: 10,
  38. },
  39. B { a: 1 },
  40. );
  41. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
error[E0053]: method `hash` has an incompatible type for trait
  --> prog.rs:13:41
   |
13 |     fn hash<H>(&self, state: &mut H) -> Result<(), io::Error>
   |                                         ^^^^^^^^^^^^^^^^^^^^^
   |                                         |
   |                                         expected `()`, found enum `Result`
   |                                         help: change the output type to match the trait: `()`
   |
   = note: expected fn pointer `fn(&A, &mut H)`
              found fn pointer `fn(&A, &mut H) -> Result<(), std::io::Error>`

error[E0308]: mismatched types
  --> prog.rs:18:18
   |
13 |     fn hash<H>(&self, state: &mut H) -> Result<(), io::Error>
   |                                         --------------------- expected `Result<(), std::io::Error>` because of return type
...
18 |           return Error::new(ErrorKind::Other, "oh no!");
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |                  |
   |                  expected enum `Result`, found struct `std::io::Error`
   |                  help: try using a variant of the expected enum: `Err(Error::new(ErrorKind::Other, "oh no!"))`
   |
   = note: expected enum `Result<(), std::io::Error>`
            found struct `std::io::Error`

error[E0308]: mismatched types
  --> prog.rs:13:41
   |
13 |     fn hash<H>(&self, state: &mut H) -> Result<(), io::Error>
   |        ----                             ^^^^^^^^^^^^^^^^^^^^^ expected enum `Result`, found `()`
   |        |
   |        implicitly returns `()` as its body has no tail or `return` expression
   |
   = note:   expected enum `Result<(), std::io::Error>`
           found unit type `()`

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0053, E0308.
For more information about an error, try `rustc --explain E0053`.
stdout
Standard output is empty