diff --git a/src/SUMMARY.md b/src/SUMMARY.md index d1cd34f..cd873c2 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -5,6 +5,7 @@ - [Chapter 1](./chapter_1.md) - [test0.rs (bind)](./exercises/bind.md) - [test1.rs (refbind)](./exercises/refbind.md) + - [Duration](./exercises/duration.md) - [A/A1/A2](./exercises/multiple_blanket.md) - [BoolStream](./exercises/bool_stream.md) - [RcChars](./exercises/rcchars.md) diff --git a/src/exercises/bind.md b/src/exercises/bind.md index e7a5342..b33779b 100644 --- a/src/exercises/bind.md +++ b/src/exercises/bind.md @@ -1,4 +1,4 @@ -Make this compile and pass tests. +Make this compile and pass tests: ```rust # mod __ { fn bind<T, F: Fn(T) -> Option<T>>(f: F, fa: Option<T>) -> Option<T> { diff --git a/src/exercises/duration.md b/src/exercises/duration.md new file mode 100644 index 0000000..2687269 --- /dev/null +++ b/src/exercises/duration.md @@ -0,0 +1,25 @@ +# `const` `Add`? + +Define `FIVE_SECONDS` as `const`: +```rust +# use std::time::Duration; +# const fn unwrap_time(d: Option<Duration>) -> Duration { match d { Some(d) => d, None => panic!() } } +const TWO_SECONDS: Duration = Duration::from_secs(2); +# /* +const FIVE_SECONDS: Duration = TWO_SECONDS + Duration::from_secs(3); +# */ +# const FIVE_SECONDS: Duration = unwrap_time(TWO_SECONDS.checked_add(Duration::from_secs(3))); +assert_eq!(FIVE_SECONDS, Duration::from_secs(5)); +``` + +Try solving it in the playground: +```rust,editable,compile_fail +use std::time::Duration; + +const TWO_SECONDS: Duration = Duration::from_secs(2); +const FIVE_SECONDS: Duration = TWO_SECONDS + Duration::from_secs(3); + +fn main() { + assert_eq!(FIVE_SECONDS, Duration::from_secs(5)); +} +``` diff --git a/src/exercises/refbind.md b/src/exercises/refbind.md index 4f55b6a..5a38dc1 100644 --- a/src/exercises/refbind.md +++ b/src/exercises/refbind.md @@ -1,4 +1,4 @@ -Make this compile and pass tests. +Make this compile and pass tests: ```rust # mod __ { fn refbind<T, F: Fn(&T) -> Option<&T>>(f: F, fa: Option<&T>) -> Option<&T> { diff --git a/src/topics.md b/src/topics.md index 401e95f..be56b31 100644 --- a/src/topics.md +++ b/src/topics.md @@ -2,12 +2,12 @@ Rows are ordered based on estimated difficulty. -| lifetimes\[-adjacent\] | `trait`s | `struct`s | -|------------------------|--------------------|-------------| -| [explicit lifetimes] | [`Fn`?] | | -| [extracting lifetimes] | [exclusive traits] | [`RcChars`] | -| [composition] | [`AnyStr`] | | -| | [merging traits] | | +| lifetimes\[-adjacent\] | `trait`s | `struct`s | +|------------------------|--------------------|--------------| +| [explicit lifetimes] | [`Fn`?] | [`Duration`] | +| [extracting lifetimes] | [exclusive traits] | [`RcChars`] | +| [composition] | [`AnyStr`] | | +| | [merging traits] | | [explicit lifetimes]: ./exercises/refbind.md @@ -18,3 +18,4 @@ Rows are ordered based on estimated difficulty. [merging traits]: ./exercises/modes.md [`AnyStr`]: ./exercises/anystr.md [composition]: ./exercises/composition.md +[`Duration`]: ./exercises/duration.md