Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- macro_rules! build_from_paths {
- ($base:expr, $($segment:expr),+) => {{
- let mut base: ::std::path::PathBuf = $base.into();
- $(
- base.push($segment);
- )*
- base
- }}
- }
- fn main() {
- use std::path::{Path, PathBuf};
- use std::ffi::OsStr;
- let a = build_from_paths!("a", "b", "c");
- println!("{:?}", a);
- let b = build_from_paths!(PathBuf::from("z"), OsStr::new("x"), Path::new("y"));
- println!("{:?}", b);
- }
- use std::path::{Path, PathBuf};
- fn join_all<P, Ps>(parts: Ps) -> PathBuf
- where Ps: IntoIterator<Item = P>,
- P: AsRef<Path>
- {
- parts.into_iter().fold(PathBuf::new(), |mut acc, p| {
- acc.push(p);
- acc
- })
- }
- fn main() {
- let parts = vec!["/usr", "bin", "man"];
- println!("{:?}", join_all(&parts));
- println!("{:?}", join_all(&["/etc", "passwd"]));
- }
- use std::path::PathBuf;
- let path: PathBuf = [r"C:", "windows", "system32.dll"].iter().collect();
Add Comment
Please, Sign In to add comment