Skip to content

Commit 0c51b4f

Browse files
committed
docs(STYLE.md): prefer try_next() over next()
1 parent dbad714 commit 0c51b4f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

STYLE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,27 @@ All errors should be handled in one of these ways:
7878
- With `.log_err().ok()`.
7979
- Bubbled up with `?`.
8080

81+
When working with [async streams](https://docs.rs/futures/0.3.31/futures/stream/index.html),
82+
prefer [`try_next`](https://docs.rs/futures/0.3.31/futures/stream/trait.TryStreamExt.html#method.try_next) over `next()`, e.g. do
83+
```
84+
while let Some(event) = stream.try_next().await? {
85+
todo!();
86+
}
87+
```
88+
instead of
89+
```
90+
while let Some(event_res) = stream.next().await {
91+
todo!();
92+
}
93+
```
94+
as it allows bubbling up the error early with `?`
95+
with no way to accidentally skip error processing
96+
with early `continue` or `break`.
97+
Some streams reading from a connection
98+
return infinite number of `Some(Err(_))`
99+
items when connection breaks and not processing
100+
errors may result in infinite loop.
101+
81102
`backtrace` feature is enabled for `anyhow` crate
82103
and `debug = 1` option is set in the test profile.
83104
This allows to run `RUST_BACKTRACE=1 cargo test`

0 commit comments

Comments
 (0)