What's the closest I can get to discriminating an enum by a char?

前端 未结 2 1006
小蘑菇
小蘑菇 2021-01-19 04:37

I\'ve written this question out many times, and have finally realized that my biggest problem is that I don\'t know how I want to represent this data, and that\'s making it

2条回答
  •  迷失自我
    2021-01-19 05:32

    Copying a &'static str (i.e. copying the reference only) has no cost. A deep copy of the string would be a clone and would be typed as a String.

    If &'static str is too verbose for you, you can always define a type alias.

    type Str = &'static str;
    

    HashMap corresponds nicely to your original map. However, if you don't need the full range of char for the key and you don't actually need to have the value typed as a char anywhere besides indexing the map, you should use an enum instead, as that will restrict the legal values that can be used as keys.

提交回复
热议问题