4 ms·
So, to be clear, unwrap is safe. It is never going to cause the sorts of memory issues that an uncontrolled crash is. That is, in terms of safety, this is the
by steveklabnik 10y ago
So, to be clear, unwrap is safe. It is never going to cause the sorts of memory issues that an uncontrolled crash is.
That is, in terms of safety, this is the same thing as explicitly handling the error and then terminating the process. Which is the only real way you're going to handle this error anyway, unless you wanted some sort of retry logic. Which you might!
- threatlock 10y agoBzzzt, wrong! You're on the right track though, fuzz harder
- sidlls 10y agoI 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.
- Manishearth 10y agoYeah, 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.
- pygy_ 10y agoRust 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.
- steveklabnik 10y agoIt'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.
- pygy_ 10y agoI 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.
- steveklabnik 10y agoI don't neccesarily disagree. Names are hard :)
- cgag 10y agoA 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.
- Manishearth 10y agoYou shouldn't really be using expect much :)
- cgag 10y agoIt's true I use and see unwrap more.
- Manishearth 10y agoThat'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.
- steveklabnik 10y ago> 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.
- Sharlin 10y agoThe 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.
- steveklabnik 10y agoAbsolutely. 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.
- Sharlin 10y ago> 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.
- hordeallergy 10y agoAgreed. 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.
- steveklabnik 10y agohttps://doc.rust-lang.org/stable/book/error-handling.html 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-handling-and-modularity.html 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.
- logingone 10y agoSometimes a little context can be helpful.
- bryanlarsen 10y agoDoing 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.
- nitrogen 10y agoAn 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.
- notduncansmith 10y agoIn the case of unreliable network peers, it isn't really "exceptional" that they be unreachable. It should just be represented as data.
- XorNot 10y agoThis poses an interesting question: how hard is it in rust to enumerate all possible error sources from some code?
- tedunangst 10y agoThe mention of memory safety is kind of weird. Reading or writing to -1 in C is memory safe too. It's even sometimes done deliberately as a debug technique if you want to insert messages in your IO traces. (Wrt your post that rust is more than just safety, people will get that impression when you introduce memory safety into unrelated threads.)
- deleted 10y ago[deleted]
- steveklabnik 10y agoWhile Rust is more than memory safety, it's really important that people accurately understand what safety actually means in Rust. Because then they oversell it. Common misunderstandings here: * Rust prevents deadlocks * You can't leak memory in Rust * Rust prevents race conditions * Panic is not safe etc. In my mind, bringing up memory safety here isn't a red herring; the parent said this: > However Rust is billing itself as a safe systems programming alternative to C and C++. The way in which we are safer is memory safety, nothing more. And knowing that is crucial. Rust code will have bugs. Rust code will have security vulnerabilities. Rust is not a panacea.
- jdf 10y ago> The way in which we are safer is memory safety, nothing more. I know you want to not overstate rust's claims given recent articles, but I think you're actually underselling a little here. For example, a rust `enum` make it much easier for the compiler to enforce code correctness. It's hard to go back to similar code in C or Go once you've gotten used to `match`.
- steveklabnik 10y agoYes, I guess in my mind, "correctness" and "memory safety" are two different things. I like that Rust can help you write more correct software, but it's not nearly as strong of a guarantee as our memory safety guarantees are.
- 10y ago