2

I am pretty new to rust, so I have a bit of trouble with the borrow checker. I have a json object which is a vector of the form: Vec<(String, Option<char>, String)>. I want to be able to convert this vector into a hash map in order to allow for quick access of each element. Originally, each tuple was unique, and the user would pass a pair of a string and a char, so I had a hashmap of which was of the form HashMap<(String, Option<char>), String>. The conversion function was just an iteration over the original vector passed as a json object like so:

let new_transition_function: Vec<(String, Option<char>, String)> = origin
    .transition_function
    .iter()
    .map(|(x, y, z)| ((x.to_owned(), y.to_owned()), z.to_owned()))
    .collect();

However, when the third element is non-unique, the value is jsut replaced in the hashmap, when I need to keep each value. How can I most efficiently and idiomatically transform the old vector of Vec<(String, Option<char>, String)> to a HashMap<(String, Option<char>), Vec<String>)>?

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
Roquentin
  • 177
  • 2
  • 12
  • 2
    It does not, thank you though. That question was asking what data structure they should use, when I already know which data structure I want to use. I am asking how can I convert that json object into my desired data structure. However, their solution and mine are identical, so that is neat. – Roquentin Jan 06 '20 at 06:47
  • 1
    Yes, but they also cover the 2 main ways to accomplish this. 1) Using a `HashMap>` and using that with the `Entry` API or 2) Using the [`multimap`](https://crates.io/crates/multimap) crate – sshashank124 Jan 06 '20 at 06:48
  • 1
    Ah, I see. Is there any way to create a multi map using the built in .iter() .map() and .collect() functions? I don't see too much documentation for it, but I can peek into the multimap source to check. Thank you for the help – Roquentin Jan 06 '20 at 06:53
  • 1
    If there isn't, you could always just replace the `map` with a `for_each` and call `insert` for each entry – sshashank124 Jan 06 '20 at 06:58
  • Yeah, but that would require for it to be mutable and not purely functional, which is not best practice from what I understand. That was my first thought as well, coming from C/C++. However, after looking through the source, I saw that there is an extension for serde, which is the json serialization library I am using, so that circumvents my problem entirely. Thank you very much. If you make that a full answer I will pick it. – Roquentin Jan 06 '20 at 07:04
  • 1
    That's good. But do keep in mind that simply having some mutability in your code does not necessarily make it *worse/bad-practice*. There is no need to create a new answer here. The best option would be for you to accept the duplicate vote since the already existing answer is quite good and future readers can benefit from it – sshashank124 Jan 06 '20 at 07:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205411/discussion-between-roquentin-and-sshashank124). – Roquentin Jan 06 '20 at 07:09
  • [How do I create a map from a list in a functional way?](https://stackoverflow.com/a/30441736/155423) covers the case where you want to use `collect` still. – Shepmaster Jan 06 '20 at 16:07

0 Answers0