To find the smallest value in a map in Rust, you can iterate over the key-value pairs in the map and use the min_by_key
method to find the minimum value.
Here's an example:
main.rs324 chars16 lines
In this example, we create a new HashMap
and insert some key-value pairs. We then use the iter
method to iterate over the key-value pairs in the map. We pass a closure to the min_by_key
method that extracts the value from each key-value pair, and returns the minimum value.
The result of min_by_key
is an Option
that may be empty if the map is empty. We unwrap the Option
to get the smallest value, and print it to the console.
If you run this code, you should see the following output:
main.rs35 chars2 lines
gistlibby LogSnag