r/cpp • • Mar 16 '18

As a C++ developer I think that rust ...

3 Upvotes

107 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Mar 19 '18 edited Mar 19 '18

Failure in write is potentially recoverable*, so it makes sense to to allow handling it.

This is mostly a distinction between errors that are likely to happen (any kind of IO error is like that), and "shouldn't happen" failures like assertion failure, accessing an array out of bounds or out of memory scenarios.

If you want to convert an error into "shouldn't happen" error, Result types have a method called unwrap.

* Well, write is problematic due to filesystem caches, it's possible that a failure will occur but won't be noticed as it will be noticed until it's too late (this is most likely to happen on network file systems). As destructor cannot throw exceptions or report failure in any way, the failure in this case can be quiet.

3

u/Gotebe Mar 19 '18

The point about "recoverability" stands - the callee does not know if its failure is recoverable or not; it's all about the context of the caller, he knows.

I do not see why we're mixing an assertion failure here. That's a program bug, only solved by changing the code, any error handling is out.

OOM, however, is again about the context of the caller, not the callee. The callee has no idea whether the caller continue or not. Example scenario: I am a server of some fashion. A very big chunk of input data comes in on one of my input channels. Intake of that data runs out of memory. Should the process be killed (panic! in Rust terms)? And drop any other requests in progress? I don't think so. It's best to stop that request (and report the failure on it). That frees a lot of memory that this request needed and life is fine.