-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clippy suggests: more directly with map_or_else(g, f) instead... recommendation does not compile #4144
Comments
This looks like you want to do this: let count = frequencies.entry(word).or_insert(0);
*count += 1; https://doc.rust-lang.org/std/collections/hash_map/struct.HashMap.html#method.entry |
Thank you for the pointer. I compiled the code to confirm that unfortunately the suggestion does not solve the issue. The motivation of the code as I have it, is to avoid instantiating a |
You could try to do this with if freq.contains_key(word) {
let count = freq.get_mut(word).unwrap();
*count += 1;
} else {
freq.insert(word.to_string(), 1);
} |
Thank you. This was useful. At this point only as a potentially useful FYI, the latest code executes consistently about 5-10% slower than the ugly syntax I was using. This experience has likely fallen into the scope of the methods provided by |
Yeah iterators can be optimized more efficiently, than if-else-blocks. Have you tested the performance of the |
Yes, I tried the code using |
What about this?
It should perform as the same as IMO, it's better style to use |
@mikerite - Your latest code performs as quickly as the original code I performed. The syntax is much nicer. Thank you! FYI: I agree with you re-style of |
Fix .map(..).unwrap_or_else(..) bad suggestion Closes #4144
Great tool! Thank you.
The code where clippy suggests I use a more direct approach:
My interpretation of the suggestion:
The caveat of course is that there is an easy fix to the error I'm receiving.
Notwithstanding, my first guess is that there is an inconsistency here:
map_or_else
should work the same way as my first iteration of the code all the time.I hope this is helpful.
- E
The text was updated successfully, but these errors were encountered: