Rust syntax is only getting worse
Rust syntax is only getting worse
Posted Sep 25, 2025 15:40 UTC (Thu) by fishface60 (subscriber, #88700)In reply to: Rust syntax is only getting worse by sturmflut
Parent article: Canceling asynchronous Rust
It's an abstraction designed to give you the option of deciding "actually, any timeout is an error and I want to bail out immediately" using the ? operator.
A possible alternative formulation would be
Ok(permit) => ...
Err(TimeoutError::Timeout(...)) => ...
Err(TimeoutError::Other(...)) => ...
by the timeout function extending the Error value of the Result with a new variant, though since TimeoutError would be defined like this
enum TimeoutError<T, E> {
Timeout(T),
Other(E),
}
and that's logically equivalent to Result there's benefits to reusing it instead of defining an extra type
timeout() could define its own return type as
enum TimeoutResult<T, E> {
Timeout(TimeoutError),
Ok(T),
Err(E),
}
and then you could match on it more simply as
TimeoutResult::Ok(permit) => ...
TimeoutResult::Timeout(...)) => ...
TimeoutResult::Err(...)) => ...
but in both cases you're adding extra cognitive overhead for new types and losing the helper functions that are part of Result,
and you have to narrow the set of operations you call timeout on to things that return a Result.
Ok(Ok(...)) matching isn't an inherent problem in rust, it's a problem that developer time is finite and adding a new type to express the result of a timeout costs more than just wrapping your Results.