Rust Path vs PathBuf
From Is working with Paths always this painful? on Reddit:
The difference between
Path
andPathBuf
is roughly the same as the one between&str
andString
or&[]
andVec,
ie.Path
only holds a reference to the path string data but doesn’t own this data, whilePathBuf
owns the string data itself. This means that aPath
is immutable and can’t be used longer than the actual data (held somewhere else) is available.The reason why both types exists is to avoid allocations where possible. As most functions take both
Path
andPathBuf
as arguments (by usingAsRef<Path>
for example), this usually doesn’t have a big impact on your code.A very rough guide for when to use
Path
vs.PathBuf:
- For return types:
- If your function gets passed a
Path[Buf]
and returns a subpath of it, you can just return aPath
(likePath[Buf].parent()
)- If you create a new path, or combine paths or anything like that, you need to return a
PathBuf
.- For arguments:
- Take a
PathBuf
if you need to store it somewhere.- Use a
Path
otherwise.- In public interfaces, you usually don’t want to use
Path
orPathBuf
directly, but rather a genericP: AsRef<Path>
orP: Into<PathBuf>
. That way the caller can pass inPath
,PathBuf,
&str
orString
.