rust_cheat_sheet
rust_cheat_sheet
rs/2018-12-01/
Contains clickable links to The Book BK, Rust by Example EX, Std Docs STD, Nomicon NOM, Reference REF. Furthermore, entries are marked
as largely deprecated 🗑 , have a minimum edition '18. or are bad ⚡ .
Data Structures
Define data types and memory locations, and use them.
Example Explanation
enum E { A, C {} } Define variants of enum; can be unit- A , tuple- B () and struct-like C{} .
enum E { A = 1 } If variants are only unit-like, allow discriminants values, e.g., for FFI.
static X: T = x; Global variable BK EX REF with 'static lifetime, single memory location.
Example Explanation
S (x) Create struct S (T) or use 'ed enum E::S () with field .0 set to x .
1 of 6 12/2/18, 11:15
Rust Language Cheat Sheet https://cheats.rs/2018-12-01/
Example Explanation
a..b Right-exclusive range REF creation, also seen as .. , a.. , ..b .
Example Explanation
&t Immutable borrow BK EX STD (i.e., an actual "pointer to t", like 0x1234 ).
&T Immutable reference BK STD NOM REF (i.e., safe pointer type holding any &t ).
Sigil Explanation
fn() -> T Function pointers, BK STD REF don't confuse with traits Fn , FnOnce , FnMut .
return || true Closures may sometimes look like logical ORs (here: return a closure).
x.f() Call member function, requires f takes self , &self , ... as first argument.
X::f(x) Same as x.f() . Unless impl Copy for X {} , f can only be called once.
unsafe {} Marker for unsafe code. BK EX NOM REF that will probably segfa#%$@.
2 of 6 12/2/18, 11:15
Rust Language Cheat Sheet https://cheats.rs/2018-12-01/
Control Flow
Control execution within a function.
Sigil Explanation
loop {} Loop infinitely REF until break . Can yield value with break x .
'label: loop {} Loop label EX REF, useful for flow control in nested loops.
break x Same, but make x value of the loop expression (only in actual loop ).
break 'label Exit not only this loop, but the enclosing one marked with 'label .
continue Continue expression REF to the next loop iteration of this loop.
continue 'label Same, but instead of enclosing loop marked with 'label .
return x Early return from function. More idiomatic way is to end with expression.
Organizing Code
Segment projects into smaller units and minimize dependencies.
Sigil Explanation
use a::b; Use EX REF b directly in this scope without requiring a anymore.
pub use a::b; Bring a::b into scope and reexport from here.
extern crate x ; Declare dependency on external crate BK EX REF 🗑 ; just use x::f in '18.
Sigil Explanation
Self Type alias for implementing type REF, e.g. fn new() -> Self .
&mut self Same, but mutably borrowed, same as f(self: &mut Self)
self: Box<Self> Arbitrary self type, add methods to smart pointers ( my_box.f_of_self() ).
3 of 6 12/2/18, 11:15
Rust Language Cheat Sheet https://cheats.rs/2018-12-01/
Sigil Explanation
x as u32 Primitive cast EX REF
, may truncate and be a bit surprising. NOM
Code Generation
Constructs expanded before the actual compilation happens.
Example Explanation
m!() Macro BK STD REF invocation, also m!{} , m![] (depending on macro).
$(x)<<+ In fact separators other than , are also accepted. Here: << .
Pattern Matching
These constructs are found in match or let expressions.
Example Explanation
D => {} Match anything, bind D ; ⚡ possibly false friend of E::D if D not in use .
[a, 0] => {} Match array with any value for a and 0 for second.
(a, 0) => {} Match tuple with any value for a and 0 for second.
Example Explanation
let Some(x) = Some(5) Notably, let also pattern matches similar to the table above.
let Some(x) = get() ⚡ Will not work if pattern can be 'refuted', use if let instead.
if let Some(x) = get() Branch if pattern can actually be assigned REF (e.g., enum variant).
fn f(S { x }: S) Function parameters also work like let , here x bound to s.x of f(s) .
Example Explanation
4 of 6 12/2/18, 11:15
Rust Language Cheat Sheet https://cheats.rs/2018-12-01/
Example Explanation
S<T> A generic BK EX type with a type parameter ( T is placeholder name).
S<T: R> Type short hand trait bound BK EX specification ( R must be trait).
T: 'a Type lifetime bound EX, all references in T must outlive 'a .
'b: 'a Lifetime 'b must live at least as long as (i.e., outlives) 'a bound.
S<T> where T: R Same as S<T: R> but easier for longer bounds.
fn f() -> impl T Existential types BK, returns an unknown-to-caller S that impl T .
fn f(x: &impl T) Trait bound,"impl traits" BK, somewhat similar to fn f<S:T>(x: &S) .
fn f(x: &dyn T) Marker for dynamic dispatch BK REF, f will not be monomorphized.
Example Explanation
r#"..."# , etc. Raw string literal, but can also contain " .
b"..." Byte string literal REF; constructs ASCII [u8] , not a string.
br"..." , br#"..."# , etc. Raw byte string literal, combination of the above.
Comments
No comment.
Example Explanation
// Line comment.
Miscellaneous
These sigils did not fit any other category but are good to know nonetheless.
Example Explanation
5 of 6 12/2/18, 11:15
Rust Language Cheat Sheet https://cheats.rs/2018-12-01/
Example Explanation
_x Variable binding explicitly marked as unused.
1u8 Type specifier for numeric literals EX REF (also i8 , u16 , ...).
Common Operators
Rust supports all common operators you would expect to find in a language ( + , * , % , = , == ...). Since they behave no differently in Rust we do
not list them here. For some of them Rust also support operator overloading. STD
Invisible Sugar
If something works that "shouldn't work now that you think about it", it might be due to one of these.
Name Description
Deref NOM Deref x: T until *x , **x , ... compatible with some target S .
Lifetime Elision BK NOM REF Automatically annotate f(x: &T) to f(x: &'a T) .
6 of 6 12/2/18, 11:15