Skip to content

Commit 44858b8

Browse files
committed
Auto merge of #39221 - frewsxcv:os-string-docs, r=GuillaumeGomez
Add doc examples for `std::ffi::OsString` fucntions/methods. None
2 parents e5b0829 + 47143e3 commit 44858b8

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/libstd/ffi/os_str.rs

+73
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,30 @@ pub struct OsStr {
4848

4949
impl OsString {
5050
/// Constructs a new empty `OsString`.
51+
///
52+
/// # Examples
53+
///
54+
/// ```
55+
/// use std::ffi::OsString;
56+
///
57+
/// let os_string = OsString::new();
58+
/// ```
5159
#[stable(feature = "rust1", since = "1.0.0")]
5260
pub fn new() -> OsString {
5361
OsString { inner: Buf::from_string(String::new()) }
5462
}
5563

5664
/// Converts to an `OsStr` slice.
65+
///
66+
/// # Examples
67+
///
68+
/// ```
69+
/// use std::ffi::{OsString, OsStr};
70+
///
71+
/// let os_string = OsString::from("foo");
72+
/// let os_str = OsStr::new("foo");
73+
/// assert_eq!(os_string.as_os_str(), os_str);
74+
/// ```
5775
#[stable(feature = "rust1", since = "1.0.0")]
5876
pub fn as_os_str(&self) -> &OsStr {
5977
self
@@ -62,12 +80,32 @@ impl OsString {
6280
/// Converts the `OsString` into a `String` if it contains valid Unicode data.
6381
///
6482
/// On failure, ownership of the original `OsString` is returned.
83+
///
84+
/// # Examples
85+
///
86+
/// ```
87+
/// use std::ffi::OsString;
88+
///
89+
/// let os_string = OsString::from("foo");
90+
/// let string = os_string.into_string();
91+
/// assert_eq!(string, Ok(String::from("foo")));
92+
/// ```
6593
#[stable(feature = "rust1", since = "1.0.0")]
6694
pub fn into_string(self) -> Result<String, OsString> {
6795
self.inner.into_string().map_err(|buf| OsString { inner: buf} )
6896
}
6997

7098
/// Extends the string with the given `&OsStr` slice.
99+
///
100+
/// # Examples
101+
///
102+
/// ```
103+
/// use std::ffi::OsString;
104+
///
105+
/// let mut os_string = OsString::from("foo");
106+
/// os_string.push("bar");
107+
/// assert_eq!(&os_string, "foobar");
108+
/// ```
71109
#[stable(feature = "rust1", since = "1.0.0")]
72110
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
73111
self.inner.push_slice(&s.as_ref().inner)
@@ -80,6 +118,20 @@ impl OsString {
80118
/// allocate.
81119
///
82120
/// See main `OsString` documentation information about encoding.
121+
///
122+
/// # Examples
123+
///
124+
/// ```
125+
/// use std::ffi::OsString;
126+
///
127+
/// let mut os_string = OsString::with_capacity(10);
128+
/// let capacity = os_string.capacity();
129+
///
130+
/// // This push is done without reallocating
131+
/// os_string.push("foo");
132+
///
133+
/// assert_eq!(capacity, os_string.capacity());
134+
/// ```
83135
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
84136
pub fn with_capacity(capacity: usize) -> OsString {
85137
OsString {
@@ -88,6 +140,18 @@ impl OsString {
88140
}
89141

90142
/// Truncates the `OsString` to zero length.
143+
///
144+
/// # Examples
145+
///
146+
/// ```
147+
/// use std::ffi::OsString;
148+
///
149+
/// let mut os_string = OsString::from("foo");
150+
/// assert_eq!(&os_string, "foo");
151+
///
152+
/// os_string.clear();
153+
/// assert_eq!(&os_string, "");
154+
/// ```
91155
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
92156
pub fn clear(&mut self) {
93157
self.inner.clear()
@@ -96,6 +160,15 @@ impl OsString {
96160
/// Returns the capacity this `OsString` can hold without reallocating.
97161
///
98162
/// See `OsString` introduction for information about encoding.
163+
///
164+
/// # Examples
165+
///
166+
/// ```
167+
/// use std::ffi::OsString;
168+
///
169+
/// let mut os_string = OsString::with_capacity(10);
170+
/// assert!(os_string.capacity() >= 10);
171+
/// ```
99172
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
100173
pub fn capacity(&self) -> usize {
101174
self.inner.capacity()

0 commit comments

Comments
 (0)