Live data from Hacker News

Switching from C++ to Rust

laplab.me

131–140 of 289 posts

Re: Switching from C++ to Rust

#131

Earlier quoted context omitted.

The overhead is the same. Rust just takes care that you're doing it right, while C++ lets you shoot your foot off in this area.

I know this is an unpopular opinion but if you want to stick with C++, the solution to this, at least in my experience, is to stop doing things that let you shoot your foot off. C++ gives you every tool in the tool chest, and most of them are not safe. Stick to a safe subset and you've solved 90% of all those stereotypically C++ problems. I've got a pretty big C++ codebase for my hobby projects, sanded down, polished…

Yes, but "just don't do that" doesn't scale to large teams with varying levels of experience and discipline. It sounds like you have rules that even you disobey on this project.

Re: Switching from C++ to Rust

#132

"First of all, generics without duck typing are greatly appreciated." Err..C++ has concepts. No duck typing required.

Only C++20 has concepts, which is a lot newer than Rust is. I've yet to ever see a C++20 concept in the wild or anyone using one outside of an example.

Heck, if you ask the average C++ programmer about concepts they'll likely have not even heard of them.

Re: Switching from C++ to Rust

#133
post #43

Earlier quoted context omitted.

Yeah. In the C++ codebase I work on, we use folly::Expected (essentially similar to Rust's Result ) heavily. Is it as ergonomic as Result is in Rust? Absolutely not. But it's still a nice design pattern for some of the same reasons people use Result in Rust. (And C++ std::optional is somewhat similar to Rust's Option type.) https://github.com/facebook/folly/blob/main/folly/Expected.h...

Swift has a similar Result type, it’s very handy.

Swift on linux is supposed to be decent server side, but as far as I've heard, not much use outside of that and even limited testimonials on that. The bit I have dabbled in Swift, I really like it. I just wish it grew more out of the Apple ecosystem. It has lots of things I like, but considering I can't exactly take it with me to any system and just run with it is why I have not dived to much into it.

Re: Switching from C++ to Rust

#134
post #28

Earlier quoted context omitted.

I was just trying to find a modern C++ tutorial, but there's nothing comparable to the Rust Book on the internet. I think even now the best resource to learn C++ is to first learn C, then original C++, then all the modern memory management techniques, which is just crazy hard for a new programmer compared to just going through the Rust book 10 times (which is needed to get a deep understanding).

Professional C++ is the 1072 page book that taught me modern C++, for what it's worth. I have the physical copy. And even then I have gripes with some of it's contents! The Rust book is second to none, tbh, and I say this as someone who doesn't really write Rust.

As someone who wrote C++ for 20 years, just don't. It's a horrible language(s). None of the codebases look the same, everyone uses different features or different versions of the language. Building is a nightmare, it takes forever to do anything. Run!

Re: Switching from C++ to Rust

#135

Earlier quoted context omitted.

Swift has a similar Result type, it’s very handy.

Swift on linux is supposed to be decent server side, but as far as I've heard, not much use outside of that and even limited testimonials on that. The bit I have dabbled in Swift, I really like it. I just wish it grew more out of the Apple ecosystem. It has lots of things I like, but considering I can't exactly take it with me to any system and just run with it is why I have not dived to much into it.

Is swift outside of Apple as fully fledged and performant when it comes to concurrency? Last I checked, a lot of the heavy lifting came from GCD (ie the Apple runtime-scheduler)

Re: Switching from C++ to Rust

#136
post #72

Earlier quoted context omitted.

struct List {}; struct Cons : public List { Cons(int v, List* nxt); int v; List* nxt; }; struct Nil : public List {}; This is how you'd do it in Java.

and you would use it ... how?

It's not exactly Java, but:

    Cons(1, Cons(2, Cons(3, Nil)))

Re: Switching from C++ to Rust

#138

Earlier quoted context omitted.

The overhead is the same. Rust just takes care that you're doing it right, while C++ lets you shoot your foot off in this area.

I know this is an unpopular opinion but if you want to stick with C++, the solution to this, at least in my experience, is to stop doing things that let you shoot your foot off. C++ gives you every tool in the tool chest, and most of them are not safe. Stick to a safe subset and you've solved 90% of all those stereotypically C++ problems. I've got a pretty big C++ codebase for my hobby projects, sanded down, polished…

Everything you said is true. C++ gives you everything you need to be safe. But that's not the point. Rust makes it HARD (or very hard) to do things that are unsafe.

It's not what C++ can or cannot do, it's not what Rust can or cannot do. It's what the Rust language and compiler opinionatedly encourage you to do and not to do. This is what's lacking in C++.

Re: Switching from C++ to Rust

#139

The call out to sum types is something I feel. I've been using Rust daily for almost 10 years now, and sum types are absolutely still one of the things I love most about it. It's easily one of the things I miss the most in other languages that don't have them. I'm usually a proponent of "using languages as they're intended," but I missed exhaustiveness checking so much that I ported a version of it to Go[1] as a sort…

Until very recently, all of the C++ code I wrote was in the 1998 style or in the 2014 style. Since 2017, there is std::variant, which is a sum type template that _technically_ allows for language-level exhaustiveness checking via std::visit. It's not pretty, but it gets you there without requiring compiler support. Rust's version looks way better.

boost::variant has been around forever.

Most modern C++ codebases do compile-time polymorphism via templates, though. std::variant is a niche use for things like serialization, when you need to make type choices at runtime.

Re: Switching from C++ to Rust

#140

Earlier quoted context omitted.

This is exactly what I think too. Sum types are so powerful, I feel a lot safer in Python + mypy with sum types (`from typing import Union`) than anything with C++ [1] even though C++ has a significantly more complex type system (it's type system is TC) and Python is as type-unsafe as a language can get. C++ made this odd choice as if any complex type system is better than a simple type system. When I was a younger s…

> even though C++ has a significantly more complex type system (it's type system is TC) For what it's worth, Python's type hints are also Turing complete ( https://arxiv.org/abs/2208.14755 , discussed on HN at https://news.ycombinator.com/item?id=32779296 )

[deleted]
Post reply on HN