Guest User

Untitled

a guest
Feb 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. macro_rules! build_from_paths {
  2. ($base:expr, $($segment:expr),+) => {{
  3. let mut base: ::std::path::PathBuf = $base.into();
  4. $(
  5. base.push($segment);
  6. )*
  7. base
  8. }}
  9. }
  10.  
  11. fn main() {
  12. use std::path::{Path, PathBuf};
  13. use std::ffi::OsStr;
  14.  
  15. let a = build_from_paths!("a", "b", "c");
  16. println!("{:?}", a);
  17.  
  18. let b = build_from_paths!(PathBuf::from("z"), OsStr::new("x"), Path::new("y"));
  19. println!("{:?}", b);
  20.  
  21. }
  22.  
  23. use std::path::{Path, PathBuf};
  24.  
  25. fn join_all<P, Ps>(parts: Ps) -> PathBuf
  26. where Ps: IntoIterator<Item = P>,
  27. P: AsRef<Path>
  28. {
  29. parts.into_iter().fold(PathBuf::new(), |mut acc, p| {
  30. acc.push(p);
  31. acc
  32. })
  33. }
  34.  
  35. fn main() {
  36. let parts = vec!["/usr", "bin", "man"];
  37. println!("{:?}", join_all(&parts));
  38. println!("{:?}", join_all(&["/etc", "passwd"]));
  39. }
  40.  
  41. use std::path::PathBuf;
  42. let path: PathBuf = [r"C:", "windows", "system32.dll"].iter().collect();
Add Comment
Please, Sign In to add comment