Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. use std::collections::HashMap;
  2.  
  3. fn main() {
  4. let attribs = vec!["alpha", "beta", "gamma", "delta"];
  5. let specials = vec!["beta", "delta"];
  6. print_attribs(attribs, specials);
  7. }
  8.  
  9. // expected output: one *two three *four
  10. fn print_attribs(attribs: Vec<&str>, specials: Vec<&str>) {
  11. let m = get_map();
  12. let mut output: Vec<String> = Vec::new();
  13. for item in attribs {
  14. let v = m.get(item).unwrap();
  15. let mut w = String::new();
  16. if specials.contains(&item) {
  17. w.push('*');
  18. }
  19. w.push_str(v);
  20. output.push(w);
  21. }
  22. // do something with 'output'...
  23. println!("\noutput = {:?} ", output);
  24. }
  25.  
  26. fn get_map() -> HashMap<String, String> {
  27. let mut m: HashMap<String, String> = HashMap::new();
  28. m.insert(String::from("alpha"), String::from("one"));
  29. m.insert(String::from("epsilon"), String::from("five"));
  30. m.insert(String::from("beta"), String::from("two"));
  31. m.insert(String::from("gamma"), String::from("three"));
  32. m.insert(String::from("delta"), String::from("four"));
  33. m
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement