@@ -48,12 +48,30 @@ pub struct OsStr {
48
48
49
49
impl OsString {
50
50
/// Constructs a new empty `OsString`.
51
+ ///
52
+ /// # Examples
53
+ ///
54
+ /// ```
55
+ /// use std::ffi::OsString;
56
+ ///
57
+ /// let os_string = OsString::new();
58
+ /// ```
51
59
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
52
60
pub fn new ( ) -> OsString {
53
61
OsString { inner : Buf :: from_string ( String :: new ( ) ) }
54
62
}
55
63
56
64
/// 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
+ /// ```
57
75
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
58
76
pub fn as_os_str ( & self ) -> & OsStr {
59
77
self
@@ -62,12 +80,32 @@ impl OsString {
62
80
/// Converts the `OsString` into a `String` if it contains valid Unicode data.
63
81
///
64
82
/// 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
+ /// ```
65
93
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
66
94
pub fn into_string ( self ) -> Result < String , OsString > {
67
95
self . inner . into_string ( ) . map_err ( |buf| OsString { inner : buf} )
68
96
}
69
97
70
98
/// 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
+ /// ```
71
109
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
72
110
pub fn push < T : AsRef < OsStr > > ( & mut self , s : T ) {
73
111
self . inner . push_slice ( & s. as_ref ( ) . inner )
@@ -80,6 +118,20 @@ impl OsString {
80
118
/// allocate.
81
119
///
82
120
/// 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
+ /// ```
83
135
#[ stable( feature = "osstring_simple_functions" , since = "1.9.0" ) ]
84
136
pub fn with_capacity ( capacity : usize ) -> OsString {
85
137
OsString {
@@ -88,6 +140,18 @@ impl OsString {
88
140
}
89
141
90
142
/// 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
+ /// ```
91
155
#[ stable( feature = "osstring_simple_functions" , since = "1.9.0" ) ]
92
156
pub fn clear ( & mut self ) {
93
157
self . inner . clear ( )
@@ -96,6 +160,15 @@ impl OsString {
96
160
/// Returns the capacity this `OsString` can hold without reallocating.
97
161
///
98
162
/// 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
+ /// ```
99
172
#[ stable( feature = "osstring_simple_functions" , since = "1.9.0" ) ]
100
173
pub fn capacity ( & self ) -> usize {
101
174
self . inner . capacity ( )
0 commit comments