I want to access the element next to the maximal one in a Vec<i32>
. I'm looking for something like this:
let v = vec![1, 3, 2];
let it = v.iter().max_element();
assert_eq!(Some(&2), it.next());
In C++, I would go with std::max_element
and then just increase the iterator (with or without bounds checking, depending on how adventurous I feel at the moment). The Rust max
only returns a reference to the element, which is not good enough for my use case.
The only solution I came up with is using enumerate
to get the index of the item - but this seems manual and cumbersome when compared to the C++ way.
I would prefer something in the standard library.
This example is simplified - I actually want to attach to the highest value and then from that point loop over the whole container (possibly with cycle()
or something similar).