Path - Rust By Example
Description: Rust by Example (RBE) is a collection of runnable examples that illustrate various Rust concepts and standard libraries.

## Keyboard shortcuts

Press `←` or `→` to navigate between chapters

Press `S` or `/` to search in the book

Press `?` to show this help

Press `Esc` to hide this help

# Path

The `Path` type represents file paths in the underlying filesystem. Across all platforms there is a single `std::path::Path` that abstracts over platform-specific path semantics and separators. Bring it into scope with `use std::path::Path;` when needed.

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

A `Path` is immutable. The owned version of `Path` is `PathBuf`. The relation between `Path` and `PathBuf` is similar to that of `str` and `String`: a `PathBuf` can be mutated in-place, and can be dereferenced to a `Path`.

Note that a `Path` is not internally represented as an UTF-8 string, but instead is stored as an `OsString`. Therefore, converting a `Path` to a `&str` is not free and may fail (an `Option` is returned). However, a `Path` can be freely converted to an `OsString` or `&OsStr` using `into_os_string` and `as_os_str`, respectively.

    use std::path::Path;

    fn main() {
        // Create a `Path` from an `&'static str`
        let path = Path::new(".");

        // The `display` method returns a `Display`able structure
        let _display = path.display();

        // `join` merges a path with a byte container using the OS specific
        // separator, and returns a `PathBuf`
        let mut new_path = path.join("a").join("b");

        // `push` extends the `PathBuf` with a `&Path`
        new_path.push("c");
        new_path.push("myfile.tar.gz");

        // `set_file_name` updates the file name of the `PathBuf`
        new_path.set_file_name("package.tgz");

        // Convert the `PathBuf` into a string slice
        match new_path.to_str() {
            None => panic!("new path is not a valid UTF-8 sequence"),
            Some(s) => println!("new path is {}", s),
        }
    }

Be sure to check other `Path` methods and the `Metadata` struct.

### See also:

OsStr and Metadata.