@@ -128,6 +128,17 @@ impl File {
128
128
///
129
129
/// This function will return an error if `path` does not already exist.
130
130
/// Other errors may also be returned according to `OpenOptions::open`.
131
+ ///
132
+ /// # Examples
133
+ ///
134
+ /// ```no_run
135
+ /// use std::fs::File;
136
+ ///
137
+ /// # fn foo() -> std::io::Result<()> {
138
+ /// let mut f = try!(File::open("foo.txt"));
139
+ /// # Ok(())
140
+ /// # }
141
+ /// ```
131
142
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
132
143
pub fn open < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
133
144
OpenOptions :: new ( ) . read ( true ) . open ( path)
@@ -139,6 +150,17 @@ impl File {
139
150
/// and will truncate it if it does.
140
151
///
141
152
/// See the `OpenOptions::open` function for more details.
153
+ ///
154
+ /// # Examples
155
+ ///
156
+ /// ```no_run
157
+ /// use std::fs::File;
158
+ ///
159
+ /// # fn foo() -> std::io::Result<()> {
160
+ /// let mut f = try!(File::create("foo.txt"));
161
+ /// # Ok(())
162
+ /// # }
163
+ /// ```
142
164
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
143
165
pub fn create < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
144
166
OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path)
@@ -156,6 +178,20 @@ impl File {
156
178
///
157
179
/// This function will attempt to ensure that all in-core data reaches the
158
180
/// filesystem before returning.
181
+ ///
182
+ /// # Examples
183
+ ///
184
+ /// ```no_run
185
+ /// use std::fs::File;
186
+ ///
187
+ /// # fn foo() -> std::io::Result<()> {
188
+ /// let mut f = try!(File::create("foo.txt"));
189
+ /// try!(f.write_all(b"Hello, world!"));
190
+ ///
191
+ /// try!(f.sync_all());
192
+ /// # Ok(())
193
+ /// # }
194
+ /// ```
159
195
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
160
196
pub fn sync_all ( & self ) -> io:: Result < ( ) > {
161
197
self . inner . fsync ( )
@@ -170,6 +206,20 @@ impl File {
170
206
///
171
207
/// Note that some platforms may simply implement this in terms of
172
208
/// `sync_all`.
209
+ ///
210
+ /// # Examples
211
+ ///
212
+ /// ```no_run
213
+ /// use std::fs::File;
214
+ ///
215
+ /// # fn foo() -> std::io::Result<()> {
216
+ /// let mut f = try!(File::create("foo.txt"));
217
+ /// try!(f.write_all(b"Hello, world!"));
218
+ ///
219
+ /// try!(f.sync_data());
220
+ /// # Ok(())
221
+ /// # }
222
+ /// ```
173
223
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
174
224
pub fn sync_data ( & self ) -> io:: Result < ( ) > {
175
225
self . inner . datasync ( )
@@ -182,12 +232,36 @@ impl File {
182
232
/// be shrunk. If it is greater than the current file's size, then the file
183
233
/// will be extended to `size` and have all of the intermediate data filled
184
234
/// in with 0s.
235
+ ///
236
+ /// # Examples
237
+ ///
238
+ /// ```no_run
239
+ /// use std::fs::File;
240
+ ///
241
+ /// # fn foo() -> std::io::Result<()> {
242
+ /// let mut f = try!(File::open("foo.txt"));
243
+ /// try!(f.set_len(0));
244
+ /// # Ok(())
245
+ /// # }
246
+ /// ```
185
247
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
186
248
pub fn set_len ( & self , size : u64 ) -> io:: Result < ( ) > {
187
249
self . inner . truncate ( size)
188
250
}
189
251
190
252
/// Queries metadata about the underlying file.
253
+ ///
254
+ /// # Examples
255
+ ///
256
+ /// ```no_run
257
+ /// use std::fs::File;
258
+ ///
259
+ /// # fn foo() -> std::io::Result<()> {
260
+ /// let mut f = try!(File::open("foo.txt"));
261
+ /// let metadata = try!(f.metadata());
262
+ /// # Ok(())
263
+ /// # }
264
+ /// ```
191
265
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
192
266
pub fn metadata ( & self ) -> io:: Result < Metadata > {
193
267
self . inner . file_attr ( ) . map ( Metadata )
0 commit comments