Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

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

Code

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 {}

References