How can I have Custom comparator for Heaps?

Apart from Min & Max heaps in Rust can I give a custom comparator without creating a new Wrapper data type.
Sample java code which I want to achieve the same in Rust with very few lines of code.

Context to problem:

Given Initial capital investment, an array of capital investments & their corresponding profits in an array, I am using a greedy technique to pick the max profit with my capital in hand & make a profit out of it and make it the capital and so on... recursively till my limit of projects I can handle reaches.

public void maximumCapital(int[] capitals, int[] profits, int initialCapital, int maxPorjectsCanHandle) {
     int n = profits.length;
     // a custom comparator picking variables from closure
     PriorityQueue<Integer> minCapitalHeap = new PriorityQueue<>(n, 
                              (i1, i2) -> capitals[i1] - capitals[i2]);
     PriorityQueue<Integer> maxProfitsHeap = new PriorityQueue<>(n, 
                              (i1, i2) -> profits[i2] - profits[i1]);

How can I achieve the same in Rust? I thought of having a wrapper but how could I pass the capitals array? I cant be passing for every wrapper object creation like Capital(1, capitals) & create an Ord using those capitals array, I couldn't show my code since my Idea itself sucks.

binary-heap-plus has a heap type that can take a custom comparator.

If you want to implement something similar yourself, you could make your own heap type that uses a std::collections::BinaryHeap and a wrapper type internally, but automatically wraps/unwraps items when inserting/retrieving them.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.