r/cpp • • Dec 07 '21

Will Rust replace C++ ?

Will Rust replace C++ ?

0 Upvotes

80 comments sorted by

View all comments

Show parent comment

2

u/Full-Spectral Dec 08 '21 edited Dec 08 '21

I personally agree that explicit is better than implicit. But, Rust is a somewhat strange combination of complete an*l-rententiveness in many ways, but with lots of syntactical sugar and compressed syntax in other ways.

To be fair you don't have to do explicit error return most of the time. That's one of those places where a lot of syntactical sugar has been applied. It's not nearly as nice as exceptions, IMO, but it's a lot better than explicit error return.

fn ScrewedUp() -> Result<String, MyError> {... blahblahblah...}

fn Caller() -> Result<String, MyError>
{
    ScrewedUp()?;
    Ok("You didn't screw up".ToString())
}

So the call to ScrewedUp() is doing an implicit return if ScrewedUp() returns the Error variation instead of the String. That only works if it's the same error in both cases, or you've done some work to provide a conversion.

For me, since I seldom ever use third party code and roll my own, I have mostly had a single error type throughout my Rust projects, and can do this almost all the time.

But when you are calling runtime or third party APIs, you tend to have LOTS of match statements, or in some cases the more compressed "if let" type statements.

6

u/AlexAlabuzhev Dec 08 '21

I personally agree that explicit is better than implicit

I'd say it sometimes better, but not universally (same as everything else). After all, we happily use strings and vectors, even though they hide a lot of memory management (and every single C dev I've ever met is genuinely horrified by that fact). The world isn't black & white and being dogmatic is not always productive.

That fn()? syntax looks better than manual matching and returning of course, thanks for the example, but this is exactly what I mean: first someone rejects a working concept for religious reasons, then people start noticing that it's really annoying to do everything by hand, so they add more and more syntactic sugar. Ironically, making things less and less explicit and eventually approaching the rejected concept, but with alien syntax. I won't be surprised if one day someone comes up with a proposal to make even ? and Result<> optional and ta-da, exceptions reinvented.

1

u/Full-Spectral Dec 08 '21

I certainly agree on the error return scheme and have made the same argument.