相关疑难解决方法(0)

从HashMap获取第一个元素

我有一个HashMap并且需要获得第一个元素:

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() {}
Run Code Online (Sandbox Code Playgroud)

但代码不编译:

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
  | …
Run Code Online (Sandbox Code Playgroud)

iterator hashmap rust

3
推荐指数
2
解决办法
1166
查看次数

标签 统计

hashmap ×1

iterator ×1

rust ×1