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 structVec<&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.