Path and Filesystem Models
Join Path Segments
Build a Child Path
Path::join appends a child path to a base path and returns an owned PathBuf.
Program
Play the program to choose a child path and join it under a fixed target directory.
join_path_segments.rs
Replay: real traced execution (multi-file project)
use std::path::Path;
fn main() {
let file = "trace.log";
let base = Path::new("target");
let full = base.join(file);
println!("{}", full.display());
}
use std::path::Path;
fn main() {
let file = "debug/out.txt";
let base = Path::new("target");
let full = base.join(file);
println!("{}", full.display());
}
use std::path::Path;
fn main() {
let file = "metrics.csv";
let base = Path::new("target");
let full = base.join(file);
println!("{}", full.display());
}
file ← "trace.log", base ← "target", full ← "target/trace.log"
3fn main() {4 let fil→ "trace.log"e = "trace.log"; //@file="trace.log", "debug/out.txt", "metrics.csv"5 let bas→ "target"e = Path::new("target");6 let ful→ "target/trace.log"l = base.join(file);7 println!("{}", full.display());8}outputtarget/trace.log
file ← "debug/out.txt", base ← "target", full ← "target/debug/out.txt"
3fn main() {4 let fil→ "debug/out.txt"e = "debug/out.txt";5 let bas→ "target"e = Path::new("target");6 let ful→ "target/debug/out.txt"l = base.join(file);7 println!("{}", full.display());8}outputtarget/debug/out.txt
file ← "metrics.csv", base ← "target", full ← "target/metrics.csv"
3fn main() {4 let fil→ "metrics.csv"e = "metrics.csv";5 let bas→ "target"e = Path::new("target");6 let ful→ "target/metrics.csv"l = base.join(file);7 println!("{}", full.display());8}outputtarget/metrics.csv
join
`join` creates a child path without changing the base path.
PathBuf
The joined result is owned so it can be moved or changed later.
display
`display` formats a path for user-facing output.