Struct OsStr
#[repr(transparent)]
pub struct OsStr { pub(in ::ffi::os_str) inner: Slice }
Borrowed reference to an OS string (see OsString).
This type represents a borrowed reference to a string in the operating system's preferred representation.
&OsStr is to OsString as &[str] is to [String]: the
former in each pair are borrowed references; the latter are owned strings.
See the module's toplevel documentation about conversions for a discussion on
the traits which OsStr implements for conversions from/to native representations.
Fields
inner: Slice
Implementations
impl OsStr
const fn new<S: ~const AsRef<OsStr> + ?Sized>(s: &S) -> &OsStrCoerces into an
OsStrslice.Examples
use OsStr; let os_str = new;unsafe fn from_encoded_bytes_unchecked(bytes: &[u8]) -> &SelfConverts a slice of bytes to an OS string slice without checking that the string contains valid
OsStr-encoded data.The byte encoding is an unspecified, platform-specific, self-synchronizing superset of UTF-8. By being a self-synchronizing superset of UTF-8, this encoding is also a superset of 7-bit ASCII.
See the module's toplevel documentation about conversions for safe, cross-platform conversions from/to native representations.
Safety
As the encoding is unspecified, callers must pass in bytes that originated as a mixture of validated UTF-8 and bytes from
OsStr::as_encoded_bytesfrom within the same Rust version built for the same target platform. For example, reconstructing anOsStrfrom bytes sent over the network or stored in a file will likely violate these safety rules.Due to the encoding being self-synchronizing, the bytes from
OsStr::as_encoded_bytescan be split either immediately before or immediately after any valid non-empty UTF-8 substring.Example
use OsStr; let os_str = new; let bytes = os_str.as_encoded_bytes; let words = bytes.split; let words: = words.map.collect;const fn from_inner(inner: &Slice) -> &OsStrconst fn from_inner_mut(inner: &mut Slice) -> &mut OsStrfn to_str(&self) -> Option<&str>Yields a
&[str]slice if theOsStris valid Unicode.This conversion may entail doing a check for UTF-8 validity.
Examples
use OsStr; let os_str = new; assert_eq!;fn to_string_lossy(&self) -> Cow<'_, str>Converts an
OsStrto a[Cow]<[str]>.Any non-UTF-8 sequences are replaced with
U+FFFD REPLACEMENT CHARACTER.Examples
Calling
to_string_lossyon anOsStrwith invalid unicode:// Note, due to differences in how Unix and Windows represent strings, // we are forced to complicate this example, setting up example `OsStr`s // with different source data and via different platform extensions. // Understand that in reality you could end up with such example invalid // sequences simply through collecting user command line arguments, for // example.fn to_os_string(&self) -> OsStringCopies the slice into an owned
OsString.Examples
use ; let os_str = new; let os_string = os_str.to_os_string; assert_eq!;fn is_empty(&self) -> boolChecks whether the
OsStris empty.Examples
use OsStr; let os_str = new; assert!; let os_str = new; assert!;fn len(&self) -> usizeReturns the length of this
OsStr.Note that this does not return the number of bytes in the string in OS string form.
The length returned is that of the underlying storage used by
OsStr. As discussed in theOsStringintroduction,OsStringandOsStrstore strings in a form best suited for cheap inter-conversion between native-platform and Rust string forms, which may differ significantly from both of them, including in storage size and encoding.This number is simply useful for passing to other methods, like
OsString::with_capacityto avoid reallocations.See the main
OsStringdocumentation information about encoding and capacity units.Examples
use OsStr; let os_str = new; assert_eq!; let os_str = new; assert_eq!;fn into_os_string(self: Box<Self>) -> OsStringConverts a
[Box]<[OsStr]>into anOsStringwithout copying or allocating.fn split_at(&self, mid: usize) -> (&OsStr, &OsStr)Divides one string slice into two at an index.
The two slices returned go from the start of the string slice to
mid, and frommidto the end of the string slice.The argument,
mid, should be a byte offset from the start of the string. It must also be on a validOsStrboundary. See [split_at_checked][Self::split_at_checked] for the definition of a valid boundary.Panics
Panics if
midis not on a valid boundary, or if it is past the end of the last code point of the string slice. For a non-panicking alternative see [split_at_checked][Self::split_at_checked].fn split_at_checked(&self, mid: usize) -> Option<(&OsStr, &OsStr)>Divides one string slice into two at an index.
The two slices returned go from the start of the string slice to
mid, and frommidto the end of the string slice.The argument,
mid, should be a valid byte offset from the start of the string. It must also be on a validOsStrboundary. The method returnsNoneif that’s not the case. A validOsStrboundary is one of:- The start of the string
- The end of the string
- The start of a valid non-empty UTF-8 substring
- Immediately follows a valid non-empty UTF-8 substring
unsafe fn split_at_unchecked(&self, mid: usize) -> (&OsStr, &OsStr)Splits an
OsStrwithout checking ifmidis a valid boundary. You should usesplit_atorsplit_at_checkedinstead.Safety
Any caller must ensure
midis within bounds and lies on a validOsStrboundary for the platform.fn as_encoded_bytes(&self) -> &[u8]Converts an OS string slice to a byte slice. To convert the byte slice back into an OS string slice, use the
OsStr::from_encoded_bytes_uncheckedfunction.The byte encoding is an unspecified, platform-specific, self-synchronizing superset of UTF-8. By being a self-synchronizing superset of UTF-8, this encoding is also a superset of 7-bit ASCII.
Note: As the encoding is unspecified, any sub-slice of bytes that is not valid UTF-8 should be treated as opaque and only comparable within the same Rust version built for the same target platform. For example, sending the slice over the network or storing it in a file will likely result in incompatible byte slices. See
OsStringfor more encoding details andstd::ffifor platform-specific, specified conversions.fn slice_encoded_bytes<R: RangeBounds<usize>>(&self, range: R) -> &SelfTakes a substring based on a range that corresponds to the return value of
OsStr::as_encoded_bytes.The range's start and end must lie on valid
OsStrboundaries. A validOsStrboundary is one of:- The start of the string
- The end of the string
- Immediately before a valid non-empty UTF-8 substring
- Immediately after a valid non-empty UTF-8 substring
Panics
Panics if
rangedoes not lie on validOsStrboundaries or if it exceeds the end of the string.Example
use OsStr; let os_str = new; let bytes = os_str.as_encoded_bytes; if let Some = bytes.iter.positionfn make_ascii_lowercase(&mut self)Converts this string to its ASCII lower case equivalent in-place.
ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', but non-ASCII letters are unchanged.
To return a new lowercased value without modifying the existing one, use
OsStr::to_ascii_lowercase.Examples
use OsString; let mut s = from; s.make_ascii_lowercase; assert_eq!;fn make_ascii_uppercase(&mut self)Converts this string to its ASCII upper case equivalent in-place.
ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', but non-ASCII letters are unchanged.
To return a new uppercased value without modifying the existing one, use
OsStr::to_ascii_uppercase.Examples
use OsString; let mut s = from; s.make_ascii_uppercase; assert_eq!;fn to_ascii_lowercase(&self) -> OsStringReturns a copy of this string where each character is mapped to its ASCII lower case equivalent.
ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', but non-ASCII letters are unchanged.
To lowercase the value in-place, use
OsStr::make_ascii_lowercase.Examples
use OsString; let s = from; assert_eq!;fn to_ascii_uppercase(&self) -> OsStringReturns a copy of this string where each character is mapped to its ASCII upper case equivalent.
ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', but non-ASCII letters are unchanged.
To uppercase the value in-place, use
OsStr::make_ascii_uppercase.Examples
use OsString; let s = from; assert_eq!;fn is_ascii(&self) -> boolChecks if all characters in this string are within the ASCII range.
An empty string returns
true.Examples
use OsString; let ascii = from; let non_ascii = from; assert!; assert!;fn eq_ignore_ascii_case<S: AsRef<OsStr>>(&self, other: S) -> boolChecks that two strings are an ASCII case-insensitive match.
Same as
to_ascii_lowercase(a) == to_ascii_lowercase(b), but without allocating and copying temporaries.Examples
use OsString; assert!; assert!; assert!;fn display(&self) -> Display<'_>Returns an object that implements
Displayfor safely printing anOsStrthat may contain non-Unicode data. This may perform lossy conversion, depending on the platform. If you would like an implementation which escapes theOsStrplease useDebuginstead.Examples
use OsStr; let s = new; println!;const fn as_os_str(&self) -> &OsStrReturns the same string as a string slice
&OsStr.This method is redundant when used directly on
&OsStr, but it helps dereferencing other string-like types to string slices, for example references toBox<OsStr>orArc<OsStr>.
Trait Implementations
impl AsInner<Slice> for OsStr
fn as_inner(&self) -> &Slice
impl AsRef<OsStr> for OsStr
fn as_ref(&self) -> &OsStr
impl AsRef<Path> for OsStr
fn as_ref(&self) -> &Path
impl CloneToUninit for OsStr
unsafe fn clone_to_uninit(&self, dst: *mut u8)
impl Debug for OsStr
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result
impl Eq for OsStr
impl Hash for OsStr
fn hash<H: Hasher>(&self, state: &mut H)
impl Ord for OsStr
fn cmp(&self, other: &OsStr) -> Ordering
impl OsStrExt for OsStr
fn from_bytes(slice: &[u8]) -> &OsStrfn as_bytes(&self) -> &[u8]
impl OsStrExt for OsStr
fn from_bytes(slice: &[u8]) -> &OsStrfn as_bytes(&self) -> &[u8]
impl OsStrExt for OsStr
fn encode_wide(&self) -> EncodeWide<'_>
impl PartialEq for OsStr
fn eq(&self, other: &OsStr) -> bool
impl PartialEq<&Path> for OsStr
fn eq(&self, other: &&Path) -> bool
impl PartialEq<Cow<'_, OsStr>> for OsStr
fn eq(&self, other: &Cow<'_, OsStr>) -> bool
impl PartialEq<Cow<'_, Path>> for OsStr
fn eq(&self, other: &Cow<'_, Path>) -> bool
impl PartialEq<OsString> for OsStr
fn eq(&self, other: &OsString) -> bool
impl PartialEq<Path> for OsStr
fn eq(&self, other: &Path) -> bool
impl PartialEq<PathBuf> for OsStr
fn eq(&self, other: &PathBuf) -> bool
impl PartialEq<str> for OsStr
fn eq(&self, other: &str) -> bool
impl PartialOrd for OsStr
fn partial_cmp(&self, other: &OsStr) -> Option<Ordering>fn lt(&self, other: &OsStr) -> boolfn le(&self, other: &OsStr) -> boolfn gt(&self, other: &OsStr) -> boolfn ge(&self, other: &OsStr) -> bool
impl PartialOrd<&Path> for OsStr
fn partial_cmp(&self, other: &&Path) -> Option<Ordering>
impl PartialOrd<Cow<'_, OsStr>> for OsStr
fn partial_cmp(&self, other: &Cow<'_, OsStr>) -> Option<Ordering>
impl PartialOrd<Cow<'_, Path>> for OsStr
fn partial_cmp(&self, other: &Cow<'_, Path>) -> Option<Ordering>
impl PartialOrd<OsString> for OsStr
fn partial_cmp(&self, other: &OsString) -> Option<Ordering>
impl PartialOrd<Path> for OsStr
fn partial_cmp(&self, other: &Path) -> Option<Ordering>
impl PartialOrd<PathBuf> for OsStr
fn partial_cmp(&self, other: &PathBuf) -> Option<Ordering>
impl PartialOrd<str> for OsStr
fn partial_cmp(&self, other: &str) -> Option<Ordering>
impl ToOwned for OsStr
type Owned = OsString;fn to_owned(&self) -> OsStringfn clone_into(&self, target: &mut OsString)
Auto Trait Implementations
impl !Sized for OsStr
impl Freeze for OsStr
impl RefUnwindSafe for OsStr
impl Send for OsStr
impl Sync for OsStr
impl Unpin for OsStr
impl UnsafeUnpin for OsStr
impl UnwindSafe for OsStr
Blanket Implementations
impl<T> Any for OsStr
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for OsStr
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for OsStr
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> SizeHint for OsStr
where
T: ?Sized,
fn lower_bound(&self) -> usizefn upper_bound(&self) -> Option<usize>