I am just starting using rust and discovered the clippy assistant. I am generally very satisfied with its functionalities, but i encountered a recommendation which I do not know if I should follow. while working with a HashMap, I want to test if a stored value (say a f32) respects a condition (say it is greater than 10.). Also, I am not sure that the value is am looking for using its key is already in the HashMap. I wrote:
if mymap.contains_key(k) {
if *mymap.get(k).unwrap() > 10. {
// do something
}
}
Then clippy politely asks me to collapse this if statement into one:
help: collapse nested if block
|
3 | if mymap.contains_key(k) && *mymap.get(k).unwrap() > 10. {
4 | // do something
5 | }
|
I feel that collapsing this would bring a chance of *mymap.get(k).unwrap() > 10.
being evaluated before mymap.contains_key(k)
. If so, and if the key is not already in mymap
, this would generate an error.
I suppose there exists more elegant ways to do what I am trying to do with combinations of if let
or so, but if I wanted to keep my if
s, would it be possible to collapse them, and in which order would the conditions be evaluated ?