|
259 | 259 | //! The [`is_ok`] and [`is_err`] methods return [`true`] if the [`Result`] |
260 | 260 | //! is [`Ok`] or [`Err`], respectively. |
261 | 261 | //! |
| 262 | +//! The [`is_ok_and`] and [`is_err_and`] methods apply the provided function |
| 263 | +//! to the contents of the [`Result`] to produce a boolean value. If the [`Result`] does not have the expected variant |
| 264 | +//! then [`false`] is returned instead without executing the function. |
| 265 | +//! |
262 | 266 | //! [`is_err`]: Result::is_err |
263 | 267 | //! [`is_ok`]: Result::is_ok |
| 268 | +//! [`is_ok_and`]: Result::is_ok_and |
| 269 | +//! [`is_err_and`]: Result::is_err_and |
264 | 270 | //! |
265 | 271 | //! ## Adapters for working with references |
266 | 272 | //! |
|
287 | 293 | //! (which must implement the [`Default`] trait) |
288 | 294 | //! * [`unwrap_or_else`] returns the result of evaluating the provided |
289 | 295 | //! function |
| 296 | +//! * [`unwrap_unchecked`] produces *[undefined behavior]* |
290 | 297 | //! |
291 | 298 | //! The panicking methods [`expect`] and [`unwrap`] require `E` to |
292 | 299 | //! implement the [`Debug`] trait. |
|
297 | 304 | //! [`unwrap_or`]: Result::unwrap_or |
298 | 305 | //! [`unwrap_or_default`]: Result::unwrap_or_default |
299 | 306 | //! [`unwrap_or_else`]: Result::unwrap_or_else |
| 307 | +//! [`unwrap_unchecked`]: Result::unwrap_unchecked |
| 308 | +//! [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html |
300 | 309 | //! |
301 | 310 | //! These methods extract the contained value in a [`Result<T, E>`] when it |
302 | 311 | //! is the [`Err`] variant. They require `T` to implement the [`Debug`] |
303 | 312 | //! trait. If the [`Result`] is [`Ok`]: |
304 | 313 | //! |
305 | 314 | //! * [`expect_err`] panics with a provided custom message |
306 | 315 | //! * [`unwrap_err`] panics with a generic message |
| 316 | +//! * [`unwrap_err_unchecked`] produces *[undefined behavior]* |
307 | 317 | //! |
308 | 318 | //! [`Debug`]: crate::fmt::Debug |
309 | 319 | //! [`expect_err`]: Result::expect_err |
310 | 320 | //! [`unwrap_err`]: Result::unwrap_err |
| 321 | +//! [`unwrap_err_unchecked`]: Result::unwrap_err_unchecked |
| 322 | +//! [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html |
311 | 323 | //! |
312 | 324 | //! ## Transforming contained values |
313 | 325 | //! |
|
330 | 342 | //! [`Some(v)`]: Option::Some |
331 | 343 | //! [`transpose`]: Result::transpose |
332 | 344 | //! |
333 | | -//! This method transforms the contained value of the [`Ok`] variant: |
| 345 | +//! These methods transform the contained value of the [`Ok`] variant: |
334 | 346 | //! |
335 | 347 | //! * [`map`] transforms [`Result<T, E>`] into [`Result<U, E>`] by applying |
336 | 348 | //! the provided function to the contained value of [`Ok`] and leaving |
337 | 349 | //! [`Err`] values unchanged |
| 350 | +//! * [`inspect`] takes ownership of the [`Result`], applies the |
| 351 | +//! provided function to the contained value by reference, |
| 352 | +//! and then returns the [`Result`] |
338 | 353 | //! |
339 | 354 | //! [`map`]: Result::map |
| 355 | +//! [`inspect`]: Result::inspect |
340 | 356 | //! |
341 | | -//! This method transforms the contained value of the [`Err`] variant: |
| 357 | +//! These methods transform the contained value of the [`Err`] variant: |
342 | 358 | //! |
343 | 359 | //! * [`map_err`] transforms [`Result<T, E>`] into [`Result<T, F>`] by |
344 | 360 | //! applying the provided function to the contained value of [`Err`] and |
345 | 361 | //! leaving [`Ok`] values unchanged |
| 362 | +//! * [`inspect_err`] takes ownership of the [`Result`], applies the |
| 363 | +//! provided function to the contained value of [`Err`] by reference, |
| 364 | +//! and then returns the [`Result`] |
346 | 365 | //! |
347 | 366 | //! [`map_err`]: Result::map_err |
| 367 | +//! [`inspect_err`]: Result::inspect_err |
348 | 368 | //! |
349 | 369 | //! These methods transform a [`Result<T, E>`] into a value of a possibly |
350 | 370 | //! different type `U`: |
@@ -578,6 +598,10 @@ impl<T, E> Result<T, E> { |
578 | 598 | /// |
579 | 599 | /// let x: Result<u32, &str> = Err("hey"); |
580 | 600 | /// assert_eq!(x.is_ok_and(|x| x > 1), false); |
| 601 | + /// |
| 602 | + /// let x: Result<String, &str> = Ok("ownership".to_string()); |
| 603 | + /// assert_eq!(x.as_ref().is_ok_and(|x| x.len() > 1), true); |
| 604 | + /// println!("still alive {:?}", x); |
581 | 605 | /// ``` |
582 | 606 | #[must_use] |
583 | 607 | #[inline] |
@@ -623,6 +647,10 @@ impl<T, E> Result<T, E> { |
623 | 647 | /// |
624 | 648 | /// let x: Result<u32, Error> = Ok(123); |
625 | 649 | /// assert_eq!(x.is_err_and(|x| x.kind() == ErrorKind::NotFound), false); |
| 650 | + /// |
| 651 | + /// let x: Result<u32, String> = Err("ownership".to_string()); |
| 652 | + /// assert_eq!(x.as_ref().is_err_and(|x| x.len() > 1), true); |
| 653 | + /// println!("still alive {:?}", x); |
626 | 654 | /// ``` |
627 | 655 | #[must_use] |
628 | 656 | #[inline] |
|
0 commit comments