Guest User

Untitled

a guest
May 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #![allow(unused_imports)]
  2. #![allow(unused_variables)]
  3.  
  4. use std::collections::HashMap;
  5. use std::env;
  6.  
  7. // Get a users PATH, split it by ":" and print as a vector
  8. // two approachs
  9. fn main() {
  10.  
  11. // using Option<T>
  12. if let Some(paths) = get_path_paths_as_option() {
  13. println!("{:?}", paths);
  14. }
  15.  
  16. // vs an empty array
  17. let paths = get_path_paths_always_vector();
  18. println!("{:?}", paths);
  19. }
  20.  
  21. fn get_path_paths_as_option() -> Option<Vec<String>> {
  22. let filtered_env: HashMap<String, String> = env::vars().collect();
  23. match filtered_env.get("PATH") {
  24. Some(t) => {
  25. Some(t.split(":")
  26. .map(|x| x.to_string())
  27. .collect())
  28. },
  29. None => None
  30. }
  31. }
  32.  
  33. fn get_path_paths_always_vector() -> Vec<String> {
  34. let filtered_env: HashMap<String, String> = env::vars().collect();
  35. match filtered_env.get("PATH") {
  36. Some(t) => {
  37. t.split(":")
  38. .map(|x| x.to_string())
  39. .collect()
  40. },
  41. None => vec![]
  42. }
  43. }
Add Comment
Please, Sign In to add comment