You can use the get()
method provided by the HashMap
to get the value associated with a certain key. Here's an example:
main.rs569 chars24 lines
In this example, we create a new HashMap
and insert three key-value pairs into it. Then, we define a search_value
variable, which is the value we want to find the key for. We use the iter()
method to iterate over all the key-value pairs in the map. For each pair, we check if the value matches our search_value
. If it does, we return the key as a Some
value. If it doesn't, we return None
. We use the find_map()
method to find the first key for which our closure returns a Some
value. Finally, we print out the result.
gistlibby LogSnag