Discussion
Loading...

Post

  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
Mara
@Mara@hachyderm.io  ·  activity timestamp 2 days ago

#rustlang hot take: We should rename .unwrap() to .or_panic(). (And .expect() to .or_panic_with().) "Unwrap" is a terrible name for the panicking function, especially since we also have things like .unwrap_or() and .unwrap_or_default() which never panic.

  • Copy link
  • Flag this post
  • Block
d@nny disc@ mc²
@hipsterelectron@circumstances.run replied  ·  activity timestamp yesterday

@Mara i would really like compiler support for determining panic-free code regions with analogy to unsafe

  • Copy link
  • Flag this comment
  • Block
endif
@balasubramanium@social.linux.pizza replied  ·  activity timestamp 2 days ago

@Mara how аbout using а much stable language like c++ instead of rust

  • Copy link
  • Flag this comment
  • Block
Ygor
@ygor@floss.social replied  ·  activity timestamp 2 days ago

@Mara
But wouldn't that make the API inconsistent? Since the other or*() methods don't change the type.

I guess there are quite a few Rust methods with innocent sounding names that can panic. clamp() is one I'm always wary of. There's also the ones that allocate memory, like for Vec and String.

#RustLang

  • Copy link
  • Flag this comment
  • Block
Tom
@tdelmas@mamot.fr replied  ·  activity timestamp 2 days ago

@ygor @Mara
On an `Option<T>`, the output type of `unwrap_or_default` is `T`.

The output type of `unwrap_or_panic` would also be `T`.

Or am I missing something?

  • Copy link
  • Flag this comment
  • Block
Sergey Bugaev
@bugaevc@floss.social replied  ·  activity timestamp 2 days ago

@Mara yes please! 💯

I had a design where the || and && operators were overloadable, and there, res.unwrap() turned into

res || panic!()

and res.unwrap_or(default_value) into

res || default_value

and res.unwrap_or_else(callback) into

res || callback()

But if you're not doing that, renaming the methods would be great.

  • Copy link
  • Flag this comment
  • Block
Agnew Hawk :bongoCat:
@agnew_hawk@mastodon.social replied  ·  activity timestamp 2 days ago

@Mara
Doesn't help that I'm associating unwrapping with unwrapping presents, and occasionally expecting what's inside. Like a perpetual birthday party.

  • Copy link
  • Flag this comment
  • Block
Mara
@Mara@hachyderm.io replied  ·  activity timestamp 2 days ago

(That the name "or_panic" would have been better isn't much of a hot take, but adding "or_panic()" now and deprecating "unwrap()" would likely be controversial.)

  • Copy link
  • Flag this comment
  • Block
Log in

bonfire.cafe

A space for Bonfire maintainers and contributors to communicate

bonfire.cafe: About · Code of conduct · Privacy · Users · Instances
Bonfire social · 1.0.0 no JS en
Automatic federation enabled
  • Explore
  • About
  • Members
  • Code of Conduct
Home
Login