I have a HashMap and need to get the first element:
type VarIdx = std::collections::HashMap<u16, u8>; fn get_first_elem(idx: VarIdx) -> u16 { let it = idx.iter(); let ret = match it.next() { Some(x) => x, None => -1, }; ret } fn main() {} but the code doesn't compile:
error[E0308]: match arms have incompatible types --> src/main.rs:5:15 | 5 | let ret = match it.next() { | _______________^ 6 | | Some(x) => x, 7 | | None => -1, 8 | | }; | |_____^ expected tuple, found integral variable | = note: expected type `(&u16, &u8)` found type `{integer}` note: match arm with an incompatible type --> src/main.rs:7:17 | 7 | None => -1, | ^^ how can I fix it?
HashMap::iterhas a tiny amount of documentation that explains all of your problems: "An iterator visiting all key-value pairs in arbitrary order. The iterator element type is(&'a K, &'a V)."