Guest User

Untitled

a guest
May 20th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 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) => {
  13. let value = get_value(x);
  14. hm.insert(1, value);
  15. }
  16. SomeEnum::Bar(_) => { }
  17. }
  18. }
  19.  
  20. // this works but is a bit ugly
  21. /*
  22. let need_insert = {
  23. if let Some(first) = hm.get_mut(&1) {
  24. false
  25. } else { true }
  26. };
  27.  
  28. if need_insert {
  29. hm.insert(1, "test");
  30. }
  31. */
  32. }
Add Comment
Please, Sign In to add comment