You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've just looked through the examples and documentation how to write a good handlers. And all over the place, the it uses .unwrap on Results which could also be Err. This leads to panics in rust - basically what Cloudflare took down last week.
And when I would now remove these unwraps+ the Ok and directly try to return the Result... it would now work because these Response builders are using as Error type hyper::http::Errors and all the examples are build around hyper::Error.
The examples are basically only showing us how to not to write good servers.
I looked at each case of unwrap in those examples, and they are indeed all safe. However, it may non-obvious, so .expect could be the better option to document why. For instance, the http builders can return an error if one of the values passed to uri(), header(), or status() are not valid values for that type. The examples are all calling them with constants that are known to be good, so the unwraps are safe.
But again, for the sake of teaching, it could be a good idea to change those to .expect() explaining why they are fine. Or... (and this is what I do typically in my own code), they could just not used the builders, and thus show that the constants are safe with things like from_static().
Activity
seanmonstar commentedon Nov 26, 2025
Hey, thanks for taking a look!
I looked at each case of unwrap in those examples, and they are indeed all safe. However, it may non-obvious, so
.expectcould be the better option to document why. For instance, thehttpbuilders can return an error if one of the values passed touri(),header(), orstatus()are not valid values for that type. The examples are all calling them with constants that are known to be good, so the unwraps are safe.But again, for the sake of teaching, it could be a good idea to change those to
.expect()explaining why they are fine. Or... (and this is what I do typically in my own code), they could just not used the builders, and thus show that the constants are safe with things likefrom_static().docs(examples): replace unwraps with expects explaining why (#4001)
docs(examples): replace unwraps with expects explaining why (hyperium…
docs(examples): replace unwraps with expects explaining why (hyperium…