To find the values of a HashMap
in Rust, you can use the get
method provided by the standard library. This method takes a key as an argument and returns an Option<&V>
, where V
is the type of values stored in the map. If the map contains the key, get
returns Some(&V)
with a reference to the value, otherwise it returns None
.
Here's an example code snippet that shows how to find the values in a HashMap
:
main.rs392 chars16 lines
In this example, we create a HashMap
, insert some key-value pairs, and then check if the key "two" exists in the map using the contains_key
method. If the key exists, we retrieve the associated value using the get
method and unwrap the Option
using unwrap
method to get the actual value. Finally, we print the value to the console.
gistlibby LogSnag