Re:My Struggles with Rust
by AlphaSite on Sunday May 07, 2017 @10:43AM
(#14285688)
The java optional api uses orElseThrow which I think is quite clear.
[ Parent
| on HN ]
Re:My Struggles with Rust
by bluejekyll on Sunday May 07, 2017 @10:53AM
(#14285734)
Agreed, I've never liked 'expect' either; 'or_else_panic(msg)' would be much clearer
Edit: 'or_panic(msg)' would be shorter and also good.
I think that was one of the proposed variations, but we ended up picking the shorter .expect to cut down on repetition. We expected (ha) that this function would be used in these one-offs, so we wanted something more efficient.
Re:My Struggles with Rust
by bluejekyll on Sunday May 07, 2017 @11:20AM
(#14285859)
As an old Java hack, my allowance for repetition is high. Especially when using and IDE that basically writes the code for me ;)
otherwise(msg)?
Re:My Struggles with Rust
by MaxGabriel on Sunday May 07, 2017 @11:44AM
(#14285987)
I would expect such a function to provide a default value
I actually initially had it as sort of a callback:
.otherwise(panic(msg))
(although I assume a rust panic! isn't really a function call).
But isn't the way to get the default, simply to use unwrap()?
In a simple script, failing to open a configuration file for reading is likely a show stopper, and you probably want to log/print an error (no such file, wrong permission, etc).
But in, say, a paint program, you'd normally not want to panic and crash if the image file a user selected to open in a file dialog is invalid or went away between the click-to-select and the click-to-open. In such a program you'd want to handle most file errors much more defensively.
Re:My Struggles with Rust
by AsyncAwait on Sunday May 07, 2017 @10:55AM
(#14285738)
It is, but also quite long, which I do not think suits Rust naming conventions.
Re:My Struggles with Rust
by bluejekyll on Sunday May 07, 2017 @11:27AM
(#14285889)
Not really though: 'unwrap_or_else' is of similar length.