duration
This commit is contained in:
parent
833647455b
commit
18b78fdc94
@ -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)
|
||||
|
@ -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> {
|
||||
|
25
src/exercises/duration.md
Normal file
25
src/exercises/duration.md
Normal file
@ -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));
|
||||
}
|
||||
```
|
@ -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> {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user