From 40eaab17de1ee63491000a02a555aab6398f8376 Mon Sep 17 00:00:00 2001 From: Anton Golov Date: Sun, 1 Aug 2021 13:16:37 +0200 Subject: [PATCH] Add documentation for the order of Option and Result --- library/core/src/option.rs | 13 +++++++++++++ library/core/src/result.rs | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 8057ff0759107..d4e9c384f9302 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -285,6 +285,19 @@ //! assert_eq!(res, ["error!", "error!", "foo", "error!", "bar"]); //! ``` //! +//! ## Comparison operators +//! +//! If `T` implements [`PartialOrd`] then [`Option`] will derive its +//! [`PartialOrd`] implementation. With this order, [`None`] compares as +//! less than any [`Some`], and two [`Some`] compare the same way as their +//! contained values would in `T`. If `T` also implements +//! [`Ord`], then so does [`Option`]. +//! +//! ``` +//! assert!(None < Some(0)); +//! assert!(Some(0) < Some(1)); +//! ``` +//! //! ## Iterating over `Option` //! //! An [`Option`] can be iterated over. This can be helpful if you need an diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 53aaa5219b1d4..861790e8a4099 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -379,6 +379,24 @@ //! [`and_then`]: Result::and_then //! [`or_else`]: Result::or_else //! +//! ## Comparison operators +//! +//! If `T` and `E` both implement [`PartialOrd`] then [`Result`] will +//! derive its [`PartialOrd`] implementation. With this order, an [`Ok`] +//! compares as less than any [`Err`], while two [`Ok`] or two [`Err`] +//! compare as their contained values would in `T` or `E` respectively. If `T` +//! and `E` both also implement [`Ord`], then so does [`Result`]. +//! +//! ``` +//! assert!(Ok(1) < Err(0)); +//! let x: Result = Ok(0); +//! let y = Ok(1); +//! assert!(x < y); +//! let x: Result<(), i32> = Err(0); +//! let y = Err(1); +//! assert!(x < y); +//! ``` +//! //! ## Iterating over `Result` //! //! A [`Result`] can be iterated over. This can be helpful if you need an