10
10
11
11
//! Error handling with the `Result` type.
12
12
//!
13
- //! `Result<T, E>` is the type used for returning and propagating
14
- //! errors. It is an enum with the variants, `Ok(T)`, representing
15
- //! success and containing a value, and `Err(E)`, representing error
13
+ //! [ `Result<T, E>`][`Result`] is the type used for returning and propagating
14
+ //! errors. It is an enum with the variants, [ `Ok(T)`] , representing
15
+ //! success and containing a value, and [ `Err(E)`] , representing error
16
16
//! and containing an error value.
17
17
//!
18
18
//! ```
23
23
//! }
24
24
//! ```
25
25
//!
26
- //! Functions return `Result` whenever errors are expected and
27
- //! recoverable. In the `std` crate `Result` is most prominently used
26
+ //! Functions return [ `Result`] whenever errors are expected and
27
+ //! recoverable. In the `std` crate, [ `Result`] is most prominently used
28
28
//! for [I/O](../../std/io/index.html).
29
29
//!
30
- //! A simple function returning `Result` might be
30
+ //! A simple function returning [ `Result`] might be
31
31
//! defined and used like so:
32
32
//!
33
33
//! ```
50
50
//! }
51
51
//! ```
52
52
//!
53
- //! Pattern matching on `Result`s is clear and straightforward for
54
- //! simple cases, but `Result` comes with some convenience methods
53
+ //! Pattern matching on [ `Result`] s is clear and straightforward for
54
+ //! simple cases, but [ `Result`] comes with some convenience methods
55
55
//! that make working with it more succinct.
56
56
//!
57
57
//! ```
80
80
//!
81
81
//! A common problem with using return values to indicate errors is
82
82
//! that it is easy to ignore the return value, thus failing to handle
83
- //! the error. Result is annotated with the #[must_use] attribute,
83
+ //! the error. [` Result`] is annotated with the ` #[must_use]` attribute,
84
84
//! which will cause the compiler to issue a warning when a Result
85
- //! value is ignored. This makes `Result` especially useful with
85
+ //! value is ignored. This makes [ `Result`] especially useful with
86
86
//! functions that may encounter errors but don't otherwise return a
87
87
//! useful value.
88
88
//!
89
- //! Consider the `write_all` method defined for I/O types
90
- //! by the [`Write`](../../std/io/trait.Write.html) trait:
89
+ //! Consider the [ `write_all`] method defined for I/O types
90
+ //! by the [`Write`] trait:
91
91
//!
92
92
//! ```
93
93
//! use std::io;
97
97
//! }
98
98
//! ```
99
99
//!
100
- //! *Note: The actual definition of `Write` uses `io::Result`, which
101
- //! is just a synonym for `Result<T, io::Error>`.*
100
+ //! *Note: The actual definition of [ `Write`] uses [ `io::Result`] , which
101
+ //! is just a synonym for [ `Result`]` <T, `[` io::Error`]` >`.*
102
102
//!
103
103
//! This method doesn't produce a value, but the write may
104
104
//! fail. It's crucial to handle the error case, and *not* write
119
119
//! warning (by default, controlled by the `unused_must_use` lint).
120
120
//!
121
121
//! You might instead, if you don't want to handle the error, simply
122
- //! assert success with `expect`. This will panic if the
122
+ //! assert success with [ `expect`] . This will panic if the
123
123
//! write fails, providing a marginally useful message indicating why:
124
124
//!
125
125
//! ```{.no_run}
139
139
//! assert!(file.write_all(b"important message").is_ok());
140
140
//! ```
141
141
//!
142
- //! Or propagate the error up the call stack with `try!`:
142
+ //! Or propagate the error up the call stack with [ `try!`] :
143
143
//!
144
144
//! ```
145
145
//! # use std::fs::File;
156
156
//! # The `try!` macro
157
157
//!
158
158
//! When writing code that calls many functions that return the
159
- //! `Result` type, the error handling can be tedious. The `try!`
159
+ //! [ `Result`] type, the error handling can be tedious. The [ `try!`]
160
160
//! macro hides some of the boilerplate of propagating errors up the
161
161
//! call stack.
162
162
//!
219
219
//!
220
220
//! *It's much nicer!*
221
221
//!
222
- //! Wrapping an expression in `try!` will result in the unwrapped
223
- //! success (`Ok`) value, unless the result is `Err`, in which case
224
- //! `Err` is returned early from the enclosing function. Its simple definition
222
+ //! Wrapping an expression in [ `try!`] will result in the unwrapped
223
+ //! success ([ `Ok`] ) value, unless the result is [ `Err`] , in which case
224
+ //! [ `Err`] is returned early from the enclosing function. Its simple definition
225
225
//! makes it clear:
226
226
//!
227
227
//! ```
230
230
//! }
231
231
//! ```
232
232
//!
233
- //! `try!` is imported by the prelude and is available everywhere, but it can only
234
- //! be used in functions that return `Result` because of the early return of
235
- //! `Err` that it provides.
233
+ //! [`try!`] is imported by the prelude and is available everywhere, but it can only
234
+ //! be used in functions that return [`Result`] because of the early return of
235
+ //! [`Err`] that it provides.
236
+ //!
237
+ //! [`expect`]: enum.Result.html#method.expect
238
+ //! [`Write`]: ../../std/io/trait.Write.html
239
+ //! [`write_all`]: ../../std/io/trait.Write.html#method.write_all
240
+ //! [`io::Result`]: ../../std/io/type.Result.html
241
+ //! [`try!`]: ../../std/macro.try.html
242
+ //! [`Result`]: enum.Result.html
243
+ //! [`Ok(T)`]: enum.Result.html#variant.Ok
244
+ //! [`Err(E)`]: enum.Result.html#variant.Err
245
+ //! [`io::Error`]: ../../std/io/struct.Error.html
246
+ //! [`Ok`]: enum.Result.html#variant.Ok
247
+ //! [`Err`]: enum.Result.html#variant.Err
236
248
237
249
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
238
250
@@ -264,7 +276,7 @@ impl<T, E> Result<T, E> {
264
276
// Querying the contained values
265
277
/////////////////////////////////////////////////////////////////////////
266
278
267
- /// Returns true if the result is `Ok`
279
+ /// Returns true if the result is `Ok`.
268
280
///
269
281
/// # Examples
270
282
///
@@ -286,7 +298,7 @@ impl<T, E> Result<T, E> {
286
298
}
287
299
}
288
300
289
- /// Returns true if the result is `Err`
301
+ /// Returns true if the result is `Err`.
290
302
///
291
303
/// # Examples
292
304
///
@@ -309,11 +321,13 @@ impl<T, E> Result<T, E> {
309
321
// Adapter for each variant
310
322
/////////////////////////////////////////////////////////////////////////
311
323
312
- /// Converts from `Result<T, E>` to `Option<T>`
324
+ /// Converts from `Result<T, E>` to [ `Option<T>`].
313
325
///
314
- /// Converts `self` into an `Option<T>`, consuming `self`,
326
+ /// Converts `self` into an [ `Option<T>`] , consuming `self`,
315
327
/// and discarding the error, if any.
316
328
///
329
+ /// [`Option<T>`]: ../../std/option/enum.Option.html
330
+ ///
317
331
/// # Examples
318
332
///
319
333
/// Basic usage:
@@ -334,11 +348,13 @@ impl<T, E> Result<T, E> {
334
348
}
335
349
}
336
350
337
- /// Converts from `Result<T, E>` to `Option<E>`
351
+ /// Converts from `Result<T, E>` to [ `Option<E>`].
338
352
///
339
- /// Converts `self` into an `Option<E>`, consuming `self`,
353
+ /// Converts `self` into an [ `Option<E>`] , consuming `self`,
340
354
/// and discarding the success value, if any.
341
355
///
356
+ /// [`Option<E>`]: ../../std/option/enum.Option.html
357
+ ///
342
358
/// # Examples
343
359
///
344
360
/// Basic usage:
@@ -363,7 +379,7 @@ impl<T, E> Result<T, E> {
363
379
// Adapter for working with references
364
380
/////////////////////////////////////////////////////////////////////////
365
381
366
- /// Converts from `Result<T, E>` to `Result<&T, &E>`
382
+ /// Converts from `Result<T, E>` to `Result<&T, &E>`.
367
383
///
368
384
/// Produces a new `Result`, containing a reference
369
385
/// into the original, leaving the original in place.
@@ -388,7 +404,7 @@ impl<T, E> Result<T, E> {
388
404
}
389
405
}
390
406
391
- /// Converts from `Result<T, E>` to `Result<&mut T, &mut E>`
407
+ /// Converts from `Result<T, E>` to `Result<&mut T, &mut E>`.
392
408
///
393
409
/// # Examples
394
410
///
@@ -563,7 +579,7 @@ impl<T, E> Result<T, E> {
563
579
564
580
/// Calls `op` if the result is `Ok`, otherwise returns the `Err` value of `self`.
565
581
///
566
- /// This function can be used for control flow based on result values.
582
+ /// This function can be used for control flow based on `Result` values.
567
583
///
568
584
/// # Examples
569
585
///
@@ -646,7 +662,7 @@ impl<T, E> Result<T, E> {
646
662
}
647
663
648
664
/// Unwraps a result, yielding the content of an `Ok`.
649
- /// Else it returns `optb`.
665
+ /// Else, it returns `optb`.
650
666
///
651
667
/// # Examples
652
668
///
@@ -837,7 +853,10 @@ impl<'a, T, E> IntoIterator for &'a mut Result<T, E> {
837
853
// The Result Iterators
838
854
/////////////////////////////////////////////////////////////////////////////
839
855
840
- /// An iterator over a reference to the `Ok` variant of a `Result`.
856
+ /// An iterator over a reference to the [`Ok`] variant of a [`Result`].
857
+ ///
858
+ /// [`Ok`]: enum.Result.html#variant.Ok
859
+ /// [`Result`]: enum.Result.html
841
860
#[ derive( Debug ) ]
842
861
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
843
862
pub struct Iter < ' a , T : ' a > { inner : Option < & ' a T > }
@@ -872,7 +891,10 @@ impl<'a, T> Clone for Iter<'a, T> {
872
891
fn clone ( & self ) -> Iter < ' a , T > { Iter { inner : self . inner } }
873
892
}
874
893
875
- /// An iterator over a mutable reference to the `Ok` variant of a `Result`.
894
+ /// An iterator over a mutable reference to the [`Ok`] variant of a [`Result`].
895
+ ///
896
+ /// [`Ok`]: enum.Result.html#variant.Ok
897
+ /// [`Result`]: enum.Result.html
876
898
#[ derive( Debug ) ]
877
899
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
878
900
pub struct IterMut < ' a , T : ' a > { inner : Option < & ' a mut T > }
@@ -902,10 +924,11 @@ impl<'a, T> ExactSizeIterator for IterMut<'a, T> {}
902
924
#[ unstable( feature = "fused" , issue = "35602" ) ]
903
925
impl < ' a , T > FusedIterator for IterMut < ' a , T > { }
904
926
905
- /// An iterator over the value in a `Ok` variant of a `Result`. This struct is
927
+ /// An iterator over the value in a [ `Ok`] variant of a [ `Result`] . This struct is
906
928
/// created by the [`into_iter`] method on [`Result`][`Result`] (provided by
907
929
/// the [`IntoIterator`] trait).
908
930
///
931
+ /// [`Ok`]: enum.Result.html#variant.Ok
909
932
/// [`Result`]: enum.Result.html
910
933
/// [`into_iter`]: ../iter/trait.IntoIterator.html#tymethod.into_iter
911
934
/// [`IntoIterator`]: ../iter/trait.IntoIterator.html
0 commit comments