Skip to content

Commit 4558207

Browse files
committedMay 21, 2022
Expand the explanation of OsString capacity
1 parent 81e2108 commit 4558207

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed
 

‎library/std/src/ffi/os_str.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,21 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};
4545
/// values, encoded in a less-strict variant of UTF-8. This is useful to
4646
/// understand when handling capacity and length values.
4747
///
48-
/// # Capacity of OsString
48+
/// # Capacity of `OsString`
4949
///
50-
/// Capacity means UTF-8 byte size for OS strings which created from
51-
/// valid unicode, and not otherwise specified for other contents.
50+
/// Capacity uses units of UTF-8 bytes for OS strings which were created from valid unicode, and
51+
/// uses units of bytes in an unspecified encoding for other contents. On a given target, all
52+
/// `OsString` and `OsStr` values use the same units for capacity, so the following will work:
53+
/// ```
54+
/// use std::ffi::{OsStr, OsString};
55+
///
56+
/// fn concat_os_strings(a: &OsStr, b: &OsStr) -> OsString {
57+
/// let mut ret = OsString::with_capacity(a.len() + b.len()); // This will allocate
58+
/// ret.push(a); // This will not allocate further
59+
/// ret.push(b); // This will not allocate further
60+
/// ret
61+
/// }
62+
/// ```
5263
///
5364
/// # Creating an `OsString`
5465
///

0 commit comments

Comments
 (0)
Please sign in to comment.