▲ 155 ▼ Slapping on a .expect is also error handling! (lemmy.world) submitted 1 year ago* (last edited 1 year ago) by qaz@lemmy.world to c/programmer_humor@programming.dev 29 comments fedilink hide all child comments Don't say anyway, say anyhow
[–] mobotsar@sh.itjust.works 4 points 1 year ago* (3 children) If it's guaranteed to not be None, why is it an Option? permalink fedilink source parent hideshow 3 child comments replies: [–] emilgardis@lemmy.ml 2 points 1 year ago 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. permalink fedilink source parent [–] marcos@lemmy.world 2 points 1 year ago* 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.) permalink fedilink source parent [–] Korne127@lemmy.world 1 point 1 year ago 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. permalink fedilink source parent
[–] emilgardis@lemmy.ml 2 points 1 year ago 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. permalink fedilink source parent
[–] marcos@lemmy.world 2 points 1 year ago* 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.) permalink fedilink source parent
[–] Korne127@lemmy.world 1 point 1 year ago 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. permalink fedilink source parent