-1

Preface: bloody rust beginner coming from Java.

I have this struct Foo:

#[derive(Debug)]
#[derive(PartialEq, Eq, Hash)]
struct Foo {
    typus: u32,
    value: u32
}

At some point in my code, I create permutations of a Vec<Foo> using itertools::Itertools. These permutations I want to store in a HashMap<Vec<Foo>, u32>. This is the code:

fn combinations(values: Vec<Foo>, maximum_value: u32) -> Vec<Vec<Foo>>{
    let mut result: HashMap<Vec<Foo>, u32> = HashMap::<Vec<Foo>, u32>::new();
    
    for i in 0..values.len() {
        values.iter().into_iter().permutations(i).unique().into_iter().for_each(|perm| {
            let sum: u32 = perm.into_iter().map(|&x|x.value).sum();
            if sum <= maximum_value {
                result.insert(perm, maximum_value-sum);
            }
        });
    }

    filter(result)
}

On this line

result.insert(perm, maximum_value-sum);

it throws an error saying that perm is Vec<&Foo> but the HashMap I defined expects Vec<Foo>.

expected struct Foo, found &Foo

expected struct Vec<Foo> found struct Vec<&Foo>

How can I convert to reference to a value? In general I am having issues with borrows, reference and ownership, as I didn't have to bother about it in Java.

XtremeBaumer
  • 5,619
  • 2
  • 16
  • 54
  • Are you sure using `Vec` as a key for a `HashMap` is the correct way to solve your problem? Seems dubious – Finomnis May 19 '22 at 15:03
  • 1
    In general I already had the "problem" solved, as I was just porting the code from java to rust to improve with rust. There might be better ways to solve it, but for me this worked already – XtremeBaumer May 19 '22 at 15:05

0 Answers0