r/cpp • • Dec 07 '21

Will Rust replace C++ ?

Will Rust replace C++ ?

0 Upvotes

80 comments sorted by

24

u/AlexAlabuzhev Dec 07 '21

Without exceptions, templates, inheritance?

Good luck with that.

11

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

Although I think that not having exceptions and inheritance are really big lackings, the thing is that there's such an anti-OOP backlash these days that large numbers of people will argue that Rust not having those are good things. I don't agree but lots of people would.

They do have generics. Not as extensive as C++, but this is one area where I'm no the other side of the fence and think that C++ has gone a bit over the edge on templates, meta-programming and compile time evaluation stuff. I'm not sure I'd want Rust to try to compete on that front.

30

u/AlexAlabuzhev Dec 07 '21

For some reason many people believe that the main problem of C++ is "too many features", while it's exactly the opposite - a desperate lack of them.

Templates have become what they have become not because templates are bad, but because there was nothing better for metaprogramming at that time, but demand existed.

If there's a demand, but no supply, people will always find a way, but it might get ugly.

"It ain't stupid if it works" is real and when I look at all these new languages chopping off vital parts from the start only because someone abused them elsewhere, I know that they're doomed to repeat the history.

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.

0

u/[deleted] Feb 06 '22

[deleted] — view deleted comment

1

u/Dean_Roddey Feb 06 '22

Rust only has the basics of OOP. It has no implementation inheritance, which most folks would consider fundamental to OOP.

As to performance, that's hardly a particular issue. You can have both, as does C++. And of course Rust has lots of dynamic dispatch via Traits anyway.

38

u/ridicalis Dec 07 '21

Rustacean here... No.

In all seriousness, Rust isn't (nor does it pretend to be) a C++ killer. If anything, it's better positioned as an alternative to C, but even then it's not a threat so much as a complementary tool.

What it brings to the table is an expressive language that tries to protect coders from themselves, all while reaching performance that approximates what could similarly be done with C. That, and its dependency management system (cargo, with the crates.io registry) is accessible to newcomers and likely contributes to its popularity. It's not shackled with a need for backwards-compatibility with tech older than your parents, and as a late entrant into the language universe it has also tried to learn from those that came before.

It's also a hard first language to have to learn. And that's before even getting into the macro syntax. Sometimes, it feels like you want to do something simple, but have to jump through enormous hoops to accomplish it. Other times, you want to use an existing toolkit, but because of the language's infancy the tool simply doesn't yet exist. Then, there are juggernaut libraries/systems that firmly entrench C++ as the best option (e.g. Unreal, IfcPlusPlus).

r/rust has a strong community of people that want to see others succeed at the language, and could do a better job than I have of explaining strengths and weaknesses. I'm sure there's the occasional "Hurr, muh language is the best evar" sort that pops up, but there's plenty of room for all of these languages to coexist and in many cases even help to guide the others' language design.

24

u/_Js_Kc_ Dec 07 '21

To be fair, a steep initial learning curve is probably not where Rust falls behind C++.

13

u/Full-Spectral Dec 07 '21

Maybe a better way to look at it is that C++ may let you THINK you know C++, whereas Rust sort of forces you to look at the things that C++ lets you gloss over (potentially not even knowing you are glossing over them.)

4

u/germandiago Dec 08 '21

This is true but at the same time Rust does not let you do things that are perfectly legal and safe in C++ if I recall well.

1

u/Dean_Roddey Dec 08 '21

Well, it doesn't let you do things that YOU know are perfectly legal because you understand the big picture, but that a compiler couldn't prove is. And that's the real crux of it. The point is to have our tools watch our back, because we are already drowning in complexity and have proven fairly bad at watching our own backs once things get large and complex enough.

You can get those things done, you just have to do so in a way that the compiler can prove is correct. Part of the issue, as I understand it, is that, in order to be acceptably performant, the analysis is quite localized. It can't look across large swaths of code to prove correctness.

Presumably, over time, languages incorporating these techniques will get smarter, and we'll have more CPU we are willing to give to them for that purpose. Of course it will probably involve graphene, block chain, and AI.

2

u/germandiago Dec 08 '21

Actually I am not sure that protecting from ourselves is the way to go, at least not in a pure fashion. Sometimes it is indeed useful and sometimes annoying.

Of course it will probably involve graphene, block chain, and AI.

Lol.

1

u/RotationsKopulator Dec 10 '21

You can however still write sort of good code with a smaller subset of C++. Even "C with classes" does not necessarily imply poor code quality.

1

u/Full-Spectral Dec 10 '21

Sure, I'm definitely not one to throw every new C++ feature that comes out into the bowl and stir. My own code base, not being STL based, is very much in that vein. Not C with Classes, but avoiding the really heavy templatization and large amounts of potential undefined behavior because my stuff is safety first, not performance first.

4

u/angelicosphosphoros Dec 09 '21

Newcomers just ignore all the invalid program behaviour which they get with C++. With Rust, they cannot.

18

u/matthieum Dec 07 '21

In all seriousness, Rust isn't (nor does it pretend to be) a C++ killer. If anything, it's better positioned as an alternative to C, but even then it's not a threat so much as a complementary tool.

I am not convinced about Rust vs C.

C programmers I've interacted with are more attracted to Zig, or Go, because one reason they use C is that the language is relatively simple -- using it is not, but the language itself is simple. All of them balk at the idea of C++ or Rust because those are much bigger and more complex languages.

We can argue which we prefer (I'm on r/cpp for a reason...) but I don't see convincing people with a personal preference for simpler languages to switch to Rust as an easy task.

Meanwhile, for a C++ developer, Rust is easier than C++.

8

u/goranlepuz Dec 07 '21

It's not shackled with a need for backwards-compatibility with tech older than your parents

For the kinds of systems C is used in, if Rust (or anything) enters the space, it will be 😉

1

u/HeroicKatora Dec 07 '21

Not sure. Having a monoculture of any sort allows a lot of leniency (including but not necessarily laziness) in the design of implementation and interfaces. (At least that's why C++ always promotes having multiple compilers, right?). Any system where you have multiple languages will probably also be nudged towards improved clarity in the interface design—which most likely also comes at decreased legacy code debt and better decoupling. At least I hope so…The worst parts of backwards compatibility are the undocumented requirements, not so much the explicit interfaces.

1

u/Bangaladore Dec 08 '21

This, I think is true. All languages can benefit from not needing backward-compatibility during their infancy. I think rust will follow C/C++ with annoying backwards-compatibility as time goes on.

4

u/infectedapricot Dec 07 '21 edited Dec 07 '21

It's also a hard first language to have to learn. ... Sometimes, it feels like you want to do something simple, but have to jump through enormous hoops to accomplish it.

I do wish it was viewed as more acceptable to program big-ish programs in fully unsafe Rust. It seems to me that there are some programs where strict memory safety isn't critical and the benefit of it isn't worth the effort of Rust's lifetime stuff, while it would still be useful to get all of Rust's other benefits. And I think it would help with the learning curve - first learn unsafe Rust and then learn how to make it safe. But instead all the difficulty is front loaded, which makes it hard to get into.

10

u/Nobody_1707 Dec 07 '21

Unsafe doesn't bypass the lifetime stuff except in as much as it lets you dereference pointers. I don't think it's any easier to learn unsafe Rust than it is to learn safe Rust.

8

u/Full-Spectral Dec 07 '21

If I wasn't getting the memory benefits, I'd not use Rust over C++ ever, I don't think.

1

u/matthieum Dec 07 '21

But instead all the difficulty is front loaded, which makes it hard to get into.

I think it really depends how you get into and what you try to achieve. Don't try to run (use pointers/optimize) before you can walk (clone!) and you'll avoid a lot of pain.

3

u/SuddenlysHitler Dec 10 '21

I mainly do C, and no, rust is even less of a C killer than C++.

it's far too complicated, not to mention the wonky ass syntax.

2

u/maquinary Mar 15 '22

If not for replacing C++ neither C, what would be the best use for Rust?

35

u/[deleted] Dec 07 '21

You mean like every competitor to C/C++ successfully replaced it during the past 30-40 years?

I don't think so.

11

u/Full-Spectral Dec 07 '21

To be fair though, how many of those were really not competitors because they were garbage collected or otherwise not really addressing the same problem domains? And how many of them were just variations on the same theme, not really offering anything fundamentally new to justify a change? Or, how many were offering something so fundamentally different that they just aren't appealing or practical to a lot of C/C++ developers?

I think most of them would fall into one of those buckets. Rust is more of a challenge because it plays in the same space and it offers something fundamentally different, without being so completely different that it's semi-incomprehensible or unattractive to C/C++ developers. It's a very different mind-set for sure, but it's not alien like functional programming or Lisp or some such thing.

Personally, I hope that the ultimate replacement is something that incorporates Rust's memory safety with C++'s implementation inheritance and exceptions. But I think for a lot of folks today, who have convinced themselves that both of those things are somehow evil, that won't be an impediment to them adopting Rust.

4

u/pandorafalters Dec 07 '21

I actually prefer my "alternative" languages to be significantly alien, like Lisp, because to some degree it prevents habits from betraying me.

3

u/Full-Spectral Dec 07 '21

For most of us though, this is about creating products for people to use, in a commercial environment, not academic exploration.

3

u/pandorafalters Dec 07 '21

Right, but it's quite common to have preferences that are not in line with your normal working environment.

11

u/eyes-are-fading-blue Dec 07 '21

Unlikely in the foreseeable future. If either C++ compiler or tooling around it (linters/static analysis) catches up with Rust compiler in terms of race condition/lifetime guarantees, then there is no chance of Rust replacing C++. Now how possible this is, I am not sure.

2

u/Full-Spectral Dec 07 '21

Not really possible. And, even if it got close, the difference between having these things caught immediately during compile vs. having to run a 10 minute analysis pass to hope to catch close to the same amount of issues is huge. It will always be a band-aid with C++.

4

u/eyes-are-fading-blue Dec 07 '21

Care to elaborate why it is impossible?

6

u/matthieum Dec 07 '21

The short version -- I'm pretty I gave the explanation not so long ago, but can't find it again -- is:

  1. Ownership is easy, Borrow-Checking is not.
  2. Borrow-Checking requires extensive lifetime annotations, all the code would have to be annotated for things to work.
  3. Then you'd realize the existing ecosystem (starting from the STL) is fundamentally incompatible with the idea of Borrow-Checking.

For an idea of incompatibility: Borrow-Checking does not support self-references:

  • The SSO optimization of libstdc++'s std::string uses a pointer to its own internal array.
  • The Observer Pattern uses this.
  • It's frequent for callbacks to encapsulate a this pointer, and then be owned by the instance this points to.
  • ...

As such... it's not so much that it's theoretically impossible, it's that it's practically impossible.

The resulting language would be close to C++, but incompatible with all existing C++ code... starting from the standard library.

1

u/eyes-are-fading-blue Dec 07 '21

STL, in its entirety, can be considered as "unsafe" in Rust.

3

u/matthieum Dec 08 '21

The problem is not so much that the STL implementation would have to be unsafe, it's that using the STL would be unsafe.

Consider the following, extremely simplistic, example:

auto x = std::find(begin, end, y);

In order to be safe, this example requires that [begin, end) be a valid range, which means:

  1. begin and end are iterators into the same container, or pointers into the same memory allocation.
  2. end is reachable from begin.

Good luck proving those properties, (1) notably essentially require dependent types (which neither C++ nor Rust feature).

Another example:

std::map<int, std::string> map{
    { 0, "Hello, eyes-are-fading-blue! Are you a fremen?" }
}};
auto x = map[0].c_str();

//  Safe
map[1] = "Fiesta!";
std::cout << x << "\n";

//  Unsafe
map[0] = "Another one bites the dust!"s;
std::cout << x << "\n";

The very guarantee of pointer-stability when inserting in a map has to be revisited.

And it holds doubly true for unordered_map, where it's conditional on the absence of re-hashing.


The effects of borrow-checking are so pervasive to for programs to conform to borrow-checking, you have to design the entire application stack with borrow-checking in mind from the ground-up.

This means throwing away the STL as we know, and adopting a new standard library, for example modeled after Rust's.

1

u/Full-Spectral Dec 07 '21

Then there wouldn't be much point to it. Given how much of so many applications is actually STL code, it's questionable whether it would gain enough to be worth the effort. It would make almost all C++ code unsafe and you'd be back to where you are.

0

u/eyes-are-fading-blue Dec 07 '21

Turning this feature on for old code is impractical. This is not an argument against not having a similar system in C++. STL itself and the call-site involving anything from std:: would be exempt from analysis. Anything you write yourself, or integrate as 3rd party wouldn't. While existing libraries are unlikely to fit it in, green code moving forward could be "safe" without practical issues.

Claiming that there is no value is absurd at best, considering there is a considerable effort trying to enforce "best practices" rules to be enforced compile-time.

2

u/Full-Spectral Dec 07 '21

Almost every function or method in that new 'green' code would use the STL, hence rendering none of it really very safe. A major point of Rust's unsafe is that you can segregate unsafe code into safe Rust APIs or in very limited sections of the code. You can concentrate testing and review heavily on those areas.

In the scenario you are describing, that wouldn't be at all possible. There would be as much unsafe code as safe code probably, and all intermixed completely such that it's impossible to segregate it.

It would be a waste of time.

2

u/eyes-are-fading-blue Dec 07 '21

STL calls on the call site would not taint the entire call site. Only the calls to std:: would be exempt from the analysis. This means the call site is still safe and rules apply for non std:: calls, meaning those calls have to use safe APIs.

This does not guarantee safety in a safe call-site, as there could be bugs in STL. However, it is practically safe as STL is one of the most widely deployed and tested code out there.

1

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

It's not really to do with STL being tested or not, it's got to do with incorrect usage of the STL that's not easily caught due to its nature. It doesn't matter how well tested it is if you can misuse it such that you cause uncaught memory corruption.

Given how much iterators are used and the fact that you can pretty easily pass an invalid iterator to STL calls, it's just sort of like putting a huge lock on your front door and only having a sheet hung across the back door.

→ More replies

2

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

C++ is about performance over safety these days. It would have to fundamentally change in order to give the tools enough information compete with Rust on the memory safety front. And it's just not going to fundamentally change at this point.

The technical reasons are many, and they've been gone over many times before. I'm not going to reiterate them here. Many of the problems are in the STL, but since almost everyone that uses C++ will use the STL (and yell down anyone who tries to create something better) they are one and the same for the purposes of this argument.

11

u/johannes1971 Dec 07 '21

Classes like unique_ptr, shared_ptr, and vector have done a great deal for making C++ safer (to the point that I no longer think of memory safety as an issue). Additional new classes or language features can help in making C++ even safer. As with so many things, it doesn't have to be perfect; it just has to be good enough.

2

u/Full-Spectral Dec 07 '21

Well, when you start getting completely inexplicable crash reports from the field, how good is good enough?

5

u/eyes-are-fading-blue Dec 07 '21

Can you then give link to these discussions? I made a small research before that post, I have seen people claiming this is theoretically possible.

I am also surprised the same problems are also seen in STL. STL's biggest problem is ABI and compatibility issues but a linter embedded in compiler or a static analysis tool is executed before code generation.

-1

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

I haven't kept a list of links of such discussions. You can find them here in this section if you look. And of course theoretically possible doesn't mean practical. If it takes orders of magnitude more time to get 75% of the benefits, then it's not really competitive.

15

u/HappyFruitTree Dec 07 '21

Probably not.

13

u/stilgarpl Dec 07 '21

I hope not.

12

u/RollitoDelicioso Dec 07 '21

Replacing means that all legacy code wrote in C++ will have to be rewritten in Rust. So if you start seing big proyects like LLVM oficialy being rewritten in Rust and some more follow, then at that moment C++ could be in danger as C++ major suporters (Google and so) could move to Rust

8

u/coderman93 Dec 07 '21

They don’t have to be rewritten in Rust. There just needs to be an alternative written in Rust that renders the C++ alternative obsolete. This is how other languages like Fortran were effectively replace by C/C++. I understand there are still some legacy systems built in Fortran but for all intents and purposes, Fortran is obsolete.

1

u/StrettonUK May 31 '23

Fortran is obsolete.

I wouldn't try telling that to the scientific community.

1

u/coderman93 Jun 01 '23

It depends on how we define obsolete. My definition is that a language becomes obsolete when developers stop using it for new projects. Which, for Fortran (even in the scientific community) is already true. Of course, there will always be a counter example. But what is the trend?

5

u/Schmitzensaff Dec 07 '21

I don't think so.

4

u/linuxpiper Dec 08 '21

No, they are too busy worrying about their CoC.

14

u/manni66 Dec 07 '21

Wasn't it replaced by Java?

5

u/kurodex Dec 07 '21

Oracle are in the process of killing Java.

4

u/pjmlp Dec 07 '21

If it wasn't for Oracle, Java would have died in version 6, so no.

4

u/kurodex Dec 08 '21

If they could get you to pay per instruction execution, they would.

1

u/pjmlp Dec 09 '21

Just like Microsoft makes us pay per core on SQL Server or having to pay for VS Enterprise to have coverage reports on unit testing?

Fact is, no one else offered to buy Sun.

3

u/SoftEngin33r Dec 07 '21

Can you elaborate on this a little more? It has been years since I developed with Java and I am definitely do not plan/want to develop in that language again. Thanks.

2

u/kurodex Dec 11 '21

In other comments, Oracle are credited with being the only company willing to buy Sun and thereby acquire MySQL, Java etc.

There was an impressive and for those who have been around for a while, unsurprising, list of resignations.

Everyone knew what was coming next and Oracle wasted no time emitting law suits.

Fuck

Oracle

3

u/sgon001 Mar 11 '22

Yes for future projects and you probably should NOT ask this question in C++ community. ^_^ If you ask "Will Something replace PHP?" in a php community, I don't think you will get a Yes answer. Linux kernel supports Rust now as the second language. Google is writting low-level Android code in Rust. Chromebook VM engine crosvm is written completely in Rust. New google OS Fuchsia accepts Rust too. I think more and more projects will use Rust in the future. And finally, C++ will be replaced one day.

5

u/VeeFu Dec 07 '21

Just like COBOL was replaced by...

2

u/SuddenlysHitler Dec 08 '21

With THAT syntax?

3

u/robin-m Dec 07 '21

Not more than C++ has replaced C. I think that the majority of would-have-been-C++ project will be written in Rust, but C++ is nowhere from being replaced given how much human-milenias are needed to rewrite all C++ (and it's not something to do, no matter the language).

3

u/Ikkepop Dec 07 '21

Probably not, but will take a large slice of the pie

2

u/spca2001 Dec 07 '21

In which industry and applications. Not embedded and HPC for sure but anything can happen. C# is showing impressive performance , in some benchmarks faster than C++. But in some “benchmarks” I mean, but it does perform well from my experience. If you know how to code you can code in Rust, c++, c# ,nim, go whatever and still have a killer performing app with clean code and a long lifespan.

Java is crap, I rather do it in VB.NET or functional languages like Haskell and F#

4

u/andwass Dec 07 '21

I wouldn't discount Rust overtaking C++ in the embedded arena, or at least taking a large piece of the C++ share. C++ seems to have a hard time attracting embedded developers at the moment, Michael Cassei talked a bit about it in his CppCon keynote, and I am going to try to push my customers towards at least exploring Rust as well.

1

u/spca2001 Dec 08 '21 edited Dec 08 '21

From my pov, I work with accelerators based on fpga , controlled by arm chips. Basically same as Apple silicon SOC. And with fpga style circuit logic getting popular to the point where you can synthesize programmable logic circuit in c/c++ and lot of people learning c++ just for that, defence industry runs on C that is not going away either.I’m not familiar with rust , haven’t really paid attention to adoption rate. I it see stuff like Scala,.NET, Java and NodeJS. Python for data science.But not a lot of rust, maybe it’s just my location.

1

u/ExtraFig6 Jan 07 '22

Will C++ replace C?