voddle
voddle

Reputation: 49

How to get value from Reverse()?

I'm trying MinHeap from std::BinaryHeap like

use std::collections::BinaryHeap;
use std::cmp::Reverse;

let mut heap = BinaryHeap::new();

heap.push(Reverse(1));
heap.push(Reverse(5));
heap.push(Reverse(2));

assert_eq!(heap.pop(), Some(Reverse(1)));
assert_eq!(heap.pop(), Some(Reverse(2)));
assert_eq!(heap.pop(), Some(Reverse(5)));
assert_eq!(heap.pop(), None);

But allow varibles from heap.pop() or heap.peek() is Reverse() type, which is not easy to use, how to get value from it?

Upvotes: 4

Views: 614

Answers (1)

mhutter
mhutter

Reputation: 2916

From the docs we can see that Reverse is a tuple and its only field is public:

pub struct Reverse<T>(pub T);

This means we can access the field with .0 Like this:

let reversed = Reverse(42);
assert_eq!(reversed.0, 42);

Upvotes: 5

Related Questions