News for hackers. Stuff that matters.
Re:My Struggles with Rust by eriknstr on Sunday May 07, 2017 @10:09AM (#14285524)
While it's too late to change the name "expect", could one create an alias for it and call it say, "on_error"?
Re:My Struggles with Rust by eximius on Sunday May 07, 2017 @11:25AM (#14285877)
I'd expect 'on_error' to take a function as a callback, not a string. But yes, a better named function could be added
Re:My Struggles with Rust by skybrian on Sunday May 07, 2017 @12:12PM (#14286114)
Perhaps or_die, similar to Perl?
Re:My Struggles with Rust by sanderjd on Sunday May 07, 2017 @06:00PM (#14287938)
Yep, this is what I always wanted it to be! But maybe or_panic would be better. Naming things is hard.
Re:My Struggles with Rust by allan_s on Tuesday May 09, 2017 @04:07PM (#14303384)
or_fail_with("") ?
Re:My Struggles with Rust by eternalban on Sunday May 07, 2017 @03:31PM (#14287131)
expect[ing_error]("could not open the file")
Re:My Struggles with Rust by rootlocus on Sunday May 07, 2017 @04:16PM (#14287385)
You're not expecting an error. On the contrary, you're expecting a value or throwing an error.
Re:My Struggles with Rust by eternalban on Tuesday May 09, 2017 @08:32AM (#14299425)
You're expecting specific errors.
Re:My Struggles with Rust by edmccard on Sunday May 07, 2017 @08:14PM (#14288430)
In my Rust crates, I use a custom trait[1], with implementations for Option and Result, that provides an expected() method which, if it panics, prefixes the message with "expected: ". So you can write

  foo().pop().expected("foo length >= 1");
and if it fails, the error is something like "panicked at 'expected foo length >= 1'".

[1]https://gist.github.com/edmccard/8898dd397eec0ff3595c28ada52...

Re:My Struggles with Rust by dleslie on Sunday May 07, 2017 @03:59PM (#14287285)
A failed expectation for one user isn't always an error for other users; calling out on_error may confuse people because it indicates an error may occur and must be handled.
Re:My Struggles with Rust by IshKebab on Sunday May 07, 2017 @02:04PM (#14286624)
Or `unwrap_msg()`?