I'm sure there is a better way to do this, but I do not know how to extract structure from Enum during reduce(). Assume ALL enums are identical in vector (but prob. solution needs to handle if this is not true also).
Simplest example:
#[derive(Debug)]
struct Data {
d: u64,
}
#[derive(Debug)]
enum DataEnum {
First(Data),
Second,
}
fn main() {
let v = vec![DataEnum::First(Data{d: 10}), DataEnum::First(Data{d: 5}), DataEnum::First(Data{d: 15}), DataEnum::First(Data{d: 13})];
let mut vi = v.iter();
let mut current = vi.next().unwrap();
let mut max: u64;
match current {
DataEnum::First(data) => max = data.d,
_ => panic!("dont know what to do with {:?}", current),
}
for next in vi {
match next {
DataEnum::First(data) => if max < data.d {
current = next;
max = data.d;
},
_ => panic!("dont know what to do with {:?}", next),
}
}
println!("found max: {:?}", current);
}
Is there a better way to do this with fold or reduce?
Problem I have with reduce() is that I don't know how to handle the 'match' part inside closure properly (to extract and compare data.d).
max_by_key.#[derive(PartialOrd, PartialEq, Ord, Eq)]and.iter().max().unwrap()or.into_iter().max().unwrap()?