Struct OsStr
struct OsStr { ... }
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.
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;fn to_str(self: &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: &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: &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: &Self) -> boolChecks whether the
OsStris empty.Examples
use OsStr; let os_str = new; assert!; let os_str = new; assert!;fn len(self: &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 as_encoded_bytes(self: &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: ops::RangeBounds<usize>>(self: &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(self: &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(self: &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: &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: &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: &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: &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: &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: &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>.
impl AsRef for OsStr
fn as_ref(self: &Self) -> &OsStr
impl AsRef for crate::ffi::OsStr
fn as_ref(self: &Self) -> &Path
impl CloneToUninit for OsStr
unsafe fn clone_to_uninit(self: &Self, dst: *mut u8)
impl Debug for OsStr
fn fmt(self: &Self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result
impl Eq for OsStr
impl Freeze for OsStr
impl Hash for OsStr
fn hash<H: Hasher>(self: &Self, state: &mut H)
impl Ord for OsStr
fn cmp(self: &Self, other: &OsStr) -> cmp::Ordering
impl OsStrExt for crate::ffi::OsStr
fn from_bytes(slice: &[u8]) -> &OsStrfn as_bytes(self: &Self) -> &[u8]
impl OsStrExt for crate::ffi::OsStr
fn encode_wide(self: &Self) -> EncodeWide<'_>
impl OsStrExt for crate::ffi::OsStr
fn from_bytes(slice: &[u8]) -> &OsStrfn as_bytes(self: &Self) -> &[u8]
impl PartialEq for OsStr
fn eq(self: &Self, other: &OsStr) -> bool
impl PartialEq for OsStr
fn eq(self: &Self, other: &str) -> bool
impl PartialEq for crate::ffi::OsStr
fn eq(self: &Self, other: &Path) -> bool
impl PartialEq for crate::ffi::OsStr
fn eq(self: &Self, other: &PathBuf) -> bool
impl PartialOrd for OsStr
fn partial_cmp(self: &Self, other: &str) -> Option<cmp::Ordering>
impl PartialOrd for OsStr
fn partial_cmp(self: &Self, other: &OsStr) -> Option<cmp::Ordering>fn lt(self: &Self, other: &OsStr) -> boolfn le(self: &Self, other: &OsStr) -> boolfn gt(self: &Self, other: &OsStr) -> boolfn ge(self: &Self, other: &OsStr) -> bool
impl PartialOrd for crate::ffi::OsStr
fn partial_cmp(self: &Self, other: &Path) -> Option<cmp::Ordering>
impl PartialOrd for crate::ffi::OsStr
fn partial_cmp(self: &Self, other: &PathBuf) -> Option<cmp::Ordering>
impl RefUnwindSafe for OsStr
impl Send for OsStr
impl Sized for OsStr
impl Sync for OsStr
impl ToOwned for OsStr
fn to_owned(self: &Self) -> OsStringfn clone_into(self: &Self, target: &mut OsString)
impl Unpin for OsStr
impl UnwindSafe for OsStr
impl<'a> PartialEq for crate::ffi::OsStr
fn eq(self: &Self, other: &Cow<'a, Path>) -> bool
impl<'a> PartialEq for crate::ffi::OsStr
fn eq(self: &Self, other: &&'a Path) -> bool
impl<'a> PartialOrd for crate::ffi::OsStr
fn partial_cmp(self: &Self, other: &&'a Path) -> Option<cmp::Ordering>
impl<'a> PartialOrd for crate::ffi::OsStr
fn partial_cmp(self: &Self, other: &Cow<'a, Path>) -> Option<cmp::Ordering>
impl<'a, 'b> PartialEq for OsStr
fn eq(self: &Self, other: &OsString) -> bool
impl<'a, 'b> PartialEq for OsStr
fn eq(self: &Self, other: &Cow<'a, OsStr>) -> bool
impl<'a, 'b> PartialOrd for OsStr
fn partial_cmp(self: &Self, other: &Cow<'a, OsStr>) -> Option<cmp::Ordering>
impl<'a, 'b> PartialOrd for OsStr
fn partial_cmp(self: &Self, other: &OsString) -> Option<cmp::Ordering>
impl<T> Any for OsStr
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for OsStr
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for OsStr
fn borrow_mut(self: &mut Self) -> &mut T