you are viewing a single comment's thread
view the rest of the comments
[–] 13 points 11 months ago (9 children)

I feel like I've seen an insane number of error messages in various apps and websites around the unwrap method.

But this is on a result type, right? I'd figure the point would be that you would match on it and that the unwrap itself, which if my assumptions are correct, is more like get value or throw, should either not exist or take a default value. You shouldn't be able to directly get the value of a monad.

  • source
  • hideshow 9 child comments
  • [–] 27 points 11 months ago (2 children)

    There is a function that does what you are asking for.

    .unarap_or()

    It either unwraps the value or uses a provided default. Personally, i think unwrap() should be renamed unwrap_or_panic() to follow existing conventions and prevent confusion for non-rust programmers.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 12 points 11 months ago

    "unwrap should not exist" is true as long as you don't want to ever use the language. If you actually want to use it, you need it. At least while developing.

    Some values cannot have a default value. And some cases it's preferable to panic even if it has a default value.

    unwrap is not the problem. Cloudflare's usage is.

  • source
  • parent
  • [–] 11 points 11 months ago (3 children)

    I feel like I’ve seen an insane number of error messages in various apps and websites around the unwrap method.

    I suspect this is related to LLM usage somehow. We'll probably see a lot more of this type of problem (sudden flareups of a particular bad code implementation)

  • source
  • parent
  • hideshow 3 child comments
  • [–] 9 points 11 months ago (2 children)

    I actually disagree, because I've both seen it everywhere and I also work mainly in dotnet, and when I've talked to people about option and result types, the first inclination is to have a .Value, but that defeats the purpose. I've done quite a few code reviews where I was essentially saying "you know this will throw, right? Use .Match or .Map instead".

    I think the imperative programming backgrounds encourage this line of thinking, since one of the first questions I've gotten is "how do I get the value out of an Option? I'm 100% sure it's there." And often, surprise, it wasn't.

  • source
  • parent
  • hideshow 2 child comments