Skip to content

Examples full of unsafe usage of .unwrap() #3984

Closed

Description

@CharlemagneLasse

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.

Here are a couple of unsafe unwraps:

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.

Activity

  1. seanmonstar commented on Nov 26, 2025

    @seanmonstar
    Member

    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 .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().

  2. added a commit that references this issue on Jan 12, 2026
    0f0b6ed
  3. added 2 commits that reference this issue on Oct 21, 2026
    02d419c
    fefd4f1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug. Something is wrong. This is bad!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions