2 ms·
"From the third slide: we can see two different function definition syntaxes, a 'normal' style and a 'ruby' style. I really like the way of JavaScript, CoffeeSc
by pcwalton 15y ago
"From the third slide: we can see two different function definition syntaxes, a 'normal' style and a 'ruby' style. I really like the way of JavaScript, CoffeeScript and OCaml that have a single unified function syntax."
Functions declared at top-level with the |fn| keyword can be mutually recursive and don't close over anything by default. Blocks, however, are unique in that they can close over stack variables (we statically ensure that they can't escape) and can't be mutually recursive. At the moment, blocks can only appear in function argument position. So, although both |fn| functions and blocks are functions in some sense, they have different enough semantics that we felt that different syntax helped to convey the distinction.
"Also on this slide: why have a special syntax for importing submodules from modules/crates? Again, CoffeeScript excels here, an import is simply a variable declaration:"
Well, in CoffeeScript, imports are actually statements that are evaluated at runtime. In Rust, they're compile-time constructs. Furthermore, using a separate syntax gives us more power — you can say |import *|, you can say things like |import std::{hashmap::map, vec}|, etc.
"Furthermore, it has always bothered me why generic types use <> brackets. I can understand it in Java and C# (where types are declared before values/functions), but in Rust, it's really unnecessary to introduce the 4th type of brackets into the language (apparently, they used [], but latter changed it to <>... I would really like to know why.)"
Because you can use generic type instantiation in value position as well (e.g. none::<int>), and [] was preventing us from using [] for array indexing (previously, you wrote v.(0) for v[0]). I also thought it'd be more familiar for C++, C#, and Java programmers.