• 3 Posts
  • 23 Comments
Joined 1 year ago
cake
Cake day: June 4th, 2023

help-circle
rss













  • You’re trying to iterate over a Vec while mutating its contents in other places, which is something the borrow checker doesn’t like. Altough the later cards that get their copies count increased aren’t the same as the iterating reference card, Rust forbids two mutable references into the same Vec, even if they reference different elements of the Vec.

    You could try to iterate over indices into the array instead of directly over array elements, then you get rid of the reference in the outer loop. This would probably require the least change to your code.

    Another option would be to split apart the data structures between what needs to be mutated and what doesn’t. I’ve solved this puzzle in Rust and had a separate mutable Vec for the number of copies of each card. Then you can iterate over and mutate both Vecs separately without having conflicting references.