
Rust Language Cheat Sheet - ivank
https://cheats.rs/
======
spectramax
I love the design of this site. It is compact, dense and to the point. I think
we need to port cheatsheets for most popular languages in this format. There
is also Learn Y in X minutes[1] which I often refer to but a cheatsheet like
this where everything is neatly organized would be a huge help. I jump between
3-4 languages everyday and it is a pain in the ass to Google syntax, frankly
embarrassing that I have to.

[1] [https://learnxinyminutes.com/](https://learnxinyminutes.com/)

~~~
ExtremisAndy
Oh, my! I have spend countless hours on the web looking for (and successfully
finding, of course) cheat sheets for all sorts of languages, but somehow I had
never come across this site. This is extremely well done. Thank
you,spectramax, for basically revolutionizing my life this morning! :)

~~~
andrewl-hn
Surprisingly,
[https://learnxinyminutes.com/docs/bash/](https://learnxinyminutes.com/docs/bash/)
is the best resource out there to learn shell scripting. This page basically
turned me from a novice who could only write two commands with | or && between
them to a competent shell programmer. My shell scripts have functions,
descriptive variables, error handling, good formatting, etc.

~~~
dzamlo
I find shellcheck to be really great to go to the step beyound. It find a lot
of things that make most shell script brittle and teach you some best
practice.

Add shfmt on top and you are good to go.

------
cswiercz
Great quick reference, but a wasted opportunity to call the site “cheate.rs”.

------
nmca
The explanation of lifetimes linked here is really great - I think this goes a
little further than just a cheat sheet. Seems to be more like excellent Clif
notes.

~~~
alexbanks
Lifetimes are about where I bowed out of Rust. I haven't worked with C++
before, or really ever thought about borrowing or lifetimes, so it never
really clicked in my head. What's worse is the Rust community's desire to
remind you to "Just read the book". Maybe this would be a worthwhile read to
get back into it?

~~~
Myrmornis
The Book by Steve Klabnik looks fantastic for newer programmers, but as an
experienced programmer I did find it difficult to get into. On the other hand,
the O’Reilly book by Jim Blandy and Jason Orendorff is just amazing for
experienced programmers (and would be unsuitable for new programmers). It’s
the best programming language book I’ve ever read. It’s great that both books
exist, filling those two niches.

------
xrio
Author here. Thank you all for your comments, issues and pull requests. You
are amazing!

------
RedHotRiplets
Any plans on adding Box, Rc, RefCell, and the differences thereof? That's the
thing that trips me up the most right now.

~~~
steveklabnik
To give you a quick summary:

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.

------
spencerwgreene
If you had an equivalent Cheat Sheet for other programming languages (no
programs or opinions), which language would look best?

~~~
masklinn
Smalltalk. Or Forth.

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...](https://miro.medium.com/max/1000/1*Aa0AEoUgjZov8yrgdplsIg.png)

(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).

~~~
excessive
Scheme has to fall somewhere between Smalltalk and Forth for quantity of
syntax too. To me, these are the prettiest languages.

~~~
masklinn
I didn't mention lisps because I'm never sure how to classify "built-in"
special forms: should they be considered normal names and thus omitted from a
syntactic overview or should they be considered keywords and included?

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.

~~~
excessive
Yes, and I'm not sure the R*RS standards even specify which special forms are
fundamental vs implemented by macros. However, Smalltalk has some keywords
too. In any case, it could be a concise cheat sheet.

~~~
masklinn
> However, Smalltalk has some keywords too.

Indeed but they're all on the postcard above (true, false, nil, super and
self).

Smalltalk's closest thing to built-in special forms would be primitives but
they have a generic "shape" and they're pretty much always used as
implementation details for the "normal" messages which system users interact
with.

------
vvanders
Pretty solid list, only thing I saw missing was binary representation for
literals("0b0000_1000") which is really handy when working with micros or
other things that use a lot of flags or bits.

------
ridiculous_fish
As a Rust noob, what's the rhyme or reason for semicolons?

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?

~~~
kchamplewski
They appear to be similar to the way most other languages like C and Java use
semicolons, where a semicolon isn't required after a code block (generally
denoted by squiggly brackets), but is required at the end of most (all?) other
statements.

~~~
Sharlin
The most confusing thing ever is, of course, the fact that C struct/union and
C++ class declarations _do_ require a semicolon after the closing curly
bracket. This is because type declarations are permitted to occur inline in
certain contexts, notably as the type specifier in a variable declaration.

------
fold_left
Ralf Biedert, thanks so much for this! I've been learning Rust via the Rust
Book for the past few months and this will help so, so much - fantastic work.

~~~
quietbritishjim
I know everyone says how good Rust's documentation is but I tried learning
Rust with The Rust Book and found it frustrating: too long and wordy on the
easy bits and without useful insight on the hard bits. I ended up buying
Programming Rust by Jim Blandy [0] on a recommendation from HN comments and
found it must easier to learn from. I'm already familiar with several other
languages, which that book assumes, so your milage may vary.

[0] [https://www.amazon.co.uk/Programming-Rust-Jim-
Blandy/dp/1491...](https://www.amazon.co.uk/Programming-Rust-Jim-
Blandy/dp/1491927283)

~~~
indemnity
You’re not alone. I found the Rust book very hard going, whereas Programming
Rust was just the right amount of rigor and interesting examples for me.

~~~
steveklabnik
This is why I'm glad we have both; I don't think one resource could ever
satisfy everyone. Pumped to see Rust in Action getting closer to publication
too!

~~~
AlchemistCamp
I just bought Rust in Action a couple weeks ago (after having gone up to
chapter 11 in the No Starch book). It's definitely good to have both. I'll
probably do 1/2 the new book, finish the 1st one and then finish the new one.

I did pretty much the same thing with Dave Thomas's Programming Elixir book,
bailing halfway through, using a 2nd resource to consolidate, and then
returning to finish it.

I like learning Rust so far. The main thing I wonder is what kinds of projects
are good to do while learning. I went from Node to Rails to Phoenix and
normally I just rewrite web apps I've previously created in the language I'm
learning. With Rust, it feels like I should build something different,
something lower-level like a tool, an emulator or a 2D game.

~~~
jasondclinton
> I like learning Rust so far. The main thing I wonder is what kinds of
> projects are good to do while learning. I went from Node to Rails to Phoenix
> and normally I just rewrite web apps I've previously created in the language
> I'm learning. With Rust, it feels like I should build something different,
> something lower-level like a tool, an emulator or a 2D game.

If your experience is web apps, I would stick with that; Rust has a great web
app story in the form of at least two mature frameworks.

------
saagarjha
Really enjoyed the fact that there was an "Idiomatic Rust" section.

------
ohazi
> unsafe {}

> If you need to crash your code in production

heh :-)

------
legulere
The first ' in '' is red and the second one is black. Does the syntax
highlighter maybe work on ucs-16 characters wrongly?

------
rrss
Anyone remember when rust had ~ and @?

------
known
[https://archive.is/utKnz](https://archive.is/utKnz)

------
ammaskartik
This is great. Nice work!

------
wuxb
Just to be funny, it's a Cheatbook.

------
shmerl
Very nice!

