3 ms·
enums + match expressions + tagged unions are the secret sauce of Rust.
by ModernMech 1y ago
enums + match expressions + tagged unions are the secret sauce of Rust.
- mixmastamyk 1y agoMaybe I need to read it again, but I remember the Rust book saying… you can use enums like C, but what if instead you used them in this more concise way? (Match on members, without the container.) Ok, was able to proceed but don’t feel like I understand what they really are.
- pjmlp 1y agoLike this code snippet? (* Expressions *) type Exp = UnMinus of Exp | Plus of Exp * Exp | Minus of Exp * Exp | Times of Exp * Exp | Divides of Exp * Exp | Power of Exp * Exp | Real of float | Var of string | FunCall of string * Exp | Fix of string * Exp ;; let rec tokenizer s = let (ch, chs) = split s in match ch with ' ' -> tokenizer chs | '(' -> LParTk:: (tokenizer chs) | ')' -> RParTk:: (tokenizer chs) | '+' -> PlusTk::(tokenizer chs) | '-' -> MinusTk::(tokenizer chs) | '*' -> TimesTk::(tokenizer chs) | '^' -> PowerTk::(tokenizer chs) | '/' -> DividesTk::(tokenizer chs) | '=' -> AssignTk::(tokenizer chs) | ch when (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') -> let (id_str, chs) = get_id_str s in (Keyword_or_Id id_str)::(tokenizer chs) | ch when (ch >= '0' && ch <= '9') -> let (fl_str, chs) = get_float_str s in (RealTk (float (fl_str)))::(tokenizer chs) | '$' -> if chs = "" then [] else raise (SyntaxError ("")) | _ -> raise (SyntaxError (SyntErr ())) ;; Hint, this isn't Rust.
- lmm 1y agoYes, the secret of Rust is that it offers both a) some important but slightly subtle language features from the late '70s that were sadly not present in Algol '52 and are therefore missing from popular lineages b) a couple of party tricks, in particular the ability to outperform C on silly microbenchmarks; b) is what leads people to adopt it and a) is what makes it non-awful to program in. Yes it's a damning indictment of programming culture than people did not adopt pre-Rust ML-family languages, but it could be worse, they could be not adopting Rust either.
- hollerith 1y ago>important but slightly subtle language features from the late '70s Programming-language researchers didn't start investigating linear (or affine) types till 1989. Without the constraint that vectors, boxes, strings, etc, are linear, Rust cannot deliver its memory-safety guarantees (unless Rust were radically changed to rely on a garbage collecting runtime). >it's a damning indictment of programming culture than people did not adopt pre-Rust ML-family languages In pre-Rust ML-family languages, it is harder to reason about CPU usage, memory usage and memory locality than it is in languages like C and Rust. One reason for that is the need in pre-Rust ML-family langs for a garbage collector. In summary, there are good reasons ML, Haskell, etc, never got as popular as Rust.
- lmm 1y ago> Programming-language researchers didn't start investigating linear (or affine) types till 1989. Sure, but as ModernMech said, the vast majority of Rust's benefits come from having sum types and pattern matching. > In pre-Rust ML-family languages, it is harder to reason about CPU usage, memory usage and memory locality than it is in languages like C and Rust. Marginally harder for the first two and significantly harder for the last, sure. None of which is enough to matter in the overwhelming majority of cases where Rust is seeing use.
- gf000 1y ago> Sure, but as ModernMech said, the vast majority of Rust's benefits come from having sum types and pattern matching. Doubt. There were lots of languages giving you just that, and they never had this amount of hype. See Scala, OCaml, Haskell, etc. Rust has one unique ability, and many shared by other languages. It's quite clearly popular for the former (though languages are a packages, so of course it's a well put together language all around).
- ModernMech 1y agoScala, OCaml, and Haskell all approach programming from a functional-first perspective. What no other language did before Rust was to bring those features to such a well-designed imperative core. And while this was necessary to Rust's success, I don't think it was sufficient, insofar as it also needed a good deal of corporate backing, a great and welcoming community, and luck to be at the right place at the right time. Haskell never tried to be more than a academic language targeting researchers. OCaml never had a big community or corporate backing. Scala never really had a niche; the most salient reason to use it is if you're already in the Java ecosystem and you want to write functional code. The value propositions for each are very different, so these language didn't receive the same reaction as Rust despite offering similar features.