1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-09 09:11:50 +02:00

Clarify the suggested steps in the pin page (#2130)

The speaker notes suggest an evolution of the code to support a periodic
timer, but the last step was under-specified.

(As mentioned by @fw-immunant and referenced in #1536)
This commit is contained in:
Dustin J. Mitchell 2024-06-07 16:39:14 -04:00 committed by GitHub
parent b69b68f5e2
commit 412eac6689
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -82,7 +82,7 @@ async fn main() {
- Instead, add a `timeout_fut` containing that future outside of the `loop`:
```rust,compile_fail
let mut timeout_fut = sleep(Duration::from_millis(100));
let timeout_fut = sleep(Duration::from_millis(100));
loop {
select! {
..,
@ -106,7 +106,18 @@ async fn main() {
- This compiles, but once the timeout expires it is `Poll::Ready` on every
iteration (a fused future would help with this). Update to reset
`timeout_fut` every time it expires.
`timeout_fut` every time it expires:
```rust,compile_fail
let mut timeout_fut = Box::pin(sleep(Duration::from_millis(100)));
loop {
select! {
_ = &mut timeout_fut => {
println!(..);
timeout_fut = Box::pin(sleep(Duration::from_millis(100)));
},
}
}
```
- Box allocates on the heap. In some cases, `std::pin::pin!` (only recently
stabilized, with older code often using `tokio::pin!`) is also an option, but