News for hackers. Stuff that matters.
Re:Announcing Tokio 0.1 by Manishearth on Wednesday January 11, 2017 @01:12PM (#13376036)
Yeah, unwrap()s in examples should be written to use expect() (which spits a message and then panics).

But for most example programs there's no easy way to handle errors further than just using expect() unless you want there to be more error handling code than actual useful example code.

Re:Announcing Tokio 0.1 by pygy_ on Wednesday January 11, 2017 @03:36PM (#13377285)
Rust n00b here (I read a lot about it but haven't written a line yet), but wouldn't `.orPanic('error message')` be a better name than `.expect('error message')`? The latter seems backwards.
Re:Announcing Tokio 0.1 by steveklabnik on Wednesday January 11, 2017 @03:47PM (#13377360)
It'd be or_panic, following conventions.

The idea is that I expect this to be Ok/Some, and if it's not, please use this error message.

Re:Announcing Tokio 0.1 by pygy_ on Wednesday January 11, 2017 @05:05PM (#13377920)
I understand the intent, but usually the programmatic object is the subject of the sentence, the method is the verb and the parameter/argument is the grammatical object.

Here, the programmatic object is the grammatical object. That's what I meant by 'backwards'.

could_fail().or_panic_with('message') may be even better.

Re:Announcing Tokio 0.1 by cgag on Wednesday January 11, 2017 @05:36PM (#13378119)
A lot of people don't seem to agree with me but I think even expect is too long of a name and terseness for commonly used names seems more important to me than reading like natural language.
Re:Announcing Tokio 0.1 by ben0x539 on Thursday January 12, 2017 @07:50AM (#13381785)
I don't think terseness helps much here, isn't the actual error message gonna dwarf the name of the method it's passed to in almost all scenarios?
Re:Announcing Tokio 0.1 by Manishearth on Wednesday January 11, 2017 @06:05PM (#13378297)
You shouldn't really be using expect much :)
Re:Announcing Tokio 0.1 by cgag on Wednesday January 11, 2017 @06:45PM (#13378557)
It's true I use and see unwrap more.
Re:Announcing Tokio 0.1 by Manishearth on Wednesday January 11, 2017 @06:54PM (#13378619)
That's not good either :|

unwrap should be confined to test and temporary code. It somewhat makes sense in example code, but expect is better for that.

There are sometimes cases when you very easily know that the unwrap will never fail and if it does something has gone very horribly wrong, in which case you might use it.

Libraries should keep usage of unwrap/expect to a minimum. Applications can be more liberal with it, but they should try to use expect or better error handling.

Not everyone perfectly follows this, sadly. But most do.

Re:Announcing Tokio 0.1 by pygy_ on Thursday January 12, 2017 @03:00PM (#13385576)
It's at least partly a matter of affordance. You could soft-deprecate .unwrap() and .expect() (emit warnings at compile time, it will be annoying but not fatal), and provide a new version of expect with a name that's more cumbersome to type (or_panic_with(...) isn't bad in that regard, but you could do even worse... or_panic_with_error_message(...) :-).
Re:Announcing Tokio 0.1 by steveklabnik on Wednesday January 11, 2017 @05:27PM (#13378070)
I don't neccesarily disagree. Names are hard :)