Skip to content

Commit e805cb6

Browse files
Add io::Error doc examples
1 parent 1523a54 commit e805cb6

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

src/libstd/io/error.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ impl Error {
208208
/// This function reads the value of `errno` for the target platform (e.g.
209209
/// `GetLastError` on Windows) and will return a corresponding instance of
210210
/// `Error` for the error code.
211+
///
212+
/// # Examples
213+
///
214+
/// ```
215+
/// use std::io::Error;
216+
///
217+
/// println!("last OS error: {:?}", Error::last_os_error());
218+
/// ```
211219
#[stable(feature = "rust1", since = "1.0.0")]
212220
pub fn last_os_error() -> Error {
213221
Error::from_raw_os_error(sys::os::errno() as i32)
@@ -248,6 +256,27 @@ impl Error {
248256
/// If this `Error` was constructed via `last_os_error` or
249257
/// `from_raw_os_error`, then this function will return `Some`, otherwise
250258
/// it will return `None`.
259+
///
260+
/// # Examples
261+
///
262+
/// ```
263+
/// use std::io::{Error, ErrorKind};
264+
///
265+
/// fn print_os_error(err: &Error) {
266+
/// if let Some(raw_os_err) = err.raw_os_error() {
267+
/// println!("raw OS error: {:?}", raw_os_err);
268+
/// } else {
269+
/// println!("Not an OS error");
270+
/// }
271+
/// }
272+
///
273+
/// fn main() {
274+
/// // Will print "raw OS error: ...".
275+
/// print_os_error(&Error::last_os_error());
276+
/// // Will print "Not an OS error".
277+
/// print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
278+
/// }
279+
/// ```
251280
#[stable(feature = "rust1", since = "1.0.0")]
252281
pub fn raw_os_error(&self) -> Option<i32> {
253282
match self.repr {
@@ -260,6 +289,27 @@ impl Error {
260289
///
261290
/// If this `Error` was constructed via `new` then this function will
262291
/// return `Some`, otherwise it will return `None`.
292+
///
293+
/// # Examples
294+
///
295+
/// ```
296+
/// use std::io::{Error, ErrorKind};
297+
///
298+
/// fn print_error(err: &Error) {
299+
/// if let Some(inner_err) = err.get_ref() {
300+
/// println!("Inner error: {:?}", inner_err);
301+
/// } else {
302+
/// println!("No inner error");
303+
/// }
304+
/// }
305+
///
306+
/// fn main() {
307+
/// // Will print "No inner error".
308+
/// print_error(&Error::last_os_error());
309+
/// // Will print "Inner error: ...".
310+
/// print_error(&Error::new(ErrorKind::Other, "oh no!"));
311+
/// }
312+
/// ```
263313
#[stable(feature = "io_error_inner", since = "1.3.0")]
264314
pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> {
265315
match self.repr {
@@ -273,6 +323,63 @@ impl Error {
273323
///
274324
/// If this `Error` was constructed via `new` then this function will
275325
/// return `Some`, otherwise it will return `None`.
326+
///
327+
/// # Examples
328+
///
329+
/// ```
330+
/// use std::io::{Error, ErrorKind};
331+
/// use std::{error, fmt};
332+
/// use std::fmt::Display;
333+
///
334+
/// #[derive(Debug)]
335+
/// struct MyError {
336+
/// v: String,
337+
/// }
338+
///
339+
/// impl MyError {
340+
/// fn new() -> MyError {
341+
/// MyError {
342+
/// v: "oh no!".to_owned()
343+
/// }
344+
/// }
345+
///
346+
/// fn change_message(&mut self, new_message: &str) {
347+
/// self.v = new_message.to_owned();
348+
/// }
349+
/// }
350+
///
351+
/// impl error::Error for MyError {
352+
/// fn description(&self) -> &str { &self.v }
353+
/// }
354+
///
355+
/// impl Display for MyError {
356+
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
357+
/// write!(f, "MyError: {}", &self.v)
358+
/// }
359+
/// }
360+
///
361+
/// fn change_error(mut err: Error) -> Error {
362+
/// if let Some(inner_err) = err.get_mut() {
363+
/// inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
364+
/// }
365+
/// err
366+
/// }
367+
///
368+
/// fn print_error(err: &Error) {
369+
/// if let Some(inner_err) = err.get_ref() {
370+
/// println!("Inner error: {}", inner_err);
371+
/// } else {
372+
/// println!("No inner error");
373+
/// }
374+
/// }
375+
///
376+
/// fn main() {
377+
/// // Will print "No inner error".
378+
/// print_error(&change_error(Error::last_os_error()));
379+
/// // Will print "Inner error: ...".
380+
/// print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
381+
/// }
382+
/// ```
276383
#[stable(feature = "io_error_inner", since = "1.3.0")]
277384
pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> {
278385
match self.repr {
@@ -285,6 +392,27 @@ impl Error {
285392
///
286393
/// If this `Error` was constructed via `new` then this function will
287394
/// return `Some`, otherwise it will return `None`.
395+
///
396+
/// # Examples
397+
///
398+
/// ```
399+
/// use std::io::{Error, ErrorKind};
400+
///
401+
/// fn print_error(err: Error) {
402+
/// if let Some(inner_err) = err.into_inner() {
403+
/// println!("Inner error: {}", inner_err);
404+
/// } else {
405+
/// println!("No inner error");
406+
/// }
407+
/// }
408+
///
409+
/// fn main() {
410+
/// // Will print "No inner error".
411+
/// print_error(Error::last_os_error());
412+
/// // Will print "Inner error: ...".
413+
/// print_error(Error::new(ErrorKind::Other, "oh no!"));
414+
/// }
415+
/// ```
288416
#[stable(feature = "io_error_inner", since = "1.3.0")]
289417
pub fn into_inner(self) -> Option<Box<error::Error+Send+Sync>> {
290418
match self.repr {
@@ -294,6 +422,23 @@ impl Error {
294422
}
295423

296424
/// Returns the corresponding `ErrorKind` for this error.
425+
///
426+
/// # Examples
427+
///
428+
/// ```
429+
/// use std::io::{Error, ErrorKind};
430+
///
431+
/// fn print_error(err: Error) {
432+
/// println!("{:?}", err.kind());
433+
/// }
434+
///
435+
/// fn main() {
436+
/// // Will print "No inner error".
437+
/// print_error(Error::last_os_error());
438+
/// // Will print "Inner error: ...".
439+
/// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
440+
/// }
441+
/// ```
297442
#[stable(feature = "rust1", since = "1.0.0")]
298443
pub fn kind(&self) -> ErrorKind {
299444
match self.repr {

0 commit comments

Comments
 (0)