I was banging my head in the wall for the last 2 days, but I still cannot figure out a good way to do this in Rust, as I think the solution is a lot different that in other languages.
This is a non-working example of what I am trying to achieve. In general I have a struct holding data from user input from in my case - infinite loop(although the example is with a
for I simplified it), and I want to use the Strings from the Struct and construct vertices
(exchange: String/&str, from: String/&str) and
(exchange: String/&str, to: String/&str) and connect them with edges. I know this is not possible, since
String doens’t have
Copy and I had many issues related to
&str and lifetimes, but also I want to own the
String in the struct. One way I started thinking is to have a
Vec with the vertices and use their index as well as a
HashMap holding the vertices as a key and the index as a value, so I can also be able to check if the vertices exist already so I can update them, as the example is not full and the struct is missing the Edges’ weights.
So my question is: What approaches can I take to ensure I can do this, but also what are my options in general dealing with such cases, as this is not the first time I bump into this issue.
I also checked
Box, but it I am also not quite sure when should I use these smart pointers at the moment. So guides towards a bit of information about what cases they are used in, is also welcome, as I am new the strictly-typed and system programming languages.
Thank you in advance!