Enum Component

pub 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 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) -> &'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"]);

Trait Implementations

impl AsRef<OsStr> for Component<'_>

fn as_ref(&self) -> &OsStr

impl AsRef<Path> for Component<'_>

fn as_ref(&self) -> &Path

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

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

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

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

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

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

fn assert_fields_are_eq(&self)

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

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

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

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

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

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

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

fn partial_cmp(&self, other: &Component<'a>) -> Option<Ordering>

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

impl<'a> TrivialClone for Component<'a>

Auto Trait Implementations

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

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

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

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

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

impl<'a> UnsafeUnpin for Component<'a>

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

Blanket Implementations

impl<T> Any for Component<'a> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Component<'a> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Component<'a> where T: ?Sized,

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

impl<T> CloneToUninit for Component<'a> where T: Clone,

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

impl<T> From<T> for Component<'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Printable for Component<'a> where T: Copy + Debug,

impl<T> SizeHint for Component<'a> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for Component<'a>

impl<T> ToOwned for Component<'a> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Component<'a> where U: From<T>,

fn into(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<U> for Component<'a> where U: Into<T>,

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

impl<T, U> TryInto<U> for Component<'a> where U: TryFrom<T>,

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