Guest User

Untitled

a guest
May 20th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. use std::collections::HashMap;
  2.  
  3. fn main() {
  4. let mut hm = HashMap::new();
  5. hm.insert(1, "test");
  6.  
  7. // This does not work
  8. if let Some(first) = hm.get_mut(&1) {
  9. // do things
  10. } else {
  11. match some_enum {
  12. SomeEnum::Foo(x) => { hm.insert(1, value); }
  13. SomeEnum::Bar(x) => { }
  14. }
  15. }
  16.  
  17. // this works but is a bit ugly
  18. /*
  19. let need_insert = {
  20. if let Some(first) = hm.get_mut(&1) {
  21. false
  22. } else { true }
  23. };
  24.  
  25. if need_insert {
  26. hm.insert(1, "test");
  27. }
  28. */
  29. }
Add Comment
Please, Sign In to add comment