Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

agnos.is Forums

  1. Home
  2. Programmer Humor
  3. Slapping on a `.expect` is also error handling!

Slapping on a `.expect` is also error handling!

Scheduled Pinned Locked Moved Programmer Humor
programmerhumor
25 Posts 18 Posters 2 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mobotsar@sh.itjust.worksM [email protected]

    If it's guaranteed to not be None, why is it an Option?

    E This user is from outside of this forum
    E This user is from outside of this forum
    [email protected]
    wrote on last edited by
    #21

    Here's a bad example but hopefully captures the why. https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=43d055381e7bb52569c339d4526818f4

    We have a condition we know must be satisfied (the option will always be Some), but cant prove in code.

    1 Reply Last reply
    1
    • mobotsar@sh.itjust.worksM [email protected]

      If it's guaranteed to not be None, why is it an Option?

      M This user is from outside of this forum
      M This user is from outside of this forum
      [email protected]
      wrote on last edited by [email protected]
      #22

      Oh, it can happen when you do calculations with compile-time constants...

      But the GP's claim that it's a "frequent" thing is suspect.

      (Crashing is also useful when you are writing and-user applications, but you'll probably want .expect like in the meme.)

      1 Reply Last reply
      0
      • A [email protected]

        “An abrupt exit”, more commonly known as a “crash”.

        If you’re going to argue that an exit through panic!() is not a crash, I will argue that your definition of a crash is just an abrupt exit initiated by the OS. In other words, there’s no meaningful distinction as the result is the same.

        dgriffith@aussie.zoneD This user is from outside of this forum
        dgriffith@aussie.zoneD This user is from outside of this forum
        [email protected]
        wrote on last edited by [email protected]
        #23

        I was talking more about unwrap causing a panic rather than calling the actual panic macro directly. Rust forces the programmer to deal with bad or ambiguous results, and what that is exactly is entirely decided by the function you are calling. If a function decides to return None when (system timer mod 2 == 0), then you'd better check for None in your code. Edit: otherwise your code is ending now with a panic, as opposed to your code merrily trotting down the path of undefined behaviour and a segfault or similar later on.

        Once you get to a point where we are doing the actual panic, well, that is starting to just be semantics.

        1 Reply Last reply
        1
        • S [email protected]

          Unwrap is good for prototyping and trying out stuff fast, but it generally shouldn't make it past a code review onto main, unless you're very sure

          D This user is from outside of this forum
          D This user is from outside of this forum
          [email protected]
          wrote last edited by
          #24

          Exactly.

          Personally, I call it "python mode" since you're staying on the "happy path" and let the program just crash out if those expectations aren't met.

          1 Reply Last reply
          1
          • mobotsar@sh.itjust.worksM [email protected]

            If it's guaranteed to not be None, why is it an Option?

            korne127@lemmy.worldK This user is from outside of this forum
            korne127@lemmy.worldK This user is from outside of this forum
            [email protected]
            wrote last edited by
            #25

            A very typical use-case would be getting something from a HashMap (or a Vector) and calling unwrap because you know it must exist (as you got a reference to the index or object that must be valid in the HashMap or Vector).
            Or if you call a function that returns Option<…> depending on the current state and you know that it must return Some(…) in the current situation.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups