Binding does not live long enough when storing a reference to a vector item in a hash map

徘徊边缘 提交于 2019-12-02 07:55:55

I pass a reference to each of the hash maps (so they borrow it) and then pass the option straight to the vector to move it there, so that the option doesn't go out of scope.

Once something is borrowed, you can't move it elsewhere.

If you place an element into a vector and borrow it from there, you can't mutate the vector until the borrow ends.

In other words your current approach won't work.

The simplest solution is probably to store indices into the vector in your hash maps.

Alternatively, it might be possible to design a fancy key that can be compared to both the short and long names, and then you can store the option directly in a single hash map. I say "might" because I'm not sure if this is currently possible.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!