Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(unused_imports)]
- #![allow(unused_variables)]
- use std::collections::HashMap;
- use std::env;
- // Get a users PATH, split it by ":" and print as a vector
- // two approachs
- fn main() {
- // using Option<T>
- if let Some(paths) = get_path_paths_as_option() {
- println!("{:?}", paths);
- }
- // vs an empty array
- let paths = get_path_paths_always_vector();
- println!("{:?}", paths);
- }
- fn get_path_paths_as_option() -> Option<Vec<String>> {
- let filtered_env: HashMap<String, String> = env::vars().collect();
- match filtered_env.get("PATH") {
- Some(t) => {
- Some(t.split(":")
- .map(|x| x.to_string())
- .collect())
- },
- None => None
- }
- }
- fn get_path_paths_always_vector() -> Vec<String> {
- let filtered_env: HashMap<String, String> = env::vars().collect();
- match filtered_env.get("PATH") {
- Some(t) => {
- t.split(":")
- .map(|x| x.to_string())
- .collect()
- },
- None => vec![]
- }
- }
Add Comment
Please, Sign In to add comment