Skip to content

Commit b1166e1

Browse files
authored
Rollup merge of #87654 - jesyspa:issue-87238-option-result-doc, r=scottmcm
Add documentation for the order of Option and Result This resolves issue #87238.
2 parents 14f3418 + 40eaab1 commit b1166e1

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

library/core/src/option.rs

+13
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,19 @@
285285
//! assert_eq!(res, ["error!", "error!", "foo", "error!", "bar"]);
286286
//! ```
287287
//!
288+
//! ## Comparison operators
289+
//!
290+
//! If `T` implements [`PartialOrd`] then [`Option<T>`] will derive its
291+
//! [`PartialOrd`] implementation. With this order, [`None`] compares as
292+
//! less than any [`Some`], and two [`Some`] compare the same way as their
293+
//! contained values would in `T`. If `T` also implements
294+
//! [`Ord`], then so does [`Option<T>`].
295+
//!
296+
//! ```
297+
//! assert!(None < Some(0));
298+
//! assert!(Some(0) < Some(1));
299+
//! ```
300+
//!
288301
//! ## Iterating over `Option`
289302
//!
290303
//! An [`Option`] can be iterated over. This can be helpful if you need an

library/core/src/result.rs

+18
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,24 @@
379379
//! [`and_then`]: Result::and_then
380380
//! [`or_else`]: Result::or_else
381381
//!
382+
//! ## Comparison operators
383+
//!
384+
//! If `T` and `E` both implement [`PartialOrd`] then [`Result<T, E>`] will
385+
//! derive its [`PartialOrd`] implementation. With this order, an [`Ok`]
386+
//! compares as less than any [`Err`], while two [`Ok`] or two [`Err`]
387+
//! compare as their contained values would in `T` or `E` respectively. If `T`
388+
//! and `E` both also implement [`Ord`], then so does [`Result<T, E>`].
389+
//!
390+
//! ```
391+
//! assert!(Ok(1) < Err(0));
392+
//! let x: Result<i32, ()> = Ok(0);
393+
//! let y = Ok(1);
394+
//! assert!(x < y);
395+
//! let x: Result<(), i32> = Err(0);
396+
//! let y = Err(1);
397+
//! assert!(x < y);
398+
//! ```
399+
//!
382400
//! ## Iterating over `Result`
383401
//!
384402
//! A [`Result`] can be iterated over. This can be helpful if you need an

0 commit comments

Comments
 (0)