Guest User

Untitled

a guest
Oct 15th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #![allow(unused)]
  2. fn main() {
  3. use std::collections::HashMap;
  4.  
  5. let mut map: HashMap<[i64; 2], String> = HashMap::new();
  6. map.insert([1,1], String::from("1x1"));
  7. map.insert([1,2], String::from("1x2"));
  8. map.insert([1,3], String::from("1x3"));
  9.  
  10. let str = "13";
  11.  
  12. // Ugly as hell - how do I make this more rust-like?
  13. let a = cast_to_i64(str.chars().nth(0));
  14. let b = cast_to_i64(str.chars().nth(1));
  15.  
  16. if a.is_none() || b.is_none() {
  17. print!("nothing is good");
  18. return;
  19. }
  20.  
  21. if let Some(x) = map.get_mut(&[a.unwrap(),b.unwrap()]) {
  22. print!("got it: {}, ", x);
  23. *x = String::from("1x4");
  24. }
  25.  
  26. if let Some(x) = map.get_mut(&[a.unwrap(),b.unwrap()]) {
  27. print!("again: got it: {}", x);
  28. }
  29. }
  30.  
  31. fn cast_to_i64(c: Option<char>) -> Option<i64> {
  32. if c.is_none() || c.unwrap().to_digit(10).is_none() {
  33. return None;
  34. }
  35.  
  36. let digit = c.unwrap().to_digit(10).unwrap() as i64;
  37.  
  38. return Some(digit);
  39. }
Add Comment
Please, Sign In to add comment