Skip to content

Commit 20889fd

Browse files
committed
add more to result overview
1 parent a5e2b69 commit 20889fd

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

core/src/result.rs

+181
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,103 @@
227227
//!
228228
//! # Method overview
229229
//!
230+
//! In addition to working with pattern matching, [`Result`] provides a
231+
//! wide variety of different methods.
232+
//!
233+
//! ## Querying the variant
234+
//!
235+
//! The [`is_ok`] and [`is_err`] methods return [`true`] if the [`Result`]
236+
//! is [`Ok`] or [`Err`], respectively.
237+
//!
238+
//! [`is_err`]: Result::is_err
239+
//! [`is_ok`]: Result::is_ok
240+
//!
241+
//! ## Adapters for working with references
242+
//!
243+
//! * [`as_ref`] converts from `&Result<T, E>` to `Result<&T, &E>`
244+
//! * [`as_mut`] converts from `&mut Result<T, E>` to `Result<&mut T, &mut E>`
245+
//! * [`as_deref`] converts from `&Result<T>` to `Result<&T::Target, &E>`
246+
//! * [`as_deref_mut`] converts from `&mut Result<T>` to `Result<&mut T::Target, &mut E>`
247+
//!
248+
//! [`as_deref`]: Result::as_deref
249+
//! [`as_deref_mut`]: Result::as_deref_mut
250+
//! [`as_mut`]: Result::as_mut
251+
//! [`as_ref`]: Result::as_ref
252+
//!
253+
//! ## Extracting contained values
254+
//!
255+
//! These methods extract the contained value in a [`Result`] when it is
256+
//! the [`Ok`] variant. If the [`Result`] is [`Err`]:
257+
//!
258+
//! * [`expect`] panics with a provided custom message
259+
//! * [`unwrap`] panics with a generic message
260+
//! * [`unwrap_or`] returns the provided default value
261+
//! * [`unwrap_or_default`] returns the default value of the type `T`
262+
//! (which must implement the [`Default`] trait)
263+
//! * [`unwrap_or_else`] returns the result of evaluating the provided
264+
//! function
265+
//!
266+
//! The panicking methods [`expect`] and [`unwrap`] require `E` to
267+
//! implement the [`Debug`] trait.
268+
//!
269+
//! These methods extract the contained value in a [`Result`] when it is
270+
//! the [`Err`] variant. They require `T` to implement the [`Debug`] trait.
271+
//! If the [`Result`] is [`Ok`]:
272+
//!
273+
//! * [`expect_err`] panics with a provided custom message
274+
//! * [`unwrap_err`] panics with a generic message
275+
//!
276+
//! [`Debug`]: crate::fmt::Debug
277+
//! [`Default`]: crate::default::Default
278+
//! [`expect`]: Result::expect
279+
//! [`expect_err`]: Result::expect_err
280+
//! [`unwrap`]: Result::unwrap
281+
//! [`unwrap_err`]: Result::unwrap_err
282+
//! [`unwrap_or`]: Result::unwrap_or
283+
//! [`unwrap_or_default`]: Result::unwrap_or_default
284+
//! [`unwrap_or_else`]: Result::unwrap_or_else
285+
//!
286+
//! ## Transforming contained values
287+
//!
288+
//! These transformations are from [`Result`] to [`Option`]:
289+
//!
290+
//! * [`err`][Result::err] transforms [`Result<T, E>`] into [`Option<E>`],
291+
//! mapping [`Err(e)`] to [`Some(e)`] and [`Ok(v)`] to [`None`]
292+
//! * [`ok`][Result::ok] transforms [`Result<T, E>`] into [`Option<T>`],
293+
//! mapping [`Ok(v)`] to [`Some(v)`] and [`Err(e)`] to [`None`]
294+
//! * [`transpose`] transposes a [`Result`] of an [`Option`] into an
295+
//! [`Option`] of a [`Result`]
296+
//!
297+
//! These transformations are on [`Ok`] values:
298+
//!
299+
//! * [`map`] transforms [`Result<T, E>`] into [`Result<U, E>`] by applying
300+
//! the provided function to the contained value of [`Ok`] and leaving
301+
//! [`Err`] values unchanged
302+
//! * [`map_or`] transforms [`Result<T, E>`] into a value of `U` by
303+
//! applying the provided function to the contained value of [`Ok`], or
304+
//! returns the provided default value of `U` if the [`Result`] is
305+
//! [`Err`]
306+
//! * [`map_or_else`] transforms [`Result<T, E>`] into a value of `U` by
307+
//! applying the provided function to the contained value of [`Ok`], or
308+
//! applies the provided fallback function to the contained value of
309+
//! [`Err`]
310+
//!
311+
//! This transformation is on [`Err`] values:
312+
//!
313+
//! * [`map_err`] transforms [`Result<T, E>`] into [`Result<T, F>`] by
314+
//! applying the provided function to the contained value of [`Err`] and
315+
//! leaving [`Ok`] values unchanged
316+
//!
317+
//! [`Err(e)`]: Err
318+
//! [`Ok(v)`]: Ok
319+
//! [`Some(e)`]: Option::Some
320+
//! [`Some(v)`]: Option::Some
321+
//! [`map`]: Result::map
322+
//! [`map_err`]: Result::map_err
323+
//! [`map_or`]: Result::map_or
324+
//! [`map_or_else`]: Result::map_or_else
325+
//! [`transpose`]: Result::transpose
326+
//!
230327
//! ## Boolean operators
231328
//!
232329
//! These methods treat the [`Result`] as a boolean value, where [`Ok`]
@@ -269,6 +366,90 @@
269366
//! [`and_then`]: Result::and_then
270367
//! [`or`]: Result::or
271368
//! [`or_else`]: Result::or_else
369+
//!
370+
//! ## Iterating over `Result`
371+
//!
372+
//! A [`Result`] can be iterated over. This can be helpful if you need an
373+
//! iterator that is conditionally empty. The iterator will either produce
374+
//! a single value (when the [`Result`] is [`Ok`]), or produce no values
375+
//! (when the [`Result`] is [`Err`]). For example, [`into_iter`] acts like
376+
//! [`once(v)`] if the [`Result`] is [`Ok(v)`], and like [`empty()`] if
377+
//! the [`Result`] is [`Err(err)`].
378+
//!
379+
//! Iterators over [`Result`] come in three types:
380+
//!
381+
//! * [`into_iter`] consumes the [`Result`] and produces the contained
382+
//! value
383+
//! * [`iter`] produces an immutable reference of type `&T` to the
384+
//! contained value
385+
//! * [`iter_mut`] produces a mutable reference of type `&mut T` to the
386+
//! contained value
387+
//!
388+
//! See [Iterating over `Option`] for examples of how this can be useful.
389+
//!
390+
//! [`Err(err)`]: Err
391+
//! [Iterating over `Option`]: crate::option#iterating-over-option
392+
//! [`Ok(v)`]: Ok
393+
//! [`empty()`]: crate::iter::empty
394+
//! [`into_iter`]: Result::into_iter
395+
//! [`iter`]: Result::iter
396+
//! [`iter_mut`]: Result::iter_mut
397+
//! [`once(v)`]: crate::iter::once
398+
//!
399+
//! You might want to use an iterator chain to do multiple instances of an
400+
//! operation that can fail, but would like to ignore failures while
401+
//! continuing to process the successful results. In this example, we take
402+
//! advantage of the iterable nature of [`Result`] to select only the
403+
//! [`Ok`] values using [`flatten`].
404+
//!
405+
//! [`flatten`]: crate::iter::Iterator::flatten
406+
//!
407+
//! ```
408+
//! # use std::str::FromStr;
409+
//! let mut results = vec![];
410+
//! let mut errs = vec![];
411+
//! let nums: Vec<_> = vec!["17", "not a number", "99", "-27", "768"]
412+
//! .into_iter()
413+
//! .map(u8::from_str)
414+
//! // Save clones of the raw `Result` values to inspect
415+
//! .inspect(|x| results.push(x.clone()))
416+
//! // Challenge: explain how this captures only the `Err` values
417+
//! .inspect(|x| errs.extend(x.clone().err()))
418+
//! .flatten()
419+
//! .collect();
420+
//! assert_eq!(errs.len(), 3);
421+
//! assert_eq!(nums, [17, 99]);
422+
//! println!("results {:?}", results);
423+
//! println!("errs {:?}", errs);
424+
//! println!("nums {:?}", nums);
425+
//! ```
426+
//!
427+
//! ## Collecting into `Result`
428+
//!
429+
//! [`Result`] implements the [`FromIterator`] trait, which allows an
430+
//! iterator over [`Result`] values to be collected into a [`Result`] of a
431+
//! collection of each contained value of the original [`Result`] values,
432+
//! or [`Err`] if any of the elements was [`Err`].
433+
//!
434+
//! [`FromIterator`]: Result#impl-FromIterator%3CResult%3CA%2C%20E%3E%3E
435+
//!
436+
//! ```
437+
//! let v = vec![Ok(2), Ok(4), Err("err!"), Ok(8)];
438+
//! let res: Result<Vec<_>, &str> = v.into_iter().collect();
439+
//! assert_eq!(res, Err("err!"));
440+
//! let v = vec![Ok(2), Ok(4), Ok(8)];
441+
//! let res: Result<Vec<_>, &str> = v.into_iter().collect();
442+
//! assert_eq!(res, Ok(vec![2, 4, 8]));
443+
//! ```
444+
//!
445+
//! [`Result`] also implements the [`Product`] and [`Sum`] traits, allowing
446+
//! an iterator over [`Result`] values to provide the
447+
//! [`product`][m.product] and [`sum`][m.sum] methods.
448+
//!
449+
//! [`Product`]: Result#impl-Product%3CResult%3CU%2C%20E%3E%3E
450+
//! [`Sum`]: Result#impl-Sum%3CResult%3CU%2C%20E%3E%3E
451+
//! [m.product]: crate::iter::Iterator::product
452+
//! [m.sum]: crate::iter::Iterator::sum
272453
273454
#![stable(feature = "rust1", since = "1.0.0")]
274455

0 commit comments

Comments
 (0)