Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Push A Path That Starts With A Slash Onto A Rust PathBuf Without Overwriting It

- You may not need to do this if you use strip _ prefix which seems to make the paths relative which then work fine.

- these are scratch notes

- if you .push() on a PathBuf with something that starts with a '/' it overwrites the it instead of adding it

- the code below is how I'm doing it as an append

- I'll pull out the specific examples when I finish a full draft

- This works with basic paths fine. need to do check with ".." and "." and "c : /" etc..

use std::path::{Component, PathBuf};

fn main() {
    // let mut p1 = PathBuf::from("/alfa/bravo");
    // dbg!(&p1);
    // p1.push("charlie");
    // dbg!(&p1);

    // let mut p2 = PathBuf::from("/delta/echo");
    // dbg!(&p2);
    // p2.push("/foxtrot");
    // dbg!(&p2);

    // let mut p3 = PathBuf::from("/golf/hotel");
    // dbg!(&p3);
    // p3.push(PathBuf::from("/india").strip_prefix("/").unwrap());
    // dbg!(&p3);

    let mut p4 = PathBuf::from("/juliett/kilo");
    PathBuf::from("/lima/mike")
        .components()
        .for_each(|x| match x {
            Component::Normal(value) => p4.push(value),
            _ => {}
        });

    // dbg!(addition
    //     .components()
    //     .filter_map(|x| match x {
    //         Component::Normal(value) => {
    //             Some(value)
    //         }
    //         _ => {
    //             None
    //         }
    //     })
    //     .collect::<Vec<_>>());

    // p4.push(
    //     addition
    //         .components()
    //         .filter_map(|x| match x {
    //             Component::Normal(value) => Some(PathBuf::from(value)),
    //             _ => None,
    //         })
    //         .collect::<Vec<_>>(),
    // );

    // p4.push(addition.components().map(|x| x).collect::<Vec<_>>());

    // p4.push(
    //     addition
    //         .components()
    //         .into_iter()
    //         .map(|x| x.as_os_str())
    //         .collect(),
    // );

    dbg!(&p4);

    // p4.push(PathBuf::from("/lima").strip_prefix("/").unwrap());
    // dbg!(&p4);
}

// fn append_path() -> PathBuf {}

Footnotes And References