News for hackers. Stuff that matters.
Re:Announcing Tokio 0.1 by sidlls on Wednesday January 11, 2017 @01:10PM (#13376013)
I agree that in terms of safety it's equivalent. However in terms of how an actual program would be written it's...Less than optimal. I almost never simply allow a panic-like termination in a program like this, if it can be trapped, without doing something else, even if it's just to spit out a log/stdout message with a descriptive reason. Then again the plural of anecdote isn't data, and maybe I'm the oddball here.
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 :)
Re:Announcing Tokio 0.1 by steveklabnik on Wednesday January 11, 2017 @01:16PM (#13376074)
> even if it's just to spit out a log/stdout message.

This will print an error out already.

  $ ./target/debug/tokiotest
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { repr: Os { code: 98, message: "Address already in use" } }', ../src/libcore/result.rs:837
Printing out a _better_ error might be helpful, though, I'll agree :) You could use expect for that, which lets you change the message in the ''s easily.

This really shows a fundamental tension though, in documentation. Is this example supposed to be demonstrating error handling? Or just get you going? Does adding more complex error handling distract from the point it's trying to teach? These are sort of open-ended questions.

Re:Announcing Tokio 0.1 by rat87 on Wednesday January 11, 2017 @02:38PM (#13376816)
Wasn't there an attempt to get a variation of main that returned result at one point(that would presumably print error and set exit code one error)? Back before "?"

If rust had that examples would be even shorter using ? Instead of try! or unwrap

Re:Announcing Tokio 0.1 by masklinn on Wednesday January 11, 2017 @02:56PM (#13376953)
FWIW there are third-party libraries which include that feature e.g. error_chain has a `quick_main` macro (working with its `error_chain!`-generated error, but I'm not sure it actually depends on it):

    quick_main!(|| -> Result<()> {
        // insert Result-returning code here
    });
Alternatively it takes a few lines to bootstrap it by hand e.g. ripgrep uses this code to bootstrap:

    fn main() {
        match Args::parse().map(Arc::new).and_then(run) {
            Ok(0) => process::exit(1),
            Ok(_) => process::exit(0),
            Err(err) => {
                eprintln!("{}", err);
                process::exit(1);
            }
        }
    }
https://github.com/BurntSushi/ripgrep/blob/master/src/main.r...
Re:Announcing Tokio 0.1 by steveklabnik on Wednesday January 11, 2017 @02:42PM (#13376844)
Yes, and that discussion is still ongoing. The devil, as always, is in the details.
Re:Announcing Tokio 0.1 by JoshTriplett on Wednesday January 11, 2017 @03:06PM (#13377052)
`-> impl Trait` seems useful in that context, so that main could have a variety of appropriate return value types: `()`, `i32` (for an exit code), or `Result<T, E>` where T implements ReturnValue and E implements Error.

`quick_error` uses a trait like that to determine the exit code of `main()`, allowing it to return either () or i32.

Re:Announcing Tokio 0.1 by Sharlin on Wednesday January 11, 2017 @01:54PM (#13376436)
The output of panic! seems to be useful only to the programmer, not the user. As a user, getting an error message like that would lead me to think that the application is defective, as it arguably is if it cannot provide better user experience in a completely expected error condition like a port being already in use.
Re:Announcing Tokio 0.1 by kibwen on Wednesday January 11, 2017 @02:04PM (#13376540)
The output of panic will only ever be seen by the programmer. Unwrap exists to ease prototyping and to make simple code examples. IME, the first thing you do when you take a Rust application from the prototype phase to the production phase is to grep for unwraps and insert proper error handling.
Re:Announcing Tokio 0.1 by p0nce on Wednesday January 11, 2017 @04:23PM (#13377593)
> The output of panic will only ever be seen by the programmer.

Obviously not if it's used for input errors (network failure). Crashing assertions are made for bugs, not input errors.

Re:Announcing Tokio 0.1 by kibwen on Wednesday January 11, 2017 @06:09PM (#13378322)
Please read the rest of my comment. It's not used for input errors.
Re:Announcing Tokio 0.1 by p0nce on Thursday January 12, 2017 @11:30AM (#13383563)
This is invalid since nothing in the compiler forces you to remove the .unwrap() so it's safe to assume it will not be done before production. The whole "but this is just for prototyping" is a logical fallacy, as you know we have tons of prototypes in production ;)
Re:Announcing Tokio 0.1 by kibwen on Thursday January 12, 2017 @12:00PM (#13383896)
I admit that I'm having a hard time seeing this criticism as anything but overblown. Finding usage of unwrap is trivially easy via textual search. Furthermore, Clippy can be used to automatically forbid usage of unwrap for your entire team ( https://github.com/Manishearth/rust-clippy/wiki#option_unwra... ). Furthermore, even when you hit a panic, it tells you which line of code in which file triggered it so that you can fix it immediately. Furthermore, the Rust community has a strong and long-entrenched proscription against libraries which unwrap rather than handle errors.

We can agree in the cynical interpretation of the laziness of programmers, but the mitigations in this case are so trivial, and the stakes so low, that focusing on unwrap as a point of contention is a poor use of energy.

Re:Announcing Tokio 0.1 by steveklabnik on Wednesday January 11, 2017 @01:56PM (#13376453)
Absolutely. Your users aren't likely to be seeing a server-side process like this fail, though, so in this situation, seems fine.

As I said in the post you're replying to, a nicer error message would be a good thing.

Re:Announcing Tokio 0.1 by Sharlin on Wednesday January 11, 2017 @02:52PM (#13376918)
> Your users aren't likely to be seeing a server-side process like this fail, though, so in this situation, seems fine.

Whoever maintains the server and runs the service is also my user, though, in the general case.

> As I said in the post you're replying to, a nicer error message would be a good thing.

Yeah - I don't mind if unwrap panics with a dev-oriented message as it's basically an assertion, but I guess I expected expect() (no pun intended) to give a more user-friendly error. Maybe the format of the panic! output could be changed to bring the message to the front and the technical details after that.

Re:Announcing Tokio 0.1 by sidlls on Wednesday January 11, 2017 @03:36PM (#13377283)
In the world of server systems programming once an organization gets beyond a certain size it's uncommon to have programmers administering server daemons or other server applications. Those folks are indeed users.
Re:Announcing Tokio 0.1 by kibwen on Wednesday January 11, 2017 @03:48PM (#13377363)
See my sibling comment to the above. By the time your software has matured enough that it's been deployed to non-developers, unwraps have no place in the code. It's not an error-handling strategy, it's just "// TODO: Add error handling" that the compiler understands.
Re:Announcing Tokio 0.1 by contras1970 on Wednesday January 11, 2017 @05:18PM (#13378016)
Example code should be exemplary.
Re:Announcing Tokio 0.1 by steveklabnik on Wednesday January 11, 2017 @05:28PM (#13378077)
As I said elsewhere, exemplary of what? The concept, or robust error handling? Even with the latter, it's unclear what the _right_ error handling is without knowing what you're actually doing.
Re:Announcing Tokio 0.1 by bryanlarsen on Wednesday January 11, 2017 @02:03PM (#13376525)
Doing anything other than a crash is often sub-optimal, in my experience.

Such error handling code is usually untested, which is another way of saying 'buggy'. It almost always swallows useful information, like the backtrace. It sometimes lets program execution continue in a messed up state, causing very strange and hard to debug errors later on.

Certainly Rust makes it a lot harder to mess up error handling code than the languages I'm used to but in general I'm definitely in the 'all exceptions fatal' camp.

Re:Announcing Tokio 0.1 by nitrogen on Wednesday January 11, 2017 @06:21PM (#13378416)
An exception to the exception rule IMO is a program that is managing many internal tasks at once, and the failure of one should not bring down the others. For example, a program that is coordinating many IoT devices should not fail if one of those devices cannot be contacted.
Re:Announcing Tokio 0.1 by notduncansmith on Wednesday January 11, 2017 @09:02PM (#13379290)
In the case of unreliable network peers, it isn't really "exceptional" that they be unreachable. It should just be represented as data.
Re:Announcing Tokio 0.1 by hordeallergy on Wednesday January 11, 2017 @01:46PM (#13376369)
Agreed. Looking for examples of how to handle errors, results, etc and only ever finding unwrap has frustrated me in the past, and as a noob I would have preferred fleshed out rather than shortcuts.
Re:Announcing Tokio 0.1 by steveklabnik on Wednesday January 11, 2017 @01:50PM (#13376400)
https://doc.rust-lang.org/stable/book/error-handling.html goes into fairly extreme level of details about this, if you're still trying to learn.

http://rust-lang.github.io/book/ch12-03-improving-error-hand... shows how to refactor a project to improve its error handling as well, for something a bit less abstract.

Re:Announcing Tokio 0.1 by logingone on Wednesday January 11, 2017 @04:38PM (#13377711)
Sometimes a little context can be helpful.
Re:Announcing Tokio 0.1 by XorNot on Wednesday January 11, 2017 @10:17PM (#13379591)
This poses an interesting question: how hard is it in rust to enumerate all possible error sources from some code?