从HashMap获取第一个元素 [英] Get first element from HashMap

查看:522
本文介绍了从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,
无=> -1,
};
ret


fn main(){}

但是代码无法编译:


$ b

  error [E0308]:match arms have不兼容的类型
- > src / main.rs:5:15
|
5 |让ret =匹配it.next(){
| _______________ ^
6 | |一些(x)=> x,
7 | |无=> -1,
8 | | };
| | _____ ^预期的元组,找到整数变量
|
= note:预期类型`(& u16,& u8)`
找到类型`{integer}`
注意:匹配类型不兼容
- > ; src / main.rs:7:17
|
7 |无=> -1,
| ^^

我该如何解决它?

解决方案

HashMap 没有关于订单的保证如果顺序很重要,那么也许你可以切换到 BTreeMap ,它保留了基于如果你只需要得到你遇到的第一个值,换句话说就是任何 值,你可以做类似于你原来的代码:创建一个迭代器,只取第一个值:

  fn get_first_elem(idx :VarIdx) - > i16 {
match idx.values()。next(){
Some(& x)=> x as i16,
None => -1,
}
}

方法 values()只是创建一个迭代器。你错误的原因是 iter()会在一对键和值上创建一个迭代器,这就是为什么你得到了错误expected tuple

为了编译它,我必须改变其他一些东西: -1 不是有效的 u16 值,所以必须变成 i16 ,而你的值是 u8 因此必须转换为 i16



作为另一个一般评论,返回 -1 来表示失败不是很生锈。这就是 Option 用于和,假设 next()已经返回一个 Option ,这很容易实现:

  fn get_first_elem(idx:VarIdx) - >选项< U8> {
idx.values()。cloned()。next()
}



<为了将迭代器的& u8 值转换为<$>,需要 .cloned() c $ c> u8


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?

解决方案

There is no such thing as the "first" item in a HashMap. There are no guarantees about the order in which the values are stored nor the order in which you will iterate over them.

If order is important then perhaps you can switch to a BTreeMap, which preserves order based on the keys.

If you just need to get the first value that you come across, in other words any value, you can do something similar to your original code: create an iterator, just taking the first value:

fn get_first_elem(idx: VarIdx) -> i16 {
    match idx.values().next() {
        Some(&x) => x as i16,
        None => -1,
    }
}

The method values() creates an iterator over just the values. The reason for your error is that iter() will create an iterator over pairs of keys and values which is why you got the error "expected tuple".

To make it compile, I had to change a couple of other things: -1 is not a valid u16 value so that had to become i16, and your values are u8 so had to be cast to i16.

As another general commentary, returning -1 to indicate failure is not very "Rusty". This is what Option is for and, given that next() already returns an Option, this is very easy to accomplish:

fn get_first_elem(idx: VarIdx) -> Option<u8> {
    idx.values().cloned().next()
}

The .cloned() is needed in order to convert the &u8 values of the iterator into u8.

这篇关于从HashMap获取第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆