444 points · by ivank · 7y ago · 63 comments
Add shfmt on top and you are good to go.
Box is (mostly) used in two specific circumstances: when you want to have ownership of something that lives longer than a single function, and to create heterogeneous lists of things (a "trait object").
Rc is used when you need multiple ownership of something, and you're not going to be sharing those things between threads.
RefCell is a type that moves the borrow checker to runtime; it's useful when you're trying to mutate something, and Rust's static checking can't tell that it's okay. If you mess it up, you'll get a panic. This often happens in combination with Rc, since Rc makes something have multiple owners.
This cheatsheet is syntactic, both have very little syntax. You can famously exercise all of Smalltalk's syntax on half a postcard: https://miro.medium.com/max/1000/1*Aa0AEoUgjZov8yrgdplsIg.pn...
(the example doesn't feature primitives calls, not because they don't fit — a primitive call would be <primitive: xxx> — but because their use case is extremely specific to implementing a system).
On the one hand they usually have non-normal evaluation and custom mini-languages, on the other so do most macros otherwise you'd just use a function.
But yeah, these are the sort of languages I think of as "simple": small languages which reach "smallness" by giving access to a limited set of extremely powerful constructs.
I see the example:
struct S {}
but here the semicolon is required: type T = S;
or you get weird errors. It's hard to predict when a semicolon is required at least for type declarations, is there an underlying principle?So `type` needs no more blocks and ends with `;`, while `struct S { ... }` doesn't need `;`. Completing an example, there is another form of `struct` that looks like `struct S(u32);` which do need `;` by the aforementioned logic.
(The same logic applies to statements. You may think of an apparent exception like `{ println!("42"); 54 }`, but here `54` is an expression and you can't put a non-expression statement like `let x = 42` there without a semicolon.)
e.g. `let x = match y { ... };`
[0] https://www.amazon.co.uk/Programming-Rust-Jim-Blandy/dp/1491...
> [0] https://www.amazon.co.uk/Programming-Rust-Jim-Blandy/dp/1491....
Thanks for tip, the Rust Book is working well for me so far but it's good to know of other good options.
I don't know about everyone else, but when I say that I'm mostly referring to the auto generated documentation you get from Cargo. One of Rust's greatest strengths is having uniform documentation for every project.
> If you need to crash your code in production
heh :-)