Skip to content

Commit e3153cf

Browse files
Improve Result doc
1 parent 57a6037 commit e3153cf

File tree

1 file changed

+59
-36
lines changed

1 file changed

+59
-36
lines changed

src/libcore/result.rs

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
//! Error handling with the `Result` type.
1212
//!
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
1616
//! and containing an error value.
1717
//!
1818
//! ```
@@ -23,11 +23,11 @@
2323
//! }
2424
//! ```
2525
//!
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
2828
//! for [I/O](../../std/io/index.html).
2929
//!
30-
//! A simple function returning `Result` might be
30+
//! A simple function returning [`Result`] might be
3131
//! defined and used like so:
3232
//!
3333
//! ```
@@ -50,8 +50,8 @@
5050
//! }
5151
//! ```
5252
//!
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
5555
//! that make working with it more succinct.
5656
//!
5757
//! ```
@@ -80,14 +80,14 @@
8080
//!
8181
//! A common problem with using return values to indicate errors is
8282
//! 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,
8484
//! 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
8686
//! functions that may encounter errors but don't otherwise return a
8787
//! useful value.
8888
//!
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:
9191
//!
9292
//! ```
9393
//! use std::io;
@@ -97,8 +97,8 @@
9797
//! }
9898
//! ```
9999
//!
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`]`>`.*
102102
//!
103103
//! This method doesn't produce a value, but the write may
104104
//! fail. It's crucial to handle the error case, and *not* write
@@ -119,7 +119,7 @@
119119
//! warning (by default, controlled by the `unused_must_use` lint).
120120
//!
121121
//! 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
123123
//! write fails, providing a marginally useful message indicating why:
124124
//!
125125
//! ```{.no_run}
@@ -139,7 +139,7 @@
139139
//! assert!(file.write_all(b"important message").is_ok());
140140
//! ```
141141
//!
142-
//! Or propagate the error up the call stack with `try!`:
142+
//! Or propagate the error up the call stack with [`try!`]:
143143
//!
144144
//! ```
145145
//! # use std::fs::File;
@@ -156,7 +156,7 @@
156156
//! # The `try!` macro
157157
//!
158158
//! 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!`]
160160
//! macro hides some of the boilerplate of propagating errors up the
161161
//! call stack.
162162
//!
@@ -219,9 +219,9 @@
219219
//!
220220
//! *It's much nicer!*
221221
//!
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
225225
//! makes it clear:
226226
//!
227227
//! ```
@@ -230,9 +230,21 @@
230230
//! }
231231
//! ```
232232
//!
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
236248
237249
#![stable(feature = "rust1", since = "1.0.0")]
238250

@@ -264,7 +276,7 @@ impl<T, E> Result<T, E> {
264276
// Querying the contained values
265277
/////////////////////////////////////////////////////////////////////////
266278

267-
/// Returns true if the result is `Ok`
279+
/// Returns true if the result is `Ok`.
268280
///
269281
/// # Examples
270282
///
@@ -286,7 +298,7 @@ impl<T, E> Result<T, E> {
286298
}
287299
}
288300

289-
/// Returns true if the result is `Err`
301+
/// Returns true if the result is `Err`.
290302
///
291303
/// # Examples
292304
///
@@ -309,11 +321,13 @@ impl<T, E> Result<T, E> {
309321
// Adapter for each variant
310322
/////////////////////////////////////////////////////////////////////////
311323

312-
/// Converts from `Result<T, E>` to `Option<T>`
324+
/// Converts from `Result<T, E>` to [`Option<T>`].
313325
///
314-
/// Converts `self` into an `Option<T>`, consuming `self`,
326+
/// Converts `self` into an [`Option<T>`], consuming `self`,
315327
/// and discarding the error, if any.
316328
///
329+
/// [`Option<T>`]: ../../std/option/enum.Option.html
330+
///
317331
/// # Examples
318332
///
319333
/// Basic usage:
@@ -334,11 +348,13 @@ impl<T, E> Result<T, E> {
334348
}
335349
}
336350

337-
/// Converts from `Result<T, E>` to `Option<E>`
351+
/// Converts from `Result<T, E>` to [`Option<E>`].
338352
///
339-
/// Converts `self` into an `Option<E>`, consuming `self`,
353+
/// Converts `self` into an [`Option<E>`], consuming `self`,
340354
/// and discarding the success value, if any.
341355
///
356+
/// [`Option<E>`]: ../../std/option/enum.Option.html
357+
///
342358
/// # Examples
343359
///
344360
/// Basic usage:
@@ -363,7 +379,7 @@ impl<T, E> Result<T, E> {
363379
// Adapter for working with references
364380
/////////////////////////////////////////////////////////////////////////
365381

366-
/// Converts from `Result<T, E>` to `Result<&T, &E>`
382+
/// Converts from `Result<T, E>` to `Result<&T, &E>`.
367383
///
368384
/// Produces a new `Result`, containing a reference
369385
/// into the original, leaving the original in place.
@@ -388,7 +404,7 @@ impl<T, E> Result<T, E> {
388404
}
389405
}
390406

391-
/// Converts from `Result<T, E>` to `Result<&mut T, &mut E>`
407+
/// Converts from `Result<T, E>` to `Result<&mut T, &mut E>`.
392408
///
393409
/// # Examples
394410
///
@@ -563,7 +579,7 @@ impl<T, E> Result<T, E> {
563579

564580
/// Calls `op` if the result is `Ok`, otherwise returns the `Err` value of `self`.
565581
///
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.
567583
///
568584
/// # Examples
569585
///
@@ -646,7 +662,7 @@ impl<T, E> Result<T, E> {
646662
}
647663

648664
/// Unwraps a result, yielding the content of an `Ok`.
649-
/// Else it returns `optb`.
665+
/// Else, it returns `optb`.
650666
///
651667
/// # Examples
652668
///
@@ -837,7 +853,10 @@ impl<'a, T, E> IntoIterator for &'a mut Result<T, E> {
837853
// The Result Iterators
838854
/////////////////////////////////////////////////////////////////////////////
839855

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
841860
#[derive(Debug)]
842861
#[stable(feature = "rust1", since = "1.0.0")]
843862
pub struct Iter<'a, T: 'a> { inner: Option<&'a T> }
@@ -872,7 +891,10 @@ impl<'a, T> Clone for Iter<'a, T> {
872891
fn clone(&self) -> Iter<'a, T> { Iter { inner: self.inner } }
873892
}
874893

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
876898
#[derive(Debug)]
877899
#[stable(feature = "rust1", since = "1.0.0")]
878900
pub struct IterMut<'a, T: 'a> { inner: Option<&'a mut T> }
@@ -902,10 +924,11 @@ impl<'a, T> ExactSizeIterator for IterMut<'a, T> {}
902924
#[unstable(feature = "fused", issue = "35602")]
903925
impl<'a, T> FusedIterator for IterMut<'a, T> {}
904926

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
906928
/// created by the [`into_iter`] method on [`Result`][`Result`] (provided by
907929
/// the [`IntoIterator`] trait).
908930
///
931+
/// [`Ok`]: enum.Result.html#variant.Ok
909932
/// [`Result`]: enum.Result.html
910933
/// [`into_iter`]: ../iter/trait.IntoIterator.html#tymethod.into_iter
911934
/// [`IntoIterator`]: ../iter/trait.IntoIterator.html

0 commit comments

Comments
 (0)