News for hackers. Stuff that matters.
Re:Switching from C++ to Rust by the-smug-one on Monday March 13, 2023 @08:50PM (#35145697)
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.

Re:Switching from C++ to Rust by zabzonk on Monday March 13, 2023 @08:58PM (#35145792)
and you would use it ... how?
Re:Switching from C++ to Rust by dcow on Tuesday March 14, 2023 @01:33AM (#35147773)
It's not exactly Java, but:

    Cons(1, Cons(2, Cons(3, Nil)))
Re:Switching from C++ to Rust by kaba0 on Tuesday March 14, 2023 @05:42AM (#35149156)
Actually, Java does have sum types nowadays, and you can write a recursive list implementation like “new Cons(2, new Cons(3, Nil.nil()))”.

Here is a definition for a Maybe type, converting it to a List is left as an exercise to the reader :) : https://news.ycombinator.com/item?id=35133670

Re:Switching from C++ to Rust by dcow on Tuesday March 14, 2023 @11:08AM (#35152708)
I just meant because I was eliding the `new`s.
Re:Switching from C++ to Rust by the-smug-one on Tuesday March 14, 2023 @04:55AM (#35148859)
I've showed you an alternative encoding, maybe get to work and learn something yourself?