News for hackers. Stuff that matters.
Re:Sayonara, C++, and Hello to Rust by moth-fuzz on Tuesday December 07, 2021 @03:11AM (#29470132)
Trait objects are still no replacement for the areas in which subtype polymorphism excel, and Rusts' generics are a pittance compared to the completeness and expressiveness C++ templates. I know that sounds like flamebait, and yes I've read STL source code in all its underscored and abbreviated glory, and yes I've read that one snippet of the std::ranges proposal that people like to throw around. But the thing that always gets me is that most Rust proponents I've talked to simply define out of the equation Rust's problem areas - an oft-repeated mantra is that Rust 'pushes you to a good design' as if what is inherently a good design are what Rust is capable of and the areas it is less so are simply inherently poor designs anyway.

Sure you can do everything you'd want to do in C++ in Rust, but the developer experience when actually writing code is agonizing most of the time. I deeply miss C++ templates in most other languages, excepting fully dynamic languages, and D. Rust is, to me, more of a replacement for places I'd write C, and be in a C mindset. C++ (well-written, modern C++) is so much more than C, and so much more than people give it credit for. A true C++ contender would have to pass not just the C bar but the C++ bar as well, rather than looking at C++'s strengths over C and saying "actually those are bad things" like it's been popular to do for the last 10 years.

Speaking of replacing C, Rust is not without its strengths: the one place I find Rust truly a joy to write is OS dev, and similar low-level projects, in which the concepts and limitations of the language map to quite nicely to the expectations of the system. It seems like the obvious candidate for these types of applications (including embedded development) but there's little surrounding literature - it always baffles me that most people seem to be using Rust to write ... web services?

Re:Sayonara, C++, and Hello to Rust by berkut on Tuesday December 07, 2021 @03:44AM (#29470306)
Definitely for prototyping, as someone with years of 'advanced' C/C++/Python experience, and someone trying to learn Rust, I'm finding Rust's 'opinionated' ways of doing things very painful for things like fast prototyping, where you need to impl 'default' traits on structs, or create builders (which is almost as bad as C/C++'s split of declaration and implementation in that you have tightly-coupled things in different parts of the code.), sticking unwrap() everywhere to not handle all errors correctly at this stage, etc, etc. i.e. you don't really know what you want, seeing what works, how the data could be organised, refactoring in the early stages, and so much is in flux at this stage, the compiler's warning about all sorts of things, but I don't want to fix them as it might be a waste of time at that stage if you change things yet again, so just end up shoving: #[allow(dead_code)] above all enums or something, so I can actually see errors within all the warnings.

Once you move past the prototyping stage on to full on production coding, I totally buy that in a lot of cases, the safety that Rust provides (if it compiles, it will almost certainly either panic or work, ignoring logic bugs), is a big win, but I'm not convinced yet for experimentation when prototyping...

Re:Sayonara, C++, and Hello to Rust by littlestymaar on Tuesday December 07, 2021 @04:02AM (#29470401)
I personally love Rust for prototyping for one particular reason: even if it's not as quick as what you'd do in dynamic languages (JS for me, for most people it would be Python) it comes with an enormous benefit: iterating on your prototype several times until you eventually reach the final version is much, much quicker than in any other language I know, simply because the compiler will catch almost any mistakes you make during your refactoring.

So, The first draft is going to take like twice as long, but every subsequent iteration on your prototype will be much faster. I find the payoff time to be pretty quick (like one or two iteration are often enough), but obviously if you're doing a true one-shot that you're never gonna touch again, it won't be worth it (that being said in my experience, such situations are pretty rare and most one-shots end up being way more than what they where supposed to in the first place).

Re:Sayonara, C++, and Hello to Rust by berkut on Tuesday December 07, 2021 @04:06AM (#29470425)
You can argue that (and I'd generally agree in most cases above a certain complexity size) about dynamic vs any static language though?
Re:Sayonara, C++, and Hello to Rust by littlestymaar on Tuesday December 07, 2021 @04:49AM (#29470699)
Yes, it's exactly the static vs dynamic trade off, but with one of the best static programming language design.

C++ is probably at the other end of the spectrum since it's too easy to accidentally cause a memory error during refactoring. C# and Java are a bit in-between, the automatic memory management limits the footgun, but the type system is much less powerful than Rust's one so there still a lot of issues that can come during a refactoring. I have only very limited OCaml experience and no Haskell experience but I think you'd have most of Rust advantages with those, except the thread-safety part which is my favourite part of Rust but is also situational.

Re:Sayonara, C++, and Hello to Rust by colonwqbang on Tuesday December 07, 2021 @07:36AM (#29471736)
Haskell is quite thread safe, I suppose it is at least as good as Rust in this respect. Immutability by default and garbage collection really help to cut away the ceremony around threading.
Re:Sayonara, C++, and Hello to Rust by tialaramex on Tuesday December 07, 2021 @08:56AM (#29472461)
> I suppose it is at least as good as Rust in this respect.

(Safe) Rust has Data Race Freedom, which means you get Sequential Consistency. As I understand it, Haskell does not promise you data race freedom, so, if you mistakenly implemented a data race now you don't have Sequential Consistency and now you can't reason about non-trivial programs at all.

I think Haskell, like Java, and unlike C or C++, promises you still get a meaningful program if you have a data race, but it may be tricky to debug the mess.

Edited to expand: Suppose you're writing a program to process 16GB of data, the result will be a floating point value between 0 and 100.0 as a result of this processing. You realise it's embarrassingly parallel and so you spin up 16 threads to solve the problem in parallel on your CPU cores.

Mistakenly instead of processing 1GB per thread, you process the same 1GB on all sixteen threads, the processing involves a lot of twiddling data, and so now it's full of data races. What is the result of your buggy program?

In Rust, the program doesn't compile, or depending how you wrote it, exits immediately reporting that you can't go around processing the same data from different threads.

In C or C++ maybe it deletes all your data, more likely it simply exits instantly with no result, spins forever, or outputs something completely spurious.

In Java you might get an Exception reporting that you mustn't do this, but you might get a bogus but real looking answer like 46.2 now good luck figuring out why.

I don't know for sure about Haskell, but I think you get a similar situation to Java minus the exception.

Re:Sayonara, C++, and Hello to Rust by zozbot234 on Tuesday December 07, 2021 @02:10PM (#29476859)
> (Safe) Rust has Data Race Freedom, which means you get Sequential Consistency.

I don't think this is correct. Safe Rust includes atomics that are data-race free but need not be sequentially consistent.

Re:Sayonara, C++, and Hello to Rust by tialaramex on Tuesday December 07, 2021 @06:29PM (#29479519)
Unsurprisingly core::sync::atomic is not in fact implemented in Safe Rust.

However you can indeed use it from Safe Rust, and you can choose Orderings which do not give you Sequential Consistency if you're comfortable with that, or more likely because you are implementing an algorithm whose authors assure you this is fine (I hope they're correct). I think that's a very modest caveat to the overall promise of Sequential Consistency, but sure, it is a caveat. If you write code that explicitly asks for Ordering::Relaxed and are astonished you don't get Ordering::SeqCst I guess I apologise for confusing you.

Re:Sayonara, C++, and Hello to Rust by pjmlp on Tuesday December 07, 2021 @01:41PM (#29476476)
On the other hand, if one decides to use 16 processes to handle the data across a NUMA shared memory segment, to be able to easier scale it across the cluster, the Rust type system is of little help.
Re:Sayonara, C++, and Hello to Rust by tialaramex on Tuesday December 07, 2021 @06:52PM (#29479721)
To make this work you're going to need an (unsafe) wrapper that lets all your processes get write access to the same 1GB of shared memory. If you write a wrapper that ensures safety, you'll get back to the same place where the faulty program reports that you can't all map the same 1GB of data and twiddle it, and if you don't it's on you for introducing unchecked unsafety.
Re:Sayonara, C++, and Hello to Rust by pjmlp on Wednesday December 08, 2021 @01:34AM (#29481863)
Assuming every single OS process that has access to the NUMA segment is written in Rust and originates from exactly the same build, which is impossible to represent on the type system.
Re:Sayonara, C++, and Hello to Rust by juxtapose on Tuesday December 07, 2021 @08:01PM (#29480192)
> I think Haskell, like Java, and unlike C or C++, promises you still get a meaningful program if you have a data race

This is true (when you insist on mutable shared memory), but in Haskell the safety guarantee is in fact not so far from Rust. The Haskell community is well aware of concurrency issues very early, and they developed a dozen of composable abstractions to solve these problems. (IMHO Rust is the only one that has a truly novel solution in recent years.)

In Haskell, either you have to use `unsafe` functions to share bulk of memory or you don't share memory at all (as mutability is a pita in Haskell). On top of these unsafe functions, you can build other safe abstractions, just like Rust. These abstractions are often available as higher-order functions in libraries, making them highly composable and reusable.

E.g., the canonical memory-sharing data type is `TVar`, which must be access from the `STM` monad [1], guaranteeing both atomicity and data race freedom. (BTW, STM stands for software transactional memory if you aren't familiar with it.) A major offender is `IORef`, and fortunately Haskell provides atomic operations on `IORef`s.

GHC Haskell also offers free parallelism for all pure computations via the `Eval` monad [2].

> Mistakenly instead of processing 1GB per thread, you process the same 1GB on all sixteen threads

This is highly unlikely in Haskell. Haskell emphasizes on composable combinators, so a program that does not share memory would probably look like

    foldr accumulateFunc initValue . mapConcurrently processBatch . split batchSize $ data
(mapConcurrently here is a real function [3].) I'm sure C++, Java, and Rust have similar abstractions, but imperative languages make it too easy for programmers to do `for` loops, making it much more error-prone.

Several array libraries provide safe parallelism. The interface is similar to Rust rayon, but the way they work differs greatly. For example, in Repa [4] you can do

    computeP $ traverse inputArray newShapeFn (\getElem curIdx -> ...)
    
Here `traverse` results in a "delayed" array: the results are not immediately available, and what you are doing is just composing array operations. `computeP` forces the computation to happen _in parallel_. Since GHC is very capable of optimizing intermediate data structures away, this code can compile to very efficient machine code -- there is no mutability at all, yet you get very safe and efficient parallelism.

PS: what Haskell is really bad at so far is safe in-place updates for arbitrary data types (like custom ADTs), which is solved by Rust cleanly. GHC 9.0 added Linear Types [5] to address this problem. Haskell and Rust are both bad at safe in-place updates of arrays, vectors, etc., and are likely to be bad for a long time -- their indices are arbitrary integers, which cannot be analyzed statically at compile time. Haskell probably can solve this problem completely when it gets dependent types [6], whereas Rust is unlikely to support DT any time soon, if ever [7].

[1] https://hackage.haskell.org/package/stm-2.5.0.1/docs/Control...

[2] https://hackage.haskell.org/package/parallel-3.2.2.0/docs/Co...

[3] https://hackage.haskell.org/package/async-2.2.4/docs/Control...

[4] https://hackage.haskell.org/package/repa

[5] https://github.com/ghc-proposals/ghc-proposals/blob/master/p...

[6] https://github.com/ghc-proposals/ghc-proposals/blob/master/p...

[7] https://github.com/rust-lang/rfcs/issues/1930

Re:Sayonara, C++, and Hello to Rust by choeger on Tuesday December 07, 2021 @03:28AM (#29470231)
You shouldn't compare C++ templates to Rust polymorphism. The latter is decidable, the former is not. Compare C++ templates to Rust macros.

Btw. Who likes C++ templates? They are such a roundabout way to program the compiler that it regularly hurts me. You need to understand a set of arbitrary rules (steered by implementation of the existing compilers, not language design, it seems) to make the compiler do what you want in many lines of code. And only fellow template lovers will have a chance to understand your intention. It is admittedly powerful, but compared to any decent macro implementation (Lisp, Haskell, OCaml, Rust) it is so much worse...

Re:Sayonara, C++, and Hello to Rust by moth-fuzz on Tuesday December 07, 2021 @04:23AM (#29470540)
I mean, if you could tell from my original post, I like C++ templates. The point is not to constantly write templates in your calling code, the point is to architect a library with templates that affords flexibility and dynamism so that the calling code is easy to write, read, and reason about. Consider, for example, the sol2[0] example usage code vs the actual source code itself[1].

0. https://github.com/ThePhD/sol2 1. https://github.com/ThePhD/sol2/blob/develop/include/sol/func...

Re:Sayonara, C++, and Hello to Rust by salicideblock on Tuesday December 07, 2021 @03:37AM (#29470274)
> Btw. Who likes C++ templates?

Saying I like them is a stretch, but I prefer to write a C++ template rather than a macro (C++ or Rust) any day.

Re:Sayonara, C++, and Hello to Rust by mlindner on Friday December 10, 2021 @08:59AM (#29509886)
C++ macros are completely unlike Rust macros so lumping them together makes no sense.
Re:Sayonara, C++, and Hello to Rust by edflsafoiewq on Tuesday December 07, 2021 @03:45AM (#29470314)
Templates are not very much like macros. You cannot perform a syntactic rewrite at the point of instantiation that replaces the template use, and they are integrated into the semantics of eg. overload resolution in a way syntactic macros obviously can't be.

I don't see why decidability should render them incomparable.

Re:Sayonara, C++, and Hello to Rust by choeger on Tuesday December 07, 2021 @04:53AM (#29470725)
> they are integrated into the semantics of eg. overload resolution in a way syntactic macros obviously can't be.

Exactly, they are macros that can only be invoked via some weird implicit entry points. Proper macros could be used very much in the same way, of course, if the compiler recognizes their output correspondingly. You could, for instance, use macros to generate traits/type classes to enhance overloading. Why someone would prefer this code generation to be so implicit, I don't understand.

> I don't see why decidability should render them incomparable.

Of course you can compare them if you want to point out that particular difference. But if you claim that templates are more powerful, the obvious answer is: Sure, because they're not type safe. Templates simply aren't a type system extension, they're more like macros.

Re:Sayonara, C++, and Hello to Rust by moth-fuzz on Tuesday December 07, 2021 @11:22AM (#29474332)
C++ is a fully compiled language, and will not compile code that has type errors (barring you hack your way out of the spirit of the type system). Templates are no exception - they are type-safe once instantiated because they transform at that point into normal C++, which is type safe. Templates themselves are not types and are not meant to be, they’re meant to be templates, yes, more akin to macros, but not at any cost or defecit because of it. Frankly I don’t understand the criticism; I don’t care if I get a compiler error during template evaluation or during compilation or during linkage, a compiler error is a compiler error. Code that works works and code that doesn’t doesn’t.
Re:Sayonara, C++, and Hello to Rust by choeger on Tuesday December 07, 2021 @12:11PM (#29475213)
> Frankly I don’t understand the criticism; I don’t care if I get a compiler error during template evaluation or during compilation or during linkage, a compiler error is a compiler error.

You might want to consider a setup where the library author is not the same person as the library user. Don't you think it's important that the former sees and not the latter? That's why linker errors and compiler errors and template errors are not equal. Also, if you make an error in a template or its instantiation you might not even get an error but an infinite (i.e., maybe aborted) computation. If you don't see the difference between these things, I cannot help you.

Re:Sayonara, C++, and Hello to Rust by MauranKilom on Tuesday December 07, 2021 @05:07AM (#29470800)
> Sure, because they're not type safe. Templates simply aren't a type system extension, they're more like macros.

Could you elaborate in which way C++ templates are not type-safe? Does the upcoming concepts feature alleviate any of it?

Re:Sayonara, C++, and Hello to Rust by choeger on Tuesday December 07, 2021 @10:57AM (#29473901)
A type safe language has a deterministic, terminating algorithm that can prove the absence of certain errors in a program. For instance you cannot use integers instead of functions in well-typed a Haskell program whereas you can make this mistake in a python program.

C++ Templates are not type safe because they need to be evaluated before you can make any statement about their successful, correct application. This evaluation might not terminate. Macros habe the same issue, of course, but they explicitly give you a Turing-complete programming language so it feels more natural.

Re:Sayonara, C++, and Hello to Rust by mgaunard on Tuesday December 07, 2021 @03:52AM (#29470343)
Templates are just what their name says: recipes to generate types or functions.

They work very well and are quite pleasant to work with.

I don't really see what's so complicated about them.

Re:Sayonara, C++, and Hello to Rust by choeger on Tuesday December 07, 2021 @04:55AM (#29470743)
The complication of templates arises from the fact that you need to know the inner workings of a C++ compiler instead of just a language definition. I know that C++ is specified in a very implementation-oriented manner and I also know that many developers don't see the difference, but design ain't implementation.

If you have proper macros you don't need to know the compiler internals. You have an API that generates code with the language semantics you already know. By definition that's simpler. It's also more powerful, btw.

Re:Sayonara, C++, and Hello to Rust by mgaunard on Tuesday December 07, 2021 @05:02AM (#29470772)
That is just not true. Compiling C++ is a very complicated task, I'd guess that there are less than a hundred people in the world with the experience of implenting a standard-conforming C++ compiler, and there are a lot more developers comfortable with templates.

What exactly is your perceived problem? You need to have the template definition in order to instantiate it; that is pretty logical. Is this kind of basic common sense what you call "knowing the inner workings of the compiler"?

Re:Sayonara, C++, and Hello to Rust by choeger on Tuesday December 07, 2021 @11:02AM (#29473997)
Where shall I start? Template specialization order? Recursive template definitions for variadic arguments? Conditional template arguments? std::enable_if ?
Re:Sayonara, C++, and Hello to Rust by mgaunard on Tuesday December 07, 2021 @01:40PM (#29476461)
Template specialization works under the same rules as overloading with some restrictions (this is actually how it is formally defined).

The first step prior to overload resolution is name lookup, which discards template candidates whose declaration refers to dependent names on template parameters which wouldn't exist for that implicit instantiation.

The only order that matters is what specializations are visible at the time of the instantiation, and adding more specializations after that is more likely than not to lead to ill-formed programs (due to the ODR rule that you must satisfy), so you can assume it doesn't happen.

enable_if, recursion or whatever else you're referring to are merely trivial applications of the rules.

I don't think it's unreasonable to have to understand how overloads are selected in order to program in C++. The semantics were not designed to match an implementation but rather to make the most sense. This has nothing to do with compiler internals; if anything many compilers implemented it incorrectly for decades.

Re:Sayonara, C++, and Hello to Rust by josh11b on Tuesday December 07, 2021 @01:30PM (#29476314)
I don't think Rust polymorphism is decidable: https://sdleffler.github.io/RustTypeSystemTuringComplete/
Re:Sayonara, C++, and Hello to Rust by choeger on Thursday December 09, 2021 @07:23AM (#29496562)
Well, shit. They broke the language. I still hope that a reasonable subset of the type system remains decidable, though.
Re:Sayonara, C++, and Hello to Rust by tempodox on Tuesday December 07, 2021 @06:34AM (#29471362)
Re OCaml: For what a C++ template does, you'd use a parameterized module (“functor”), not a macro. The OCaml macro equivalent would be a PPX rewriter, which is a rather heavy-handed approach to metaprogramming.
Re:Sayonara, C++, and Hello to Rust by choeger on Tuesday December 07, 2021 @11:04AM (#29474030)
No. An ML-style functor is well-designed and can be type checked at the site of definition. If C++ had functors I would be very happy, but functors are not "zero cost" abstractions..., maybe they can be in a compiler like MLton, though.
Re:Sayonara, C++, and Hello to Rust by tempodox on Thursday December 09, 2021 @04:37AM (#29495474)
I was referring to the use case, not the implementation details. ML-style functors may not be zero-cost abstractions, but they are a way to implement polymorphism, just like C++ templates. You may be able to achieve that with a PPX rewriter, but I'd rather use a functor.
Re:Sayonara, C++, and Hello to Rust by otabdeveloper4 on Tuesday December 07, 2021 @08:47AM (#29472382)
You realize that C++ templates are just a compile-time Lisp-like language interpreter, right?
Re:Sayonara, C++, and Hello to Rust by hannofcart on Tuesday December 07, 2021 @05:14AM (#29470841)
I think you're right in the power and expressiveness of C++ templates and in how "modern C++" is a joy to code in but I suspect you have the privilege of working in a codebase that does not have much legacy C++ that you have to maintain.

Most large C++ codebases that have been running in production over decades are unfortunately a Frankenstein's monster written in a mix of old style C with classes C++ code full of malloc and free footguns, heavily OOP driven prefer-inheritance over composition era code full of virtual destructor footguns, and the modern nicer RAII std::unique/shared_ptr memory management based C++ code. And a lot of template and macro magic thrown in across each of those eras.

So you can get really productive in modern C++ pretty quickly and it can be an enjoyable experience. But to maintain a large old C++ codebase you are going to have to grok a lot of stuff. So much that I really can't find too many people who can hold it all in their head. I certainly can't.

Finally, am not sure if there was a hint of derision about Rust being primarily used for web services but I think it's a great thing if the next generation of web services will be developed in something as efficient as Rust rather than more resource hungry counterparts like Python or JS. Good for the balance sheet. Good for the environment. Good for the web devs who get expressive easy to understand code with a lot of guard rails built in.

Re:Sayonara, C++, and Hello to Rust by pjmlp on Tuesday December 07, 2021 @05:39AM (#29471023)
Until Rust gets the compilation story sorted out, what you save on the computer center gets spent in developer workstations.
Re:Sayonara, C++, and Hello to Rust by pas on Tuesday December 07, 2021 @06:03AM (#29471184)
Do you mean compile time?

> what you save on the computer center gets spent in developer workstations.

um, that cat is already out of the bag. unless you work on mobile or web frontend most backends are big, ugly, have a lot of dependencies, running them needs ALL the RAM anyway :)

Re:Sayonara, C++, and Hello to Rust by rapsey on Tuesday December 07, 2021 @07:03AM (#29471521)
C++ is slow as well.
Re:Sayonara, C++, and Hello to Rust by pjmlp on Tuesday December 07, 2021 @07:28AM (#29471684)
True, except the tiny detail that the usage of binary libraries is common in C++ culture.

Building from scratch Gentoo style not so much.

I seldom compile third party dependencies.

Re:Sayonara, C++, and Hello to Rust by rapsey on Tuesday December 07, 2021 @08:05AM (#29472000)
You don't recompile those every time.
Re:Sayonara, C++, and Hello to Rust by pjmlp on Tuesday December 07, 2021 @10:33AM (#29473506)
Indeed, only every time I checkout something from github, update cargo dependencies or switch worspace.
Re:Sayonara, C++, and Hello to Rust by timw4mail on Tuesday December 07, 2021 @09:06AM (#29472556)
I would not use Gentoo if I had to compile the world to, say, update PHP.
Re:Sayonara, C++, and Hello to Rust by timw4mail on Tuesday December 07, 2021 @09:06AM (#29472557)
I would not use Gentoo if I had to compile the world to update PHP.
Re:Sayonara, C++, and Hello to Rust by fhd2 on Tuesday December 07, 2021 @03:28AM (#29470228)
> it always baffles me that most people seem to be using Rust to write ... web services?

I think it's the whole x-rewritten-in-rust-now-faster hype train. Although those limitations you mention, what's easy and hard to do, do have a more or less automatic impact on performance, fueling that train.

I think of it like this:

Programmer level 1: Makes something work in a higher level language

Programmer level 2: Makes something work in a lower level language

Programmer level 3: Makes something fast in a lower level language (in most cases just a few simple optimisations on top of #2)

Programmer level 4: Makes something fast in a higher level language (object pooling, understanding how the GC and VM works to avoid GC pauses at the wrong time and all that)

So with Rust (or yes, C++, though it's a bit more of a foot gun IMHO), level 2/3 people can write code that's efficient. What I think is somewhat unique about Rust is that it keeps programmers from making lots of common mistakes working with lower level languages - like it's designed for level 1/2 folks.

Re:Sayonara, C++, and Hello to Rust by natded on Tuesday December 07, 2021 @04:36AM (#29470605)
Writing web-services in Rust is easy and I don't understand the OP's confusion about it. Web-services are highly concurrent and benefit from async, both of which are areas where Rust excels at - and Rust looks like JavaScript most of the time when writing these so it is pretty clear what to do here if you need scale and performance.

Then there's the plethora of other advantages you get from its great type system and inference etc.

Re:Sayonara, C++, and Hello to Rust by flooow on Tuesday December 07, 2021 @05:43AM (#29471053)
I agree completely, it's a very strange objection - Rust is one of the very best languages for web servers. I wish I could use it at work instead of Django.

I think some C/C++ devs take a perverse pride in their language not being suitable to write web servers, because webdev is not real programming anyway (excepting the 'systemsy' parts like nginx).

Well, Rust is great at systems, great at web servers, pretty good at one-off script, even pretty usable for frontend. Every time I hear someone pigeonhole it as just a systems language, I think - you're missing out.

Re:Sayonara, C++, and Hello to Rust by moth-fuzz on Tuesday December 07, 2021 @11:28AM (#29474428)
It definitely wasn’t meant as a criticism - I think Rust is uniquely suited to writing web applications as well. I just find it funny that writing web apps is easy enough in Rust that it almost minimizes its opinionatedness, since you don’t really need to worry about difficult lifetimes or ownership when it’s JSON in, JSON out, and the database is just an externality, and you don’t have hard performance requirements despite Rust being made for performance. It seems like an impedance mismatch but it works out.
Re:Sayonara, C++, and Hello to Rust by imron on Tuesday December 07, 2021 @03:30AM (#29470235)
> compared to the completeness and expressiveness C++ templates.

The error messages in particular - so very expressive.

Re:Sayonara, C++, and Hello to Rust by ReleaseCandidat on Tuesday December 07, 2021 @04:13AM (#29470477)
> the one place I find Rust truly a joy to write is OS dev, and similar low-level projects

That's what Rust actually is useful for. And what C++ should have been used for. It has grown in the abomination it is, because the people (well, yes, me too ;) abused it as language for 'everything'. I do actually fear that the same will happen to Rust.

Btw. most people use Rust to write web services because most people actually write web services ;)

Re:Sayonara, C++, and Hello to Rust by kitd on Tuesday December 07, 2021 @04:04AM (#29470413)
I deeply miss C++ templates in most other languages, excepting fully dynamic languages, and D.

Have you tried Zig? Comptime templating it one of its selling points.

https://ziglang.org/documentation/0.8.1/#comptime

Re:Sayonara, C++, and Hello to Rust by moth-fuzz on Tuesday December 07, 2021 @04:19AM (#29470513)
I loooove Zig - my only gripe with Zig is that it feels somewhat arcane at times, and the documentation was borderline absent last I checked. I follow Andrew Kelley on twitter and am fascinated by his exploits. Crystal has a similar macro system that I enjoy, from what I've used of it. Rust macros are similarly satisfying in what you can accomplish with them, but the implementation is so immeasurably difficult, it's almost never worth it to bother, from a general design standpoint.

As far as C replacements go, Zig has my vote for the most approachable but also the most well thought-out. I couldn't sing its praises enough.

Re:Sayonara, C++, and Hello to Rust by lenkite on Tuesday December 07, 2021 @08:43AM (#29472341)
Would love Zig if it had RAII.
Re:Sayonara, C++, and Hello to Rust by AndyKelley on Friday December 10, 2021 @12:45AM (#29506814)
RAII works fine in Zig
Re:Sayonara, C++, and Hello to Rust by lenkite on Friday December 10, 2021 @04:11AM (#29507959)
Re:Sayonara, C++, and Hello to Rust by AndyKelley on Friday December 10, 2021 @04:17AM (#29507992)
show me a raii code example in C++ and I'll show you the zig equivalent
Re:Sayonara, C++, and Hello to Rust by lenkite on Friday December 10, 2021 @10:41AM (#29511169)
Note that in `good` below, one can have many such lock guards: lk1, lk2, etc without any explicit lock release expression in the body of the function.

    std::mutex m;
    void good()
    {
    std::lock_guard<std::mutex> lk(m); // RAII class: mutex acquisition is initialization
    f();                               // if f() throws an exception, the mutex is released
    if(!everything_ok()) return;       // early return, the mutex is released
    }                                      // if good() returns normally, the mutex is released
Re:Sayonara, C++, and Hello to Rust by AndyKelley on Friday December 10, 2021 @06:16PM (#29516648)

    var m: std.Thread.Mutex = .{};

    fn good() !void {
        m.lock();
        defer m.unlock();

        try f();
        if (!everything_ok()) return;
    }
It's still the RAII pattern. The explicit call to deinitialization does not disqualify it. I can overcomplicate it with a LockGuard class too:

    const LockGuard = struct {
        mutex: *std.Thread.Mutex,

        fn init(mutex: *std.Thread.Mutex) !LockGuard {
            mutex.lock();
            return .{
                .mutex = mutex,
            };
        }

        fn deinit(lg: *LockGuard) void {
            lg.mutex.unlock();
        }
    };

    fn good() !void {
        var lk = try LockGuard.init(&m);
        defer lk.deinit();

        try f();
        if (!everything_ok()) return;
    }
In this case it demonstrates an improvement over C++, which is what happens if an error happens during a constructor. In C++ throwing an exception in a constructor is essentially UB; in zig it's a regular function call just like anything else.
Re:Sayonara, C++, and Hello to Rust by lenkite on Sunday December 12, 2021 @01:42PM (#29532305)
Thanks for the informative snippet, but explicit expressions involving defer is not what RAII means. In RAII resource is automatically tied to object lifetime.

https://en.wikipedia.org/wiki/Resource_acquisition_is_initia...

Regarding errors in a C++ constructor: https://isocpp.org/wiki/faq/exceptions#ctors-can-throw

Re:Sayonara, C++, and Hello to Rust by forrestthewoods on Tuesday December 07, 2021 @04:46AM (#29470672)
templates are definitely more powerful than Rust generics. And "enterprise" Rust code can easily become so infested with traits it's as unreadable as C++ template spaghetti. STL is a special level of unreadable. But "enterprise Rust" might be worse than "enterprise C++" at times.

I am curious, what are some actual things you have done with templates that you couldn't do with generics? I'm pretty firmly in the camp of "most of C++'s strengths over C are bad". So I'm struggling.

I'd maybe possibly say C++ templates are better for math? Which is true. Rust generics are not great for complex math. However eigen is nightmare monster that absolutely ruins my compile times so I'm not sure whether this counts as a win or loss for templates. Maybe a bit of both.

Rust is still duper bad at UI. Even if there are some imgui-like Rust libraries doing cool things. I've never seen a C++ GUI library that didn't suck horribly. But at least it was possible.

Re:Sayonara, C++, and Hello to Rust by pjmlp on Tuesday December 07, 2021 @05:41AM (#29471036)
Traits, macros and crate features actually.
Re:Sayonara, C++, and Hello to Rust by natded on Tuesday December 07, 2021 @04:34AM (#29470597)
Subtyping is mostly a bad idea so losing that doesn't matter.