Simpler way to get next number?

Is there a simpler way to accomplish this?

The idea is I want to get the next “id” which is some number (could be u32, usize, etc.), out of an Iterator… here’s what I’ve got so far but the type signature looks scary and I feel like there’s probably an easier/better way?

use std::collections::HashMap;
use std::ops::Add;
use num::One;
use num::Zero;
use std::iter::Iterator;

fn get_next_ord<'a, K: 'a>(values: impl Iterator<Item = &'a K>) -> K 
where K: PartialOrd + Add + Zero + One + Clone
    values.fold(None, |highest, x| {
        let value = x;
            match highest {
                None => value,
                Some(h) => {
                    if value >= h {
                    } else {
    .map_or(K::zero(), |x| x.clone() + K::one())


fn main() {

    let mut foo:HashMap<u32, String> = HashMap::new();
    foo.insert(2, "2".to_owned());
    foo.insert(1, "1".to_owned());
    foo.insert(3, "3".to_owned());
    foo.insert(5, "5".to_owned());
    foo.insert(4, "4".to_owned());
    println!("{}", get_next_ord(foo.keys())); //6
    let bar:Vec<&u32> = Vec::new();
    println!("{}", get_next_ord(bar.into_iter())); //0


Have you checked the Iterator::max()?

1 Like

Thanks - wasn’t aware of that!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.