|
162 | 162 | //! The [`is_some`] and [`is_none`] methods return [`true`] if the [`Option`]
|
163 | 163 | //! is [`Some`] or [`None`], respectively.
|
164 | 164 | //!
|
| 165 | +//! The [`is_some_and`] and [`is_none_or`] methods apply the provided function |
| 166 | +//! to the contents of the [`Option`] to produce a boolean value. |
| 167 | +//! If this is [`None`] then a default result is returned instead without executing the function. |
| 168 | +//! |
165 | 169 | //! [`is_none`]: Option::is_none
|
166 | 170 | //! [`is_some`]: Option::is_some
|
| 171 | +//! [`is_some_and`]: Option::is_some_and |
| 172 | +//! [`is_none_or`]: Option::is_none_or |
167 | 173 | //!
|
168 | 174 | //! ## Adapters for working with references
|
169 | 175 | //!
|
|
177 | 183 | //! <code>[Option]<[Pin]<[&]T>></code>
|
178 | 184 | //! * [`as_pin_mut`] converts from <code>[Pin]<[&mut] [Option]\<T>></code> to
|
179 | 185 | //! <code>[Option]<[Pin]<[&mut] T>></code>
|
| 186 | +//! * [`as_slice`] returns a one-element slice of the contained value, if any. |
| 187 | +//! If this is [`None`], an empty slice is returned. |
| 188 | +//! * [`as_mut_slice`] returns a mutable one-element slice of the contained value, if any. |
| 189 | +//! If this is [`None`], an empty slice is returned. |
180 | 190 | //!
|
181 | 191 | //! [&]: reference "shared reference"
|
182 | 192 | //! [&mut]: reference "mutable reference"
|
|
187 | 197 | //! [`as_pin_mut`]: Option::as_pin_mut
|
188 | 198 | //! [`as_pin_ref`]: Option::as_pin_ref
|
189 | 199 | //! [`as_ref`]: Option::as_ref
|
| 200 | +//! [`as_slice`]: Option::as_slice |
| 201 | +//! [`as_mut_slice`]: Option::as_mut_slice |
190 | 202 | //!
|
191 | 203 | //! ## Extracting the contained value
|
192 | 204 | //!
|
|
200 | 212 | //! (which must implement the [`Default`] trait)
|
201 | 213 | //! * [`unwrap_or_else`] returns the result of evaluating the provided
|
202 | 214 | //! function
|
| 215 | +//! * [`unwrap_unchecked`] produces *[undefined behavior]* |
203 | 216 | //!
|
204 | 217 | //! [`expect`]: Option::expect
|
205 | 218 | //! [`unwrap`]: Option::unwrap
|
206 | 219 | //! [`unwrap_or`]: Option::unwrap_or
|
207 | 220 | //! [`unwrap_or_default`]: Option::unwrap_or_default
|
208 | 221 | //! [`unwrap_or_else`]: Option::unwrap_or_else
|
| 222 | +//! [`unwrap_unchecked`]: Option::unwrap_unchecked |
| 223 | +//! [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html |
209 | 224 | //!
|
210 | 225 | //! ## Transforming contained values
|
211 | 226 | //!
|
|
230 | 245 | //! * [`filter`] calls the provided predicate function on the contained
|
231 | 246 | //! value `t` if the [`Option`] is [`Some(t)`], and returns [`Some(t)`]
|
232 | 247 | //! if the function returns `true`; otherwise, returns [`None`]
|
233 |
| -//! * [`flatten`] removes one level of nesting from an |
234 |
| -//! [`Option<Option<T>>`] |
| 248 | +//! * [`flatten`] removes one level of nesting from an [`Option<Option<T>>`] |
| 249 | +//! * [`inspect`] method takes ownership of the [`Option`] and applies |
| 250 | +//! the provided function to the contained value by reference if [`Some`] |
235 | 251 | //! * [`map`] transforms [`Option<T>`] to [`Option<U>`] by applying the
|
236 | 252 | //! provided function to the contained value of [`Some`] and leaving
|
237 | 253 | //! [`None`] values unchanged
|
238 | 254 | //!
|
239 | 255 | //! [`Some(t)`]: Some
|
240 | 256 | //! [`filter`]: Option::filter
|
241 | 257 | //! [`flatten`]: Option::flatten
|
| 258 | +//! [`inspect`]: Option::inspect |
242 | 259 | //! [`map`]: Option::map
|
243 | 260 | //!
|
244 | 261 | //! These methods transform [`Option<T>`] to a value of a possibly
|
@@ -621,6 +638,10 @@ impl<T> Option<T> {
|
621 | 638 | ///
|
622 | 639 | /// let x: Option<u32> = None;
|
623 | 640 | /// assert_eq!(x.is_some_and(|x| x > 1), false);
|
| 641 | + /// |
| 642 | + /// let x: Option<String> = Some("ownership".to_string()); |
| 643 | + /// assert_eq!(x.as_ref().is_some_and(|x| x.len() > 1), true); |
| 644 | + /// println!("still alive {:?}", x); |
624 | 645 | /// ```
|
625 | 646 | #[must_use]
|
626 | 647 | #[inline]
|
@@ -665,6 +686,10 @@ impl<T> Option<T> {
|
665 | 686 | ///
|
666 | 687 | /// let x: Option<u32> = None;
|
667 | 688 | /// assert_eq!(x.is_none_or(|x| x > 1), true);
|
| 689 | + /// |
| 690 | + /// let x: Option<String> = Some("ownership".to_string()); |
| 691 | + /// assert_eq!(x.as_ref().is_none_or(|x| x.len() > 1), true); |
| 692 | + /// println!("still alive {:?}", x); |
668 | 693 | /// ```
|
669 | 694 | #[must_use]
|
670 | 695 | #[inline]
|
|
0 commit comments