From 461b27df1653c69417e8e98cd7345ba76ef4f870 Mon Sep 17 00:00:00 2001 From: parrrate Date: Thu, 9 Apr 2026 12:56:16 +0000 Subject: [PATCH] err err --- src/SUMMARY.md | 1 + src/exercises/err_err.md | 56 ++++++++++++++++++++++++++++++++++++++++ src/topics.md | 15 ++++++----- 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 src/exercises/err_err.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 5b2a7c2..ced0b3d 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -20,5 +20,6 @@ - [Composition](./exercises/composition.md) - [Covariance]() - [Option, Vec and BoxFuture<'a>]() + - [Err Err](./exercises/err_err.md) [Topics (Spoilers)](./topics.md) diff --git a/src/exercises/err_err.md b/src/exercises/err_err.md new file mode 100644 index 0000000..9808d46 --- /dev/null +++ b/src/exercises/err_err.md @@ -0,0 +1,56 @@ +# `Err(Err)` + +1. Implement the function for both groups of tests. +2. Provide usecases for each group. +3. How does this relate to the rest of this chapter (generic functional programming)? + +```rust +// `try_join` or `zip` may be a better name +fn join( + r0: Result, E1>, + r1: Result, E1>, +) -> Result, E1> { +# /* + todo!() +# */ +# Ok((|r0, r1| Ok((r0?, r1?)))(r0?, r1?)) +} + +# fn join_alt( +# r0: Result, E1>, +# r1: Result, E1>, +# ) -> Result, E1> { +# match (r0, r1) { +# (Ok(Err(e)), _) | (_, Ok(Err(e))) => Ok(Err(e)), +# (Err(e), _) | (_, Err(e)) => Err(e), +# (Ok(Ok(a)), Ok(Ok(b))) => Ok(Ok((a, b))), +# } +# } +# let mut short = false; +# for join in [join, join_alt] { +# let tested = | +# r0: Result, &'static str>, +# r1: Result, &'static str> +# | join(r0, r1); +# let join = tested; +// applies to both +assert_eq!(join(Ok(Ok(0)), Ok(Ok("1"))), Ok(Ok((0, "1")))); +assert_eq!(join(Ok(Err(2)), Ok(Ok("3"))), Ok(Err(2))); +assert_eq!(join(Ok(Ok(3)), Ok(Err(4))), Ok(Err(4))); +assert_eq!(join(Ok(Err(5)), Ok(Err(5))), Ok(Err(5))); +assert_eq!(join(Err("6"), Ok(Ok("7"))), Err("6")); +assert_eq!(join(Ok(Ok(8)), Err("9")), Err("9")); +assert_eq!(join(Err("10"), Err("10")), Err("10")); +// the following two groups are mutually exclusive +# if short { + // group 1 +assert_eq!(join(Ok(Err(11)), Err("12")), Ok(Err(11))); +assert_eq!(join(Err("13"), Ok(Err(14))), Ok(Err(14))); +# } else { + // group 2 +assert_eq!(join(Ok(Err(11)), Err("12")), Err("12")); +assert_eq!(join(Err("13"), Ok(Err(14))), Err("13")); +# short = true; +# } +# } +``` diff --git a/src/topics.md b/src/topics.md index 6139263..b118e15 100644 --- a/src/topics.md +++ b/src/topics.md @@ -2,12 +2,13 @@ Rows are ordered based on estimated difficulty. -| lifetimes\[-adjacent\] | `trait`s | `struct`s | -|------------------------|--------------------|---------------| -| [explicit lifetimes] | [`Fn`?] | [`Duration`] | -| [from reference] | [`async` `Fn`] | [All Errors?] | -| [extracting lifetimes] | [exclusive traits] | [`RcChars`] | -| [composition] | [merging traits] | [`AnyStr`] | +| lifetimes\[-adjacent\] | `trait`s | `struct`s | +|------------------------|--------------------|---------------------| +| [explicit lifetimes] | [`Fn`?] | [`Duration`] | +| [from reference] | [`async` `Fn`] | [All Errors?] | +| [extracting lifetimes] | [composition] | [`RcChars`] | +| [`'static` return] | [exclusive traits] | [`AnyStr`] | +| | [merging traits] | [`Result` ordering] | p.s. most of the exercises are actually about `trait`s, this categorisation isn't strict. @@ -23,3 +24,5 @@ p.s. most of the exercises are actually about `trait`s, this categorisation isn' [All Errors?]: ./exercises/all_errors.md [from reference]: ./exercises/fromref.md [`async` `Fn`]: ./exercises/async_fn.md +[`'static` return]: ./exercises/get_functions.md +[`Result` ordering]: ./exercises/err_err.md