Enum Component

enum Component<'a>

A single component of a path.

A Component roughly corresponds to a substring between path separators (/ or \).

This enum is created by iterating over Components, which in turn is created by the components method on Path.

Examples

use std::path::{Component, Path};

let path = Path::new("/tmp/foo/bar.txt");
let components = path.components().collect::<Vec<_>>();
assert_eq!(&components, &[
    Component::RootDir,
    Component::Normal("tmp".as_ref()),
    Component::Normal("foo".as_ref()),
    Component::Normal("bar.txt".as_ref()),
]);

Variants

Prefix(PrefixComponent<'a>)

A Windows path prefix, e.g., C: or \\server\share.

There is a large variety of prefix types, see Prefix's documentation for more.

Does not occur on Unix.

RootDir

The root directory component, appears after any prefix and before anything else.

It represents a separator that designates that a path starts from root.

CurDir

A reference to the current directory, i.e., ..

ParentDir

A reference to the parent directory, i.e., ...

Normal(&'a crate::ffi::OsStr)

A normal component, e.g., a and b in a/b.

This variant is the most common one, it represents references to files or directories.

Implementations

impl<'a> Component<'a>

fn as_os_str(self: Self) -> &'a OsStr

Extracts the underlying OsStr slice.

Examples

use std::path::Path;

let path = Path::new("./tmp/foo/bar.txt");
let components: Vec<_> = path.components().map(|comp| comp.as_os_str()).collect();
assert_eq!(&components, &[".", "tmp", "foo", "bar.txt"]);

impl AsRef for Component<'_>

fn as_ref(self: &Self) -> &OsStr

impl AsRef for Component<'_>

fn as_ref(self: &Self) -> &Path

impl<'a> Clone for Component<'a>

fn clone(self: &Self) -> Component<'a>

impl<'a> Copy for Component<'a>

impl<'a> Debug for Component<'a>

fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result

impl<'a> Eq for Component<'a>

impl<'a> Freeze for Component<'a>

impl<'a> Hash for Component<'a>

fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)

impl<'a> Ord for Component<'a>

fn cmp(self: &Self, other: &Component<'a>) -> $crate::cmp::Ordering

impl<'a> PartialEq for Component<'a>

fn eq(self: &Self, other: &Component<'a>) -> bool

impl<'a> PartialOrd for Component<'a>

fn partial_cmp(self: &Self, other: &Component<'a>) -> $crate::option::Option<$crate::cmp::Ordering>

impl<'a> RefUnwindSafe for Component<'a>

impl<'a> Send for Component<'a>

impl<'a> StructuralPartialEq for Component<'a>

impl<'a> Sync for Component<'a>

impl<'a> Unpin for Component<'a>

impl<'a> UnwindSafe for Component<'a>

impl<T> Any for Component<'a>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Component<'a>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Component<'a>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for Component<'a>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Component<'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Component<'a>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for Component<'a>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Component<'a>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Component<'a>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>