qaz@lemmy.world to Programmer Humor@programming.devEnglish · edit-21 day agoSlapping on a .expect is also error handling!lemmy.worldimagemessage-square26fedilinkarrow-up1161arrow-down15file-text
arrow-up1156arrow-down1imageSlapping on a .expect is also error handling!lemmy.worldqaz@lemmy.world to Programmer Humor@programming.devEnglish · edit-21 day agomessage-square26fedilinkfile-text
minus-squareKorne127@lemmy.worldlinkfedilinkarrow-up15·2 days agoI mean using unwrap is not bad practice if the value is guaranteed to not be none, which can happen frequently in some applications.
minus-squaremobotsar@sh.itjust.workslinkfedilinkEnglisharrow-up4·edit-21 day agoIf it’s guaranteed to not be None, why is it an Option?
minus-squareemilgardis@lemmy.mllinkfedilinkEnglisharrow-up3·1 day agoHere’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.
minus-squaremarcos@lemmy.worldlinkfedilinkarrow-up2·edit-21 day agoOh, 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.)
minus-squareqaz@lemmy.worldOPlinkfedilinkEnglisharrow-up10arrow-down1·edit-21 day agoA good example would be regex. After validating it when writing the program it should always compile, although this could also be solved with a proc macro that validates the regex at compile time.
I mean using unwrap is not bad practice if the value is guaranteed to not be none, which can happen frequently in some applications.
If it’s guaranteed to not be
None
, why is it anOption
?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.
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.)
A good example would be regex. After validating it when writing the program it should always compile, although this could also be solved with a proc macro that validates the regex at compile time.