Path

The Path struct represents file paths in the underlying filesystem. There are
two flavors of Path: posix::Path, for UNIX-like systems, and
windows::Path, for Windows. The prelude exports the appropriate
platform-specific Path variant.

A Path can be created from an OsStr, and provides several methods to get
information from the file/directory the path points to.

Note that a Path is not internally represented as an UTF-8 string, but
instead is stored as a vector of bytes (Vec<u8>). Therefore, converting a
Path to a &str is not free and may fail (an Option is returned).

  1. use std::path::Path;
  2. fn main() {
  3. // Create a `Path` from an `&'static str`
  4. let path = Path::new(".");
  5. // The `display` method returns a `Show`able structure
  6. let _display = path.display();
  7. // `join` merges a path with a byte container using the OS specific
  8. // separator, and returns the new path
  9. let new_path = path.join("a").join("b");
  10. // Convert the path into a string slice
  11. match new_path.to_str() {
  12. None => panic!("new path is not a valid UTF-8 sequence"),
  13. Some(s) => println!("new path is {}", s),
  14. }
  15. }

Be sure to check at other Path methods (posix::Path or windows::Path) and
the Metadata struct.

See also

OsStr and Metadata.