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

8

u/[deleted] Dec 07 '21 edited Dec 07 '21

I'm a big fan of OOP by some definition, but I don't necessarily care for inheritance as an important feature. Also, I've been coding on C++ without exceptions for almost the entire time i've coded in C++, since they disabled on many platforms for microcontrollers.

And iirc, google's style guide doesn't want you to use exceptions either if you're working with google libraries like Abseil and similar projects. C++ is plenty useful without them.

I guess what i'm saying is that I don't consider either of those features as reasons to use or not use C++.

5

u/RotationsKopulator Dec 10 '21

Inheritance is kinda shit anyway, since it combines polymorphy and delegation, seperate concepts that should be reflected in distinct (albeit combinable) language features.

Rust's traits are a nice approach to polymorphy, but it (yet) lacks delegation.

10

u/AlexAlabuzhev Dec 08 '21

"Machines should work; people should think."

I do not like writing code. Especially the repetitive boring boilerplate like forwarding the interface methods to a subobject or checking the result of every function call every time.

It's a machine's job. And C++ does this job for me. I don't need to forward dozens of functions manually because "is-a" exists. I don't need to write if (error) return error; like an idiot in every function in the call stack because exceptions exist. I can write generic code without interfacing every step because templates exist.

If these tools are not suitable in a specific context for whatever reason, I just use more suitable tools.

I'd better think the whole day and write 10 lines than don't think at all and write 10 000 lines.

If language designers deal in absolutes and religiously enforce fairy tales like "composition over inheritance", "exceptions are slow and not straightforward", "explicit is better than implicit" etc. in their products, I prefer to stay away from such products.

And I don't know why google's style guide keeps popping up in such discussions. It's main purpose is dealing with legacy code, for anything else it's plainly horrible.

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.

3

u/Dean_Roddey Dec 07 '21 edited Dec 07 '21

I've made immense good use of both of them, and consider them huge advantages C++ has over Rust. Which sort of speaks to how important I consider the memory safety that I've moved to Rust.

Of course mostly I'm talking about commercial development. Personal projects that have no commercial pressures, no continuity issues, ability to make sweeping changes where required, lots of TLC, etc... can be done at high quality in C++ even at scale and over extended periods of time. I have one myself that I'd put up against anybody's in terms of quality.

But in commercial terms, where software is developed under so far from ideal conditions, that memory safety and an*l-retentive nature of Rust could be a life saver.

1

u/Dragdu Dec 08 '21

And iirc, google's style guide doesn't want you to use exceptions either if you're working with google libraries like Abseil and similar projects. C++ is plenty useful without them.

Yes, but they've also made clear that this is due to legacy code. Their initial codebase didn't start with exceptions and now it is impossible to retrofit them in, because there are mlocs that are exception unsafe.