]> git.lizzy.rs Git - rust.git/commitdiff
Expand the explanation of OsString capacity
authorJosh Triplett <josh@joshtriplett.org>
Fri, 20 May 2022 02:06:05 +0000 (19:06 -0700)
committerJosh Triplett <josh@joshtriplett.org>
Sat, 21 May 2022 20:42:47 +0000 (13:42 -0700)
library/std/src/ffi/os_str.rs

index ab02c424f6e5584e643e388885dda3accff950a4..e37d314b3e893fb435a48d605c64ef59957f7b34 100644 (file)
 /// 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 of `OsString`
 ///
-/// Capacity means UTF-8 byte size for OS strings which created from
-/// valid unicode, and not otherwise specified for other contents.
+/// 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`
 ///