pub struct OsString { /* private fields */ }
os_str_display
#120048)Expand description
A type that can represent owned, mutable platform-native strings, but is cheaply inter-convertible with Rust strings.
The need for this type arises from the fact that:
-
On Unix systems, strings are often arbitrary sequences of non-zero bytes, in many cases interpreted as UTF-8.
-
On Windows, strings are often arbitrary sequences of non-zero 16-bit values, interpreted as UTF-16 when it is valid to do so.
-
In Rust, strings are always valid UTF-8, which may contain zeros.
OsString
and OsStr
bridge this gap by simultaneously representing Rust
and platform-native string values, and in particular allowing a Rust string
to be converted into an โOSโ string with no cost if possible. A consequence
of this is that OsString
instances are not NUL
terminated; in order
to pass to e.g., Unix system call, you should create a CStr
.
OsString
is to &OsStr
as String
is to &str
: the former
in each pair are owned strings; the latter are borrowed
references.
Note, OsString
and OsStr
internally do not necessarily hold strings in
the form native to the platform; While on Unix, strings are stored as a
sequence of 8-bit values, on Windows, where strings are 16-bit value based
as just discussed, strings are also actually stored as a sequence of 8-bit
values, encoded in a less-strict variant of UTF-8. This is useful to
understand when handling capacity and length values.
ยงCapacity of OsString
Capacity uses units of UTF-8 bytes for OS strings which were created from valid unicode, and
uses units of bytes in an unspecified encoding for other contents. On a given target, all
OsString
and OsStr
values use the same units for capacity, so the following will work:
use std::ffi::{OsStr, OsString};
fn concat_os_strings(a: &OsStr, b: &OsStr) -> OsString {
let mut ret = OsString::with_capacity(a.len() + b.len()); // This will allocate
ret.push(a); // This will not allocate further
ret.push(b); // This will not allocate further
ret
}
ยงCreating an OsString
From a Rust string: OsString
implements
From<String>
, so you can use my_string.into()
to
create an OsString
from a normal Rust string.
From slices: Just like you can start with an empty Rust
String
and then String::push_str
some &str
sub-string slices into it, you can create an empty OsString
with
the OsString::new
method and then push string slices into it with the
OsString::push
method.
ยงExtracting a borrowed reference to the whole OS string
You can use the OsString::as_os_str
method to get an &OsStr
from
an OsString
; this is effectively a borrowed reference to the
whole string.
ยงConversions
See the moduleโs toplevel documentation about conversions for a discussion on
the traits which OsString
implements for conversions from/to native representations.
Implementationsยง
Sourceยงimpl OsString
impl OsString
1.74.0 ยท Sourcepub unsafe fn from_encoded_bytes_unchecked(bytes: Vec<u8>) -> Self
pub unsafe fn from_encoded_bytes_unchecked(bytes: Vec<u8>) -> Self
Converts bytes to an OsString
without checking that the bytes 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_bytes
from within the same Rust version
built for the same target platform. For example, reconstructing an OsString
from 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_bytes
can be
split either immediately before or immediately after any valid non-empty UTF-8 substring.
ยงExample
use std::ffi::OsStr;
let os_str = OsStr::new("Mary had a little lamb");
let bytes = os_str.as_encoded_bytes();
let words = bytes.split(|b| *b == b' ');
let words: Vec<&OsStr> = words.map(|word| {
// SAFETY:
// - Each `word` only contains content that originated from `OsStr::as_encoded_bytes`
// - Only split with ASCII whitespace which is a non-empty UTF-8 substring
unsafe { OsStr::from_encoded_bytes_unchecked(word) }
}).collect();
1.74.0 ยท Sourcepub fn into_encoded_bytes(self) -> Vec<u8> โ
pub fn into_encoded_bytes(self) -> Vec<u8> โ
Converts the OsString
into a byte slice. To convert the byte slice back into an
OsString
, use the OsStr::from_encoded_bytes_unchecked
function.
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 bytes over the network or storing it in a file
will likely result in incompatible data. See OsString
for more encoding details
and std::ffi
for platform-specific, specified conversions.
1.0.0 ยท Sourcepub fn into_string(self) -> Result<String, OsString>
pub fn into_string(self) -> Result<String, OsString>
1.9.0 ยท Sourcepub fn with_capacity(capacity: usize) -> OsString
pub fn with_capacity(capacity: usize) -> OsString
Creates a new OsString
with at least the given capacity.
The string will be able to hold at least capacity
length units of other
OS strings without reallocating. This method is allowed to allocate for
more units than capacity
. If capacity
is 0, the string will not
allocate.
See the main OsString
documentation information about encoding and capacity units.
ยงExamples
1.9.0 ยท Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the capacity this OsString
can hold without reallocating.
See the main OsString
documentation information about encoding and capacity units.
ยงExamples
1.9.0 ยท Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more capacity to be inserted
in the given OsString
. Does nothing if the capacity is
already sufficient.
The collection may reserve more space to speculatively avoid frequent reallocations.
See the main OsString
documentation information about encoding and capacity units.
ยงExamples
1.63.0 ยท Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Tries to reserve capacity for at least additional
more length units
in the given OsString
. The string may reserve more space to speculatively avoid
frequent reallocations. After calling try_reserve
, capacity will be
greater than or equal to self.len() + additional
if it returns Ok(())
.
Does nothing if capacity is already sufficient. This method preserves
the contents even if an error occurs.
See the main OsString
documentation information about encoding and capacity units.
ยงErrors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
ยงExamples
use std::ffi::{OsStr, OsString};
use std::collections::TryReserveError;
fn process_data(data: &str) -> Result<OsString, TryReserveError> {
let mut s = OsString::new();
// Pre-reserve the memory, exiting if we can't
s.try_reserve(OsStr::new(data).len())?;
// Now we know this can't OOM in the middle of our complex work
s.push(data);
Ok(s)
}
1.9.0 ยท Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for at least additional
more capacity to
be inserted in the given OsString
. Does nothing if the capacity is
already sufficient.
Note that the allocator may give the collection more space than it
requests. Therefore, capacity can not be relied upon to be precisely
minimal. Prefer reserve
if future insertions are expected.
See the main OsString
documentation information about encoding and capacity units.
ยงExamples
1.63.0 ยท Sourcepub fn try_reserve_exact(
&mut self,
additional: usize,
) -> Result<(), TryReserveError>
pub fn try_reserve_exact( &mut self, additional: usize, ) -> Result<(), TryReserveError>
Tries to reserve the minimum capacity for at least additional
more length units in the given OsString
. After calling
try_reserve_exact
, capacity will be greater than or equal to
self.len() + additional
if it returns Ok(())
.
Does nothing if the capacity is already sufficient.
Note that the allocator may give the OsString
more space than it
requests. Therefore, capacity can not be relied upon to be precisely
minimal. Prefer try_reserve
if future insertions are expected.
See the main OsString
documentation information about encoding and capacity units.
ยงErrors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
ยงExamples
use std::ffi::{OsStr, OsString};
use std::collections::TryReserveError;
fn process_data(data: &str) -> Result<OsString, TryReserveError> {
let mut s = OsString::new();
// Pre-reserve the memory, exiting if we can't
s.try_reserve_exact(OsStr::new(data).len())?;
// Now we know this can't OOM in the middle of our complex work
s.push(data);
Ok(s)
}
1.19.0 ยท Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the OsString
to match its length.
See the main OsString
documentation information about encoding and capacity units.
ยงExamples
1.56.0 ยท Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the capacity of the OsString
with a lower bound.
The capacity will remain at least as large as both the length and the supplied value.
If the current capacity is less than the lower limit, this is a no-op.
See the main OsString
documentation information about encoding and capacity units.
ยงExamples
1.20.0 ยท Sourcepub fn into_boxed_os_str(self) -> Box<OsStr>
pub fn into_boxed_os_str(self) -> Box<OsStr>
Sourcepub fn leak<'a>(self) -> &'a mut OsStr
๐ฌThis is a nightly-only experimental API. (os_string_pathbuf_leak
#125965)
pub fn leak<'a>(self) -> &'a mut OsStr
os_string_pathbuf_leak
#125965)Consumes and leaks the OsString
, returning a mutable reference to the contents,
&'a mut OsStr
.
The caller has free choice over the returned lifetime, including โstatic. Indeed, this function is ideally used for data that lives for the remainder of the programโs life, as dropping the returned reference will cause a memory leak.
It does not reallocate or shrink the OsString
, so the leaked allocation may include
unused capacity that is not part of the returned slice. If you want to discard excess
capacity, call into_boxed_os_str
, and then Box::leak
instead.
However, keep in mind that trimming the capacity may result in a reallocation and copy.
Methods from Deref<Target = OsStr>ยง
1.0.0 ยท Sourcepub fn to_string_lossy(&self) -> Cow<'_, str>
pub fn to_string_lossy(&self) -> Cow<'_, str>
Converts an OsStr
to a Cow<str>
.
Any non-UTF-8 sequences are replaced with
U+FFFD REPLACEMENT CHARACTER
.
ยงExamples
Calling to_string_lossy
on an OsStr
with 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.
#[cfg(unix)] {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
// Here, the values 0x66 and 0x6f correspond to 'f' and 'o'
// respectively. The value 0x80 is a lone continuation byte, invalid
// in a UTF-8 sequence.
let source = [0x66, 0x6f, 0x80, 0x6f];
let os_str = OsStr::from_bytes(&source[..]);
assert_eq!(os_str.to_string_lossy(), "fo๏ฟฝo");
}
#[cfg(windows)] {
use std::ffi::OsString;
use std::os::windows::prelude::*;
// Here the values 0x0066 and 0x006f correspond to 'f' and 'o'
// respectively. The value 0xD800 is a lone surrogate half, invalid
// in a UTF-16 sequence.
let source = [0x0066, 0x006f, 0xD800, 0x006f];
let os_string = OsString::from_wide(&source[..]);
let os_str = os_string.as_os_str();
assert_eq!(os_str.to_string_lossy(), "fo๏ฟฝo");
}
1.0.0 ยท Sourcepub fn to_os_string(&self) -> OsString
pub fn to_os_string(&self) -> OsString
1.9.0 ยท Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns 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 the OsString
introduction, OsString
and OsStr
store 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_capacity
to avoid reallocations.
See the main OsString
documentation information about encoding and capacity units.
ยงExamples
1.74.0 ยท Sourcepub fn as_encoded_bytes(&self) -> &[u8] โ
pub 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_unchecked
function.
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 OsString
for more encoding details
and std::ffi
for platform-specific, specified conversions.
Sourcepub fn slice_encoded_bytes<R: RangeBounds<usize>>(&self, range: R) -> &Self
๐ฌThis is a nightly-only experimental API. (os_str_slice
#118485)
pub fn slice_encoded_bytes<R: RangeBounds<usize>>(&self, range: R) -> &Self
os_str_slice
#118485)Takes 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 OsStr
boundaries.
A valid OsStr
boundary 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 range
does not lie on valid OsStr
boundaries or if it
exceeds the end of the string.
ยงExample
#![feature(os_str_slice)]
use std::ffi::OsStr;
let os_str = OsStr::new("foo=bar");
let bytes = os_str.as_encoded_bytes();
if let Some(index) = bytes.iter().position(|b| *b == b'=') {
let key = os_str.slice_encoded_bytes(..index);
let value = os_str.slice_encoded_bytes(index + 1..);
assert_eq!(key, "foo");
assert_eq!(value, "bar");
}
1.53.0 ยท Sourcepub fn make_ascii_lowercase(&mut self)
pub fn 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
1.53.0 ยท Sourcepub fn make_ascii_uppercase(&mut self)
pub 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
1.53.0 ยท Sourcepub fn to_ascii_lowercase(&self) -> OsString
pub fn to_ascii_lowercase(&self) -> OsString
Returns 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
1.53.0 ยท Sourcepub fn to_ascii_uppercase(&self) -> OsString
pub fn to_ascii_uppercase(&self) -> OsString
Returns 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
1.53.0 ยท Sourcepub fn is_ascii(&self) -> bool
pub fn is_ascii(&self) -> bool
Checks if all characters in this string are within the ASCII range.
ยงExamples
1.53.0 ยท Sourcepub fn eq_ignore_ascii_case<S: AsRef<OsStr>>(&self, other: S) -> bool
pub fn eq_ignore_ascii_case<S: AsRef<OsStr>>(&self, other: S) -> bool
Checks 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
Sourcepub fn display(&self) -> Display<'_>
๐ฌThis is a nightly-only experimental API. (os_str_display
#120048)
pub fn display(&self) -> Display<'_>
os_str_display
#120048)Trait Implementationsยง
1.52.0 ยท Sourceยงimpl<'a> Extend<&'a OsStr> for OsString
impl<'a> Extend<&'a OsStr> for OsString
Sourceยงfn extend<T: IntoIterator<Item = &'a OsStr>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = &'a OsStr>>(&mut self, iter: T)
Sourceยงfn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
#72631)1.52.0 ยท Sourceยงimpl<'a> Extend<Cow<'a, OsStr>> for OsString
impl<'a> Extend<Cow<'a, OsStr>> for OsString
1.52.0 ยท Sourceยงimpl Extend<OsString> for OsString
impl Extend<OsString> for OsString
Sourceยงfn extend<T: IntoIterator<Item = OsString>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = OsString>>(&mut self, iter: T)
Sourceยงfn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
#72631)