Hacker News new | ask | show | jobs
by sdegutis 3534 days ago
Honest question from a non C++ developer: what's the main point of Rust? What's the primary reason for choosing it over any other language? When would it be most appropriate to choose?
7 comments

Rust is a replacement for C++. It is meant to be a modern, general-purpose systems language, where "systems" generally means anywhere you need to be close to the metal. This includes areas like embedded systems and high performance computing. Modern means it has features and tooling you expect from languages in 2016. For example, on a language level, Rust has type inference, sum types, pattern matching, proper generics, and a decent macro system. A modern language should also be memory safe, and Rust opts to ensure memory safety at compile time with the borrow checker (as opposed to runtime with a garbage collector like in most languages). As for tooling, it also has a proper package manager, which believe me, is incredible compared to handling dependencies in C++.

So why would you choose Rust? If you need to use C++, because you need performance, or low-level access to memory, or any other reason, then you should choose Rust instead, because it's better for all the reasons listed above. The biggest strike against Rust compared to C++ is library support--people have been writing C++ and C libraries for decades, and Rust is a new language, so it won't have the same level of support.

While Rust is positioned firmly as an option in the same situations where C or C++ would be chosen, I'm personally of the opinion that it has a great potential of capturing use cases of higher-level languages as well, with the advent and work of Rust running on WebAssembly, it will become one of the first serious web client language alternatives to Javascript.

It's partly because of the memory safety features. There have been quite a few people I've noticed coming from languages like Ruby and Javascript and being successful. Whereas I think C and/or C++ might have been more challenging, again b/c of memory safety (GC's are a lovely thing to rely on, until they get in your way). The compiler offers a huge advantage here, because while it is true that it is much more restrictive than other languages, it also almost guarantees that your code is correct.

As an old C hack, with bad memories of C++, and a hater of the memory footprint of Java and the horribleness of the great GC pause; Rust has been a joy to learn and use. It's like bowling with the bumper rails up as an adult, and no one laughs at you (well, maybe they do, but you never throw a gutter ball!)

Yes, what wcrichton said. Additionally, I have a suggestion. One of the deciding factors for me to fall in love with Rust was my habitual use of metaphor to distance my personal feelings from the issue at hand to reach an impartial decision. In this case, I suggest you replace "libraries" which may hold emotional significance for you with another concept of similar enough nature. Bias may also come from misperceptions, such as some inexperienced web devs feeling compelled to use jQuery to make everything, or naïve hobbyists thinking Arduino boards are the hammer for all nails.

For the sake of hypothetical example, presume I have had a long history of involvement in some C++ libs which I associate with good times, hilarious IRC sessions, friends, and pouring love into the labor of creating them during 0300 caffeine overdoses. How could I possibly evaluate leaving this behind without such bias?

Many choices are vulnerable to silly human biases which are hard to detect. Debating C++ versus Rust seems to be tough for some, and technical arguments have a propensity for becoming circular, so here's an analogy instead.

- Imagine, (if you have no extreme attachments to the web) that we have created a new web with new protocols, software, hardware, solved most crypto issues, authentication is reliable, we created the safe and anonymous place of everyone's (some ppls) dreams. Most of the advances are wonderful and (mostly) fueled by the latest research and smart minds, but there are a few bugs. Overall, an improvement and well adopted but still needing polish. The problem? All the old content isn't there. Some amazing new experiences await, but reddit and youpn are not unless you pull up an old web browser. If you were tasked with creating new content, which web would you choose and why?

I propose devs consider following either this, or some other method to evaluate C++ vs. Rust more objectively and try to set attachments aside. If, rather than thinking "but I won't have libX", you ask "how can I implement this functionality better today", you may decide that casting off old clutter is liberating and insightful.

I hope this helps some of you.

If you think Rust is ready for high performance computing, I suspect you don't do HPC for a living and you haven't tried to do it with Rust. It's not just missing the needed libraries.
Can you expand on that?
What else is missing?
I think it works as a C replacement in HPC. Maybe not a C++ replacement (mostly because of missing integer generics).
I suppose if I identified with any language, C++ would be it. I have the most knowledge and experience writing code in that language. I've been learning Rust lately and have written a few toys with it. I'll share my thoughts.

1. The main benefit of Rust is its nearly cost-free safety guarantees. It's much harder to write code that is prone to UB with Rust than with C++. The comparison is almost silly, in fact, from that perspective. It comes with a runtime cost (e.g. drop flag checks, bounds/other memory access checks) in some cases, but for all cases other than those requiring even that last bit of performance this cost is tiny in comparison with the work the rest of the program is doing and well worth it in my opinion.

2. The primary reason for choosing it over any other language is context dependent. This leads me to my last point.

3. It would be fine to use for anything, really, but I think in the sense of "most appropriate" that you mean, it's "most appropriate" for systems programming that needs to be as fast as C++, but not necessarily melt-the-metal fast. Primarily this is because: a) it's as fast as C++ in almost all cases (and probably faster than equivalently-safe C++ in at least some cases) and b) it's much less likely I'll write code that smashes stacks, has data races, blows up memory or does a bunch of other crap that's trivial in C++ (or other languages). Even in cases where melt-the-metal fast is necessary it seems possible to do in Rust, but it just takes a lot of effort. That's deliberate on the part of the Rust developers, and it's difficult to argue against the reasoning behind it.

Where Rust might really shine is helping a new generation of programmers write performant code that is safe without having to learn by trial-by-fire.

I wrote a Donald Trump text to speech server in Rust. It's blazingly fast. If I'd written it in Python, it'd have taken seconds to generate audio. If I'd written it in C++, it'd likely have memleaked or segfaulted since I'm not skilled or patient enough to write safe C++.

Rust is pretty much the coolest thing ever in my opinion. It's easy and fun to write. Cargo is the best package manager ever, and makes importing library code a breeze. I've never had multithreaded code warn me when I don't lock it correctly. It's so awesome.

(wrt my Rust app, as soon as I load test and redesign the UI I'm going to share it on HN. It's pretty ridiculous and we have a lot of fun with it at work.)

Have you tried with Go?
I'm honestly not a very big fan of Go. We use it at work and it just isn't something I want to come home and write. Different strokes, I guess.

I'm sure that Go would have performance characteristics closer to Rust than Python would, but I'm almost certain Rust would still edge out Go.

I'm getting to the point where I'm almost as productive in Rust as I would be in either other language, and that makes Rust a no-brainer for me.

I'd like to respond by this and saying I'm porting a largeish codebase over to Rust for one purpose: I can embed it other languages, and I can into it from any language that exposes an FFI.

The goal is to move all business logic out of javascript on an nwjs desktop/crosswalk mobile app and move it to Rust. The interface would still be javascript, but the core logic (and crypto) would live in Rust. This gives me a portable core I can move between platforms, and in theory all that has to change is the UI (but not really, because in most cases the platforms run Javascript).

I don't think Go can be as easily embedded (because of its runtime) but perhaps I'm wrong.

Compared to C++:

- A fantastic package manager for your dependencies

- Easy build system

- A nicer language

- Memory safe

- Just as fast

- A way stronger compiler that will prevent you from making mistakes that you can make in C/C++ very easily

All in all more then enough benefits to make it worthwhile to switch unless there is a very good reason not to.

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.

Featuring

  * zero-cost abstractions
  * move semantics
  * guaranteed memory safety
  * threads without data races
  * trait-based generics
  * pattern matching
  * type inference
  * minimal runtime
  * efficient C bindings
That is from the Rust front page. HTH.
It's difficult to answer this question with 'one main reason'. Rust is what you get if you build a language that is intended to operate in the same space as C and C++, but benefits from the last 30 years of advances in programming languages.

Some things that are better as a result of things that are hard to do right in C++:

* Compiler monitors ownership of memory

* Compiler monitors ownership across threads

* Generics aren't handled as templates, and you can explicitly describe support using things like * trait bounds

* Macros are useful but need to operate on something more like the AST rather than text tokens

* A richer type system can help you express much more in the types (Rust enums)

Rust is basically a safe C++, written by people who like functional languages. Also, it has an official package manager in Cargo, something that C++ lacks completely.

As such, it targets essentially the same niche as C/C++.