Rust Language Cheat Sheet
22.03.2020

Contains clickable links to The Book BK, Rust by Example EX, Std Docs STD, Nomicon NOM, Reference REF. Other symbols used: largely deprecated 🗑️, has a minimum edition '18, is work in progress 🚧, or bad 🛑.

Hello, Rust!

If you have never seen Rust before, or if you want to try the things below:

fn main() {
    println!("Hello, world!");
}

Data Structures

Data types and memory locations defined via keywords.

ExampleExplanation
struct S {}Define a struct BK EX STD REF with named fields.
     struct S { x: T }Define struct with named field x of type T.
     struct S ​(T);Define "tupled" struct with numbered field .0 of type T.
     struct S;Define zero sized NOM unit struct. Occupies no space, optimized away.
enum E {}Define an enum BK EX REF , c. algebraic data types, tagged unions.
     enum E { A, B(), 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 discriminant values, e.g., for FFI.
union U {}Unsafe C-like union REF for FFI compatibility.
static X: T = T();Global variable BK EX REF with 'static lifetime, single memory location.
const X: T = T();Defines constant BK EX REF. Copied into a temporary when used.
let x: T;Allocate T bytes on stack 1 bound as x. Assignable once, not mutable.
let mut x: T;Like let, but allow for mutability and mutable borrow. 2
     x = y;Moves y to x, invalidating y if T is not Copy, and copying y otherwise.

1 They live on the stack for synchronous code. For async code these variables become part of the async's state machine which may ultimately reside on the heap.
2 Note that technically mutable and immutable are a bit of a misnomer. Even if you have an immutable binding or shared reference, it might contain a Cell, which supports so called interior mutability.

 

Creating and accessing data structures; and some more sigilic types.

ExampleExplanation
S { x: y }Create struct S {} or use'ed enum E::S {} with field x set to y.
S { x }Same, but use local variable x for field x.
S { ..s }Fill remaining fields from s, esp. useful with Default.
S { 0: x }Like S ​(x) below, but set field .0 with struct syntax.
S​ (x)Create struct S ​(T) or use'ed enum E::S​ () with field .0 set to x.
SIf S is unit struct S; or use'ed enum E::S create value of S.
E::C { x: y }Create enum variant C. Other methods above also work.
()Empty tuple, both literal and type, aka unit. STD
(x)Parenthesized expression.
(x,)Single-element tuple expression. EX STD REF
(S,)Single-element tuple type.
[S]Array type of unspecified length, i.e., slice. STD EX REF Can't live on stack. *
[S; n]Array type EX STD of fixed length n holding elements of type S.
[x; n]Array instance with n copies of x. REF
[x, y]Array instance with given elements x and y.
x[0]Collection indexing. Overloadable Index, IndexMut
x[..]Collection slice-like indexing via RangeFull, c. slices.
x[a..]Collection slice-like indexing via RangeFrom.
x[..b]Collection slice-like indexing RangeTo.
x[a..b]Collection slice-like indexing via Range.
a..bRight-exclusive range REF creation, also seen as ..b.
a..=bInclusive range creation, also seen as ..=b.
s.xNamed field access, REF might try to Deref if x not part of type S.
s.0Numbered field access, used for tuple types S ​(T).

* For now, see tracking issue and corresponding RFC 1909.

References & Pointers

Granting access to un-owned memory. Also see section on Generics & Constraints.

ExampleExplanation
&SShared reference BK STD NOM REF (space for holding any &s).
     &[S]Special slice reference that contains (address, length).
     &strSpecial string reference that contains (address, length).
     &mut SExclusive reference to allow mutability (also &mut [S], &mut dyn S, ...)
     &dyn TSpecial trait object BK reference that contains (address, vtable).
*const SImmutable raw pointer type BK STD REF w/o memory safety.
*mut SMutable raw pointer type w/o memory safety.
&sShared borrow BK EX STD (e.g., address, len, vtable, ... of this s, like 0x1234).
&mut sExclusive borrow that allows mutability. EX
ref sBind by reference. EX 🗑️
*rDereference BK STD NOM a reference r to access what it points to.
     *r = s;If r is a mutable reference, move or copy s to target memory.
     s = *r;Make s a copy of whatever r references, if that is Copy.
     s = *my_box;Special case for Box that can also move out Box'ed content if it isn't Copy.
'aA lifetime parameter, BK EX NOM REF, duration of a flow in static analysis.
     &'a SOnly accepts a s with an address that lives 'a or longer.
     &'a mut SSame, but allow content of address to be changed.
     struct S<'a> {}Signals S will contain address with lifetime 'a. Creator of S decides 'a.
     trait T<'a> {}Signals a S which impl T for S might contain address.
     fn f<'a>(t: &'a T)Same, for function. Caller decides 'a.
'staticSpecial lifetime lasting the entire program execution.

Functions & Behavior

Define units of code and their abstractions.

ExampleExplanation
trait T {}Define a trait; BK EX REF common behavior others can implement.
trait T : R {}T is subtrait of supertrait REF R. Any S must impl R before it can impl T.
impl S {}Implementation REF of functionality for a type S, e.g., methods.
impl T for S {}Implement trait T for type S.
impl !T for S {}Disable an automatically derived auto trait NOM REF.
fn f() {}Definition of a function BK EX REF; or associated function if inside impl.
     fn f() -> S {}Same, returning a value of type S.
     fn f(&self) {}Define a method, e.g., within an impl S {}.
const fn f() {}Constant fn usable at compile time, e.g., const X: u32 = f(Y). '18
async fn f() {}Async '18 function transformation, makes f return an impl Future. STD
     async fn f() -> S {}Same, but make f return an impl Future<Output=S>.
     async { x }Used within a function, make { x } an impl Future<Output=X>.
fn() -> SFunction pointers, BK STD REF, memory holding address of a callable.
Fn() -> SCallable Trait, BK STD (also FnMut, FnOnce), implemented by closures, fn's ...
|| {} A closure BK EX REF that borrows its captures.
     |x| {}Closure with a bound parameter x.
     |x| x + xClosure without block expression; may only consist of single expression.
     move |x| x + y Closure taking ownership of its captures.
     return || true Closures sometimes look like logical ORs (here: return a closure).
unsafe {}If you enjoy debugging segfaults Friday night; unsafe code. BK EX NOM REF

Control Flow

Control execution within a function.

ExampleExplanation
while x {}Loop REF, run while expression x is true.
loop {}Loop infinitely REF until break. Can yield value with break x.
for x in iter {}Syntactic sugar to loop over iterators. BK STD REF
if x {} else {}Conditional branch REF if expression is true.
'label: loop {}Loop label EX REF, useful for flow control in nested loops.
breakBreak expression REF to exit a loop.
     break xSame, but make x value of the loop expression (only in actual loop).
     break 'labelExit not only this loop, but the enclosing one marked with 'label.
continue Continue expression REF to the next loop iteration of this loop.
continue 'labelSame, but instead of enclosing loop marked with 'label.
x?If x is Err or None, return and propagate. BK EX STD REF
x.awaitOnly works inside async. Yield flow until Future or Stream ? x ready. '18
return xEarly return from function. More idiomatic way is to end with expression.
f()Invoke callable f (e.g., a function, closure, function pointer, Fn, ...).
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.
     X::f(&x)Same as x.f().
     X::f(&mut x)Same as x.f().
     S::f(&x)Same as x.f() if X derefs to S, i.e., x.f() finds methods of S.
     T::f(&x)Same as x.f() if X impl T, i.e., x.f() finds methods of T if in scope.
X::f()Call associated function, e.g., X::new().
     <X as T>::f()Call trait method T::f() implemented for X.

Organizing Code

Segment projects into smaller units and minimize dependencies.

ExampleExplanation
mod m {}Define a module BK EX REF, get definition from inside {}.
mod m;Define a module, get definition from m.rs or m/mod.rs.
a::bNamespace path EX REF to element b within a (mod, enum, ...).
     ::bSearch b relative to crate root. 🗑️
     crate::bSearch b relative to crate root. '18
     self::bSearch b relative to current module.
     super::bSearch b relative to parent module.
use a::b;Use EX REF b directly in this scope without requiring a anymore.
use a::{b, c};Same, but bring b and c into scope.
use a::b as x;Bring b into scope but name x, like use std::error::Error as E.
use a::b as _;Bring b anonymously into scope, useful for traits with conflicting names.
use a::*;Bring everything from a into scope.
pub use a::b;Bring a::b into scope and reexport from here.
pub T"Public if parent path is public" visibility BK for T.
     pub(crate) TVisible at most in current crate.
     pub(self) TVisible at most in current module.
     pub(super) TVisible at most in parent.
     pub(in a::b) TVisible at most in a::b.
extern crate a;Declare dependency on external crate BK EX REF 🗑️ ; just use a::b in '18.
extern "C" {}Declare external dependencies and ABI (e.g., "C") from FFI. BK EX NOM REF
extern "C" fn f() {}Define function to be exported with ABI (e.g., "C") to FFI.

Type Aliases and Casts

Short-hand names of types, and methods to convert one type to another.

ExampleExplanation
type T = S;Create a type alias BK REF, i.e., another name for S.
SelfType alias for implementing type REF, e.g. fn new() -> Self.
selfMethod subject in fn f(self) {}, same as fn f(self: Self) {}.
     &selfSame, but refers to self as borrowed, same as f(self: &Self)
     &mut selfSame, 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()).
S as TDisambiguate BK REF type S as trait T, e.g., <X as T>::f().
S as RIn use of symbol, import S as R, e.g., use a::b as x.
x as u32Primitive cast EX REF, may truncate and be a bit surprising. NOM

Macros & Attributes

Code generation constructs expanded before the actual compilation happens.

ExampleExplanation
m!()Macro BK STD REF invocation, also m!{}, m![] (depending on macro).
$x:tyMacro capture, also $x:expr, $x:ty, $x:path, ... see next table.
$xMacro substitution in macros by example. BK EX REF
$(x),*Macro repetition "zero or more times" in macros by example.
     $(x),?Same, but "zero or one time".
     $(x),+Same, but "one or more times".
     $(x)<<+In fact separators other than , are also accepted. Here: <<.
$crateSpecial hygiene variable, crate where macros is defined. ?
#[attr]Outer attribute. EX REF, annotating the following item.
#![attr]Inner attribute, annotating the surrounding item.
 

In a macro_rules! implementation, the following macro captures can be used:

Macro CaptureExplanation
$x:itemAn item, like a function, struct, module, etc.
$x:blockA block {} of statements or expressions, e.g., { let x = 5; }
$x:stmtA statement, e.g., let x = 1 + 1;, String::new(); or vec![];
$x:exprAn expression, e.g., x, 1 + 1, String::new() or vec![]
$x:patA pattern, e.g., Some(t), (17, 'a') or _.
$x:tyA type, e.g., String, usize or Vec<u8>.
$x:identAn identifier, for example in let x = 0; the identifier is x.
$x:pathA path (e.g. foo, ::std::mem::replace, transmute::<_, int>, …).
$x:literalA literal (e.g. 3, "foo", b"bar", etc.).
$x:metaA meta item; the things that go inside #[...] and #![...] attributes.
$x:ttA single token tree, see here for more details.

Pattern Matching

Constructs found in match or let expressions, or function parameters.

ExampleExplanation
match m {}Initiate pattern matching BK EX REF, then use match arms, c. next table.
let S(x) = get();Notably, let also pattern matches similar to the table below.
     let S { x } = s;Only x will be bound to value s.x.
     let (_, b, _) = abc;Only b will be bound to value abc.1.
     let (a, ..) = abc;Ignoring 'the rest' also works.
     let Some(x) = get();Won't work 🛑 if pattern can be refuted REF, use if let instead.
if let Some(x) = get() {}Branch if pattern can be assigned (e.g., enum variant), syntactic sugar. *
fn f(S { x }: S)Function parameters also work like let, here x bound to s.x of f(s).

* Desugars to match get() { Some(x) => {}, _ => () }.

 

Pattern matching arms in match expressions. The left side of these arms can also be found in let expressions.

Match ArmExplanation
E::A => {}Match enum variant A, c. pattern matching. BK EX REF
E::B ( .. ) => {}Match enum tuple variant B, wildcard any index.
E::C { .. } => {}Match enum struct variant C, wildcard any field.
S { x: 0, y: 1 } => {}Match struct with specific values (only accepts s with s.x of 0 and s.y of 1).
S { x: a, y: b } => {}Match struct with any(!) values and bind s.x to a and s.y to b.
     S { x, y } => {}Same, but shorthand with s.x and s.y bound as x and y respectively.
S { .. } => {}Match struct with any values.
D => {}Match enum variant E::D if D in use.
D => {}Match anything, bind D; possibly false friend 🛑 of E::D if D not in use.
_ => {}Proper wildcard that matches anything / "all the rest".
(a, 0) => {}Match tuple with any value for a and 0 for second.
[a, 0] => {}Slice pattern, REF 🔗 match array with any value for a and 0 for second.
     [1, ..] => {}Match array starting with 1, any value for rest; subslice pattern. ?
     [2, .., 5] => {}Match array starting with 1, ending with 5.
     [2, x @ .., 5] => {}Same, but also bind x to slice representing middle (c. next entry).
x @ 1..=5 => {}Bind matched to x; pattern binding, BK EX REF here x would be 1, 2, ... or 5.
0 | 1 => {}Pattern alternatives (or-patterns).
     E::A | E::Z Same, but on enum variants.
     E::C {x} | E::D {x}Same, but bind x if all variants have it.
S { x } if x > 10 => {}Pattern match guards, BK EX REF condition must be true as well to match.

Generics & Constraints

Generics combine with many other constructs such as struct S<T>, fn f<T>(), ...

ExampleExplanation
S<T>A generic BK EX type with a type parameter (T is placeholder name here).
S<T: R>Type short hand trait bound BK EX specification (R must be actual trait).
     T: R, P: SIndependent trait bounds (here one for T and one for P).
     T: R, SCompile error 🛑, you probably want compound bound R + S below.
     T: R + SCompound trait bound BK EX, T must fulfill R and S.
     T: R + 'aSame, but w. lifetime. T must fulfill R, if T has lifetimes, must outlive 'a.
     T: ?SizedOpt out of a pre-defined trait bound, here Sized. ?
     T: 'aType lifetime bound EX; if T has references, they must outlive 'a.
     'b: 'aLifetime 'b must live at least as long as (i.e., outlive) 'a bound.
S<T> where T: RSame as S<T: R> but more pleasant to read for longer bounds.
S<T = R>Default type parameter BK for associated type.
S<'_>Inferred anonymous lifetime; asks compiler to 'figure it out' if obvious.
S<_>Inferred anonymous type, e.g., as let x: Vec<_> = iter.collect()
S::<T>Turbofish STD call site type disambiguation, e.g. f::<u32>().
trait T<X> {}A trait generic over X. Can have multiple impl T for S (one per X).
trait T { type X; }Defines associated type BK REF X. Only one impl T for S possible.
     type X = R;Set associated type within impl T for S { type X = R; }.
impl<T> S<T> {}Implement functionality for any T in S<T>.
impl S<T> {}Implement functionality for exactly S<T> (e.g., S<u32>).
fn f() -> impl TExistential 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.
fn f() where Self: RIn a trait T {}, mark f as accessible only on types that also impl R.
for<'a>Higher-ranked trait bounds. NOM REF
     trait T: for<'a> R<'a> {}Any S that impl T would also have to fulfill R for any lifetime.

Strings & Chars

Rust has several ways to create string or char literals, depending on your needs.

ExampleExplanation
"..."String literal, REF UTF-8, will interpret \n as line break 0xA, ...
r"...",Raw string literal. REF UTF-8, won't interpret \n, ...
r#"..."#, etc.Raw string literal, UTF-8, but can also contain ".
b"..."Byte string literal; REF constructs ASCII [u8], not a string.
br"...", br#"..."#, etc.Raw byte string literal, ASCII [u8], combination of the above.
'🦀'Character literal, REF fixed 4 byte unicode 'char'. STD
b'x'ASCII byte literal. REF

Comments

No comment.

ExampleExplanation
//Line comment, use these to document code flow or internals.
//!Inner line doc comment BK EX REF for auto generated documentation.
///Outer line doc comment, use these on types.
/*...*/Block comment.
/*!...*/Inner block doc comment.
/**...*/Outer block doc comment.
```rust ... ```In doc comments, include a doc test (doc code running on cargo test).
#In doc tests, hide line from documentation (``` # use x::hidden; ```).

Miscellaneous

These sigils did not fit any other category but are good to know nonetheless.

ExampleExplanation
!Always empty never type. 🚧 BK EX STD REF
_Unnamed variable binding, e.g., |x, _| {}.
_xVariable binding explicitly marked as unused.
1_234_567Numeric separator for visual clarity.
1_u8Type specifier for numeric literals EX REF (also i8, u16, ...).
0xBEEF, 0o777, 0b1001Hexadecimal (0x), octal (0o) and binary (0b) integer literals.
r#fooA raw identifier BK EX for edition compatibility.
x;Statement REF terminator, c. expressions EX REF

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 supports operator overloading. STD


Behind the Scenes

Language Sugar

If something works that "shouldn't work now that you think about it", it might be due to one of these.

NameDescription
Coercions NOM'Weaken' types to match signature, e.g., &mut T to &T.
Deref NOMDeref x: T until *x, **x, ... compatible with some target S.
Prelude STDAutomatic import of basic types.
ReborrowSince x: &mut T can't be copied; move new &mut *x instead.
Lifetime Elision BK NOM REFAutomatically annotate f(x: &T) to f<'a>(x: &'a T).
Method Resolution REFDeref or borrow x until x.f() works.
 

Editorial Comment 💬 — While the features above will make your development life easier, they might sometimes hinder your understanding of what's going on. If you are relatively new to Rust and trying to get to the bottom of things, you should consider reading about them in more detail.


Data & Types

Memory representations of common data types.

Basic Types

Essential types built into the core of the language.

Numeric Types REF

u8, i8 u16, i16 u32, i32 u64, i64 u128, i128 f32 f64 usize, isize Same as ptr on platform.
 
Integer*Max Value
u8255
u1665_535
u324_294_967_295
u6418_446_744_073_709_551_615
u128340_282_366_920_938_463_463_374_607_431_768_211_455

* i8, i16, ... values range from -max/2 to max/2, rounded towards negative infinity.

Sample bit representation* for a f32:

S E E E E E E E E F F F F F F F F F F F F F F F F F F F F F F F
 

Explanation:

f32S (1)E (8)F (23)Value
Normalized number±1 to 254any±(1.F)2 * 2E-127
Denormalized number±0non-zero±(0.F)2 * 2-126
Zero±00±0
Infinity±2550±∞
NaN±255non-zeroNaN
 

Similarly, for f64 types this would look like:

f64S (1)E (11)F (52)Value
Normalized number±1 to 2046any±(1.F)2 * 2E-1023
Denormalized number±0non-zero±(0.F)2 * 2-1022
Zero±00±0
Infinity±20470±∞
NaN±2047non-zeroNaN
* Float types follow IEEE 754-2008 and depend on platform endianness.
 

Textual Types REF

char Any UTF-8 scalar. str ... U T F - 8 ... unspecified times Rarely seen alone, but as &str instead.

Notice how:

  • char is always 4 bytes and only holds a single Unicode scalar value (thus possibly wasting space),
  • str is a byte-array of unknown length guaranteed to hold UTF-8 code points (but harder to index).
 

Custom Types

Basic types definable by users. Actual layout REF is subject to representation; REF padding can be present.

T: Sized T T: ?Sized T Dynamically
Sized Types REF
(A, B, C) A B C struct S; struct S { b: B, c: C } B C [T; n] T T T ... n times [T] ... T T T ... unspecified times
 

These sum types hold a value of one of their sub types:

enum E { A, B, C } Tag A exclusive or Tag B exclusive or Tag C Safely holds A or B or C, also
called 'tagged union', though
compiler may omit tag.
union { ... } A unsafe or B unsafe or C Can unsafely reinterpret
memory. Result might
be undefined.

References & Pointers

References give safe access to other memory, raw pointers unsafe access. For some referents additional payload may be present (see below). The respective mut types are identical.

&'a T ptr4/8 payload4/8 | T During 'a any 'mem' this targets must
always be a valid t of T.
*const T ptr4/8 payload4/8 No guarantees.
 

The payload depends on the base type of the referent. This applies to both references and pointers.

&'a T ptr4/8 | T &'a T ptr4/8 len4/8 | T If T is an unsized struct such
as S { x: [u8] } field len is
length of dyn. sized content.
&'a [T] ptr4/8 len4/8 | ... T T ... &'a str ptr4/8 len4/8 | ... U T F - 8 ...
&'a dyn Trait ptr4/8 ptr4/8 | T |
*Drop::drop(&mut T)
size
align
*Trait::f(&T, ...)
*Trait::g(&T, ...)
Where *Drop::drop(), *Trait::f(), ... are pointers to their respective impl for T.

Closures

A closure is an ad-hoc function that comes with an automatically managed data block capturing REF the environment you accessed when defining the closure. For example:

move |x| x + y.f() + z Y Z Anonymous closure type C1 |x| x + y.f() + z ptr4/8 ptr4/8 Anonymous closure type C2 | Y | Z   Also produces anonymous fn such as f_c1 (C1, X) or
f_c2 (&C2, X). Details depend which FnOnce, FnMut, Fn ...
is supported, based on properties of captured types.

Standard Library Types

Rust's standard library combines many of the above primitive types into useful types with special semantics. Some common types:

UnsafeCell<T> T Magic type allowing
aliased mutability.
Cell<T> T Allows T's
to move in
and out.
RefCell<T> borrowed T Also support dynamic
borrowing of T. Like Cell this
is Send, but not Sync.
AtomicUsize usize4/8 Other atomic similarly. Option<T> Tag or Tag T Tag may be omitted for
certain T.
Result<T, E> Tag E or Tag T
 

General Purpose Heap Storage

Box<T> ptr4/8 payload4/8 | T Vec<T> ptr4/8 capacity4/8 len4/8 |
T T ... len
capacity
 

Owned Strings

String ptr4/8 capacity4/8 len4/8 |
U T F - 8 ... len
capacity
Observe how String differs from &str and &[char].
CString ptr4/8 len4/8 |
A B C ... len ...
Nul-terminated but w/o nul in middle.
OsString ? Platform Defined |
? ? / ? ?
PathBuf ? OsString |
? ? / ? ?
 

Shared Ownership

If the type does not contain a Cell for T, these are often combined with one of the Cell types above to allow shared de-facto mutability.

Rc<T> ptr4/8 payload4/8
| strong4/8 weak4/8 T
Share ownership of T in same thread. Needs nested Cell
or RefCellto allow mutation. Is neither Send nor Sync.
Arc<T> ptr4/8 payload4/8
| strong4/8 weak4/8 T
Same, but allow sharing between threads IF contained
T itself is Send and Sync.

Mutex<T> / RwLock<T> ptr4/8 poisoned4/8 T | lock Needs to be held in Arc to be shared between
threads, always Send and Sync. Consider using
parking_lot instead (faster, no heap usage).

Standard Library

Traits

Traits define common behavior. If S implements trait T, you know S can behave as prescribed by T. Below is an overview of traits that may be a bit more tricky.

🧵 Thread Safety

ExamplesSend*!Send
Sync*Most types ... Mutex<T>, Arc<T>1,2MutexGuard<T>1, RwLockReadGuard<T>1
!SyncCell<T>2, RefCell<T>2Rc<T>, Formatter, &dyn Trait

* An instance t where T: Send can be moved to another thread, a T: Sync means &t can be moved to another thread.
1 If T is Sync.
2 If T is Send.

🚥 Iterators

Basics

Assume you have a collection c of type C:

  • c.into_iter() — Turns collection c into an Iterator STD i and consumes* c. Requires IntoIterator STD for C to be implemented. Type of item depends on what C was. 'Standardized' way to get Iterators.
  • c.iter() — Courtesy method some collections provide, returns borrowing Iterator, doesn't consume c.
  • c.iter_mut() — Same, but mutably borrowing Iterator that allow collection to be changed.

The Iterator

Once you have an i:

  • i.next() — Returns Some(x) next element c provides, or None if we're done.

For Loops

  • for x in c {} — Syntactic sugar, calls c.into_iter() and loops i until None.

* If it looks as if it doesn't consume c that's because your type was Copy. For example, if you call (&c).into_iter() it will invoke .into_iter() on &c (which will consume the reference and turn it into an Iterator), but c remains untouched.

Basics

Let's assume you have a struct C {} that is your collection.

  • struct Iter {} — Create a struct to hold your iteration status (e.g., an index) for immutable iteration.
  • impl Iterator for Iter {} — Provide an implementation of Iterator::next() so it can produce elements.

In addition, you might want to add a convenience fn iter(&self) -> Iter inside your impl C {}.

Mutable Iterators

  • struct IterMut {} — To provide mutable iterators create another struct that can hold C as &mut.
  • impl Iterator for IterMut {} — In that case Iterator::Item is probably a &mut item

Similarly, providing a fn iter_mut(&mut self) -> IterMut might be a good idea.

Making Loops Work

  • impl IntoIterator for C {} — Now for loops work as for x in c {}.
  • impl IntoIterator for &C {} — For conveninece you might want to add these as well.
  • impl IntoIterator for &mut C {} — Same ...
 

String Conversions

If you want a string of type ...

If you have x of type ...Use this ...
Stringx
CStringx.into_string()?
OsStringx.to_str()?.into()
PathBufx.to_str()?.into()
Vec<u8> 1String::from_utf8(x)?
&strx.into()
&CStrx.to_str()?.into()
&OSStrx.to_str()?.into()
&Pathx.to_str()?.into()
&[u8] 1String::from_utf8_lossy(x).into()
If you have x of type ...Use this ...
StringCString::new(x)?
CStringx
OsString 2CString::new(x.to_str()?)?
PathBufCString::new(x.to_str()?)?
Vec<u8> 1CString::new(x)?
&strCString::new(x)?
&CStrx.into()
&OSStr 2CString::new(x.to_os_string().into_string()?)?
&Pathx.to_str()?.into()
&[u8] 1CString::new(Vec::from(x))?
*mut c_char 3unsafe { CString::from_raw(x) }
If you have x of type ...Use this ...
Stringx.into()
CStringx.to_str()?.into()
OsStringx
PathBufx.into_os_string()
Vec<u8> 1?
&strx.into()
&CStrx.to_str()?.into()
&OSStrx.into()
&Pathx.as_os_str().into()
&[u8] 1?
If you have x of type ...Use this ...
Stringx.into()
CStringx.to_str()?.into()
OsStringx.into()
PathBufx
Vec<u8> 1?
&strx.into()
&CStrx.to_str()?.into()
&OSStrx.into()
&Pathx.into()
&[u8] 1?
If you have x of type ...Use this ...
Stringx.into_bytes()
CStringx.into_bytes()
OsString?
PathBuf?
Vec<u8> 1x
&strx.as_bytes().into()
&CStrx.to_bytes_with_nul().into()
&OSStr?
&Path?
&[u8] 1x.into()
If you have x of type ...Use this ...
Stringx.as_str()
CStringx.to_str()?
OsStringx.to_str()?
PathBufx.to_str()?
Vec<u8> 1std::str::from_utf8(&x)?
&strx
&CStrx.to_str()?
&OSStrx.to_str()?
&Pathx.to_str()?
&[u8] 1std::str::from_utf8(x)?
If you have x of type ...Use this ...
StringCString::new(x)?.as_c_str()
CStringx.as_c_str()
OsString 2x.to_str()?
PathBufCStr::from_bytes_with_nul(x.to_str()?.as_bytes())?
Vec<u8> 1CStr::from_bytes_with_nul(&x)?
&strCStr::from_bytes_with_nul(x.as_bytes())?
&CStrx
&OSStr 2?
&Path?
&[u8] 1CStr::from_bytes_with_nul(x)?
*const c_char 1unsafe { CStr::from_ptr(x) }
If you have x of type ...Use this ...
StringOsStr::new(&x)
CString?
OsStringx.as_os_str()
PathBufx.as_os_str()
Vec<u8> 1?
&strOsStr::new(x)
&CStr?
&OSStrx
&Pathx.as_os_str()
&[u8] 1?
If you have x of type ...Use this ...
Stringx.as_ref()
CStringx.to_str()?.as_ref()
OsStringx.as_ref()
PathBufx.as_ref()
Vec<u8> 1?
&strx.as_ref()
&CStrx.to_str()?.as_ref()
&OSStrx.as_ref()
&Pathx
&[u8] 1?
If you have x of type ...Use this ...
Stringx.as_bytes()
CStringx.as_bytes()
OsString?
PathBuf?
Vec<u8> 1&x
&strx.as_bytes()
&CStrx.to_bytes_with_nul()
&OSStrx.as_bytes() 2
&Path?
&[u8] 1x
You wantAnd have xUse this ...
*const c_charCStringx.as_ptr()

1 You should or must (if unsafe calls are involved) ensure the raw data comes with a valid representation for the string type (e.g., being UTF-8 encoded data for a String).

2 Only on some platforms std::os::<your_os>::ffi::OsStrExt exists with helper methods to get a raw &[u8] representation of the underlying OsStr. Use the rest of the table to go from there, e.g.:

use std::os::unix::ffi::OsStrExt;
let bytes: &[u8] = my_os_str.as_bytes();
CString::new(bytes)?

3 The c_char must have come from a previous CString. If it comes from FFI see &CStr instead.

 

String Formatting

Formatting applies to print!, eprint!, write! (and their -ln siblings like println!). Each format argument is either empty {}, {argument}, or follows a basic syntax:

{ [argument] ':' [[fill] align] [sign] ['#'] [width [$]] ['.' precision [$]] [type] }
ElementMeaning
argumentNumber (0, 1, ...) or argument name, e.g., print!("{x}", x = 3).
fillThe character to fill empty spaces with (e.g., 0), if width is specified.
alignLeft (<), center (^), or right (>), if width is specified.
signCan be + for sign to always be printed.
#Alternate formatting, e.g. prettify Debug ? or prefix hex with 0x.
widthMinimum width (≥ 0), padding with fill (default to space). If starts with 0, zero-padded.
precisionDecimal digits (≥ 0) for numerics, or max width for non-numerics.
$Interpret width or precision as argument identifier instead to allow for dynamic formatting.
typeDebug (?) formatting, hex (x), binary (b), octal (o), pointer (p), exp (e) ... see more.
 
ExampleExplanation
{:?}Print the next argument using Debug.
{2:#?}Pretty-print the 3rd argument with Debug formatting.
{val:^2$}Center the val named argument, width specified by the 3rd argument.
{:<10.3}Left align with width 10 and a precision of 3.
{val:#x}Format val argument as hex, with a leading 0x (alternate format for x).
 

Tooling

Project Anatomy

Basic project layout, and common files and folders, as used by Rust tooling.

EntryCode
📁 benches/Benchmarks for your crate, run via cargo bench, requires nightly by default. * 🚧
📁 examples/Examples how to use your crate, run via cargo run --example my_example.
📁 src/Actual source code for your project.
     build.rsPre-build script, e.g., when compiling C / FFI, needs to be specified in Cargo.toml.
     main.rsDefault entry point for applications, this is what cargo run uses.
     lib.rsDefault entry point for libraries. This is where lookup for my_crate::f starts.
📁 tests/Integration tests go here, invoked via cargo test. Unit tests often stay in src/ file.
.rustfmt.tomlIn case you want to customize how cargo fmt works.
.clippy.tomlSpecial configuration for certain clippy lints.
Cargo.tomlMain project configuration. Defines dependencies, artifacts ...
Cargo.lockDependency details for reproducible builds, recommended to git for apps, not for libs.

* On stable consider Criterion.

 

Minimal examples for various entry points might look like:

// src/main.rs (default application entry point)

fn main() {
    println!("Hello, world!");
}
// src/lib.rs (default library entry point)

pub fn f() {}      // Is a public item in root, so it's accessible from the outside.

mod m {
    pub fn g() {}  // No public path (`m` not public) from root, so `g`
}                  // is not accessible from the outside of the crate.
// src/lib.rs (default entry point for proc macros)

extern crate proc_macro;  // Apparently needed to be imported like this.

use proc_macro::TokenStream;

#[proc_macro_attribute]   // Can now be used as `#[my_attribute]`
pub fn my_attribute(_attr: TokenStream, item: TokenStream) -> TokenStream {
    item
}
// Cargo.toml

[package]
name = "my_crate"
version = "0.1.0"

[lib]
crate_type = ["proc-macro"]
// src/my_module.rs (any file of your project)

fn f() -> u32 { 0 }

#[cfg(test)]
mod test {
    use super::f;           // Need to import items from parent module. Has
                            // access to non-public members.
    #[test]
    fn ff() {
        assert_eq!(f(), 0);
    }
}
// tests/sample.rs (sample integration test)

#[test]
fn my_sample() {
    assert_eq!(my_crate::f(), 123); // Integration tests (and benchmarks) 'depend' to the crate like
}                                   // a 3rd party would. Hence, they only see public items.
// benches/sample.rs (sample benchmark)

#![feature(test)]   // #[bench] is still experimental

extern crate test;  // Even in '18 this is needed ... for reasons.
                    // Normally you don't need this in '18 code.

use test::{black_box, Bencher};

#[bench]
fn my_algo(b: &mut Bencher) {
    b.iter(|| black_box(my_crate::f())); // `black_box` prevents `f` from being optimized away.
}
 

Cargo

Some commands and tools that are good to know.

CommandDescription
cargo initCreate a new project for the latest edition.
cargo buildBuild the project in debug mode (--release for all optimization).
cargo checkCheck if project would compile (much faster).
cargo testRun tests for the project.
cargo runRun your project, if a binary is produced (main.rs).
cargo doc --openLocally generate documentation for your code and dependencies.
cargo rustc -- -Zunpretty=XShow more desugared Rust code, in particular with X being:
     expandedShow with expanded macros, ...
cargo +{nightly, stable} ...Runs command with given toolchain, e.g., for 'nightly only' tools.
rustup docsOpen offline Rust documentation (incl. the books), good on a plane!

A command like cargo build means you can either type cargo build or just cargo b.

 

These are optional rustup components. Install them with rustup component add [tool].

ToolDescription
cargo clippyAdditional (lints) catching common API misuses and unidiomatic code. 🔗
cargo fmtAutomatic code formatter (rustup component add rustfmt). 🔗
 

A large number of additional cargo plugins can be found here.

 

Cross Compilation

General rundown: 🔗

  1. Check target is supported.
  2. Install target via rustup target install X.
  3. Install native toolchain (required to link, depends on target). Get this from your target vendor (Google, Apple, ...). Might not be available for your host (e.g., no iOS toolchain for Windows). Some toolchains require additional build steps (e.g., Android's make-standalone-toolchain.sh).
  4. Update ~/cargo/.config like this:
    [target.aarch64-linux-android]
    linker = "[PATH_TO_TOOLCHAIN]/aarch64-linux-android/bin/aarch64-linux-android-clang"
    
    or
    [target.aarch64-linux-android]
    linker = "C:/[PATH_TO_TOOLCHAIN]/prebuilt/windows-x86_64/bin/aarch64-linux-android21-clang.cmd"
    
  5. Sometimes (depending on how compiler complains) you might also need to set an environment variable. Note that some platforms / configuration can be extremely sensitive how paths are specified (e.g., \ vs /) and quoted:
    set CC=C:\[PATH_TO_TOOLCHAIN]\prebuilt\windows-x86_64\bin\aarch64-linux-android21-clang.cmd
    
  6. Compile with cargo build --target=X
 

Coding Guides

Idiomatic Rust

If you are used to programming Java or C, consider these.

IdiomCode
Think in Expressionsx = if x { a } else { b };
x = loop { break 5 };
fn f() -> u32 { 0 }
Think in Iterators(1..10).map(f).collect()
names.iter().filter(|x| x.starts_with("A"))
Handle Absence with ?x = try_something()?;
get_option()?.run()?
Use Strong Typesenum E { Invalid, Valid { ... } } over ERROR_INVALID = -1
enum E { Visible, Hidden } over visible: bool
struct Charge(f32) over f32
Provide BuildersCar::new("Model T").hp(20).run();
Split ImplementationsGeneric types S<T> can have a separate impl per T.
Rust doesn't have OO, but with separate impl you can get specialization.
UnsafeAvoid unsafe {}, often safer, faster solution without it. Exception: FFI.
Implement Traits#[derive(Debug, Copy, ...)] and custom impl where needed.
ToolingWith clippy you can improve your code quality.
Formatting with rustfmt helps others to read your code.
Add unit tests BK (#[test]) to ensure your code works.
Add doc tests BK (``` my_api::f() ```) to ensure docs match code.
DocumentationAnnotate your APIs with doc comments that can show up on docs.rs.
Don't forget to include a summary sentence and the Examples heading.
If applicable: Panics, Errors, Safety, Abort and Undefined Behavior.
 

🔥 We highly recommend you also follow the API Guidelines (Checklist) for any shared project! 🔥

 

Async-Await 101

If you are familiar with async / await in C# or TypeScript, here are some things to keep in mind:

ConstructExplanation
asyncAnything declared async always returns an impl Future<Output=_>. STD
     async fn f() {}Function f returns an impl Future<Output=()>.
     async fn f() -> S {}Function f returns an impl Future<Output=S>.
     async { x }Transforms { x } into an impl Future<Output=X>.
let sm = f(); Calling f() that is async will not execute f, but produce state machine sm. 1
     sm = async { g() };Likewise, does not execute the { g() } block; produces state machine.
runtime.block_on(sm); 2Outside an async {}, schedules sm to actually run. Would execute g().
sm.awaitInside an async {}, run sm until complete. Yield to runtime if sm not ready.

1 Technically async transforms the following code into an anonymous, compiler-generated state machine type, and f() instantiates that machine. The state machine always impl Future, possibly Send< & co, depending on types you used inside async. State machine driven by worker thread invoking Future::poll() via runtime directly, or parent .await indirectly.
2 Right now Rust doesn't come with its own runtime. Use external crate instead, such as async-std or tokio 0.2+. Also, Futures in Rust are an MPV. There is much more utility stuff in the futures crate.

 

At each x.await, state machine passes control to subordinate state machine x. At some point a low-level state machine invoked via .await might not be ready. In that the case worker thread returns all the way up to runtime so it can drive another Future. Some time later the runtime:

  • might resume execution. It usually does, unless sm / Future dropped.
  • might resume with the previous worker or another worker thread (depends on runtime).

Simplified diagram for code written inside an async block :

       consecutive_code();           consecutive_code();           consecutive_code();
START --------------------> x.await --------------------> y.await --------------------> READY
// ^                          ^     ^                               Future<Output=X> ready -^
// Invoked via runtime        |     |
// or an external .await      |     This might resume on another thread (next best available),
//                            |     or NOT AT ALL if Future was dropped.
//                            |
//                            Execute `x`. If ready: just continue execution; if not, return
//                            this thread to runtime.
 

This leads to the following considerations when writing code inside an async construct:

Constructs 1Explanation
sleep_or_block();Definitely bad 🛑, never halt current thread, clogs executor.
set_TL(a); x.await; TL();Definitely bad 🛑, await may return from other thread, thread local invalid.
s.no(); x.await; s.go();Maybe bad 🛑, await will not return if Future dropped while waiting. 2
Rc::new(); x.await; rc();Non-Send types prevent impl Future from being Send; less compatible.

1 Here we assume s is any non-local that could temporarily be put into an invalid state; TL is any thread local storage, and that the async {} containing the code is written without assuming executor specifics.
2 Since Drop is run in any case when Future is dropped, consider using drop guard that cleans up / fixes application state if it has to be left in bad condition across .await points.

 

Closures in APIs

There is a subtrait relationship Fn : FnMut : FnOnce. That means, a closure that implements Fn, also implements FnMut and FnOnce. Likewise, a closure that implements FnMut, also implements FnOnce.

From a call site perspective that means:

SignatureFunction g can call ...Function g accepts ...
g<F: FnOnce()>(f: F)... f() once.Fn, FnMut, FnOnce
g<F: FnMut()>(mut f: F)... f() multiple times.Fn, FnMut
g<F: Fn()>(f: F)... f() multiple times.Fn

Notice how asking for a Fn closure as a function is most restrictive for the caller; but having a Fn closure as a caller is most compatible with any function.

 

From the perspective of someone defining a closure:

ClosureImplements*Comment
|| { moved_s; } FnOnceCaller must give up ownership of moved_s.
|| { &mut s; } FnOnce, FnMutAllows g() to change caller's local state s.
|| { &s; } FnOnce, FnMut, FnMay not mutate state; but can share and reuse s.

* Rust prefers capturing by reference (resulting in the most "compatible" Fn closures from a caller perspective), but can be forced to capture its environment by copy or move via the move || {} syntax.

 

That gives the following advantages and disadvantages:

RequiringAdvantageDisadvantage
F: FnOnceEasy to satisfy as caller.Single use only, g() may call f() just once.
F: FnMutAllows g() to change caller state.Caller may not reuse captures during g().
F: FnMany can exist at same time.Hardest to produce for caller.
 

Reading Lifetimes

Lifetimes can be overwhelming at times. Here is a simplified guide on how to read and interpret constructs containing lifetimes if you are familiar with C.

ConstructHow to read
let s: S = S(0)A location that is S-sized, named s, and contains the value S(0).
If declared with let, that location lives on the stack. 1
Generally, s can mean location of s, and value within s.
As a location, s = S(1) means, assign value S(1) to location s.
As a value, f(s) means call f with value inside of s.
To explicitly talk about its location (address) we do &s.
To explicitly talk about a location that can hold such a location we do &S.
&'a SA &S is a location that can hold (at least) an address, called reference.
Any address stored in here must be that of a valid S.
Any address stored must be proven to exist for at least (outlive) duration 'a.
In other words, the &S part sets bounds for what any address here must contain.
While the &'a part sets bounds for how long any such address must at least live.
The lifetime our containing location is unrelated, but naturally always shorter.
Duration of 'a is purely compile time view, based on static analysis.
&SSometimes 'a might be elided (or can't be specified) but it still exists.
Within methods bodies, lifetimes are determined automatically.
Within signatures, lifetimes may be 'elided' (annotated automatically).
&sThis will produce the actual address of location s, called 'borrow'.
The moment &s is produced, location s is put into a borrowed state.
Checking if in borrowed state is based on compile-time analysis.
This analysis is based on all possible address propagation paths.
As long as any &s could be around, s cannot be altered directly.
For example, in let a = &s; let b = a;, also b needs to go.
Borrowing of s stops once last &s is last used, not when &s dropped.
&mut sSame, but will produce a mutable borrow.
A &mut will allow the owner of the borrow (address) to change s content.
This reiterates that not the value in s, but location of s is borrowed.

1 Compare Data Structures section above: while true for synchronous code, an async 'stack frame' might actually be placed on to the heap by the used async runtime.

 

When reading function or type signatures in particular:

ConstructHow to read
S<'a> {}Signals that S will contain* at least one address (i.e., reference).
'a will be determined automatically by the user of this struct.
'a will be chosen as small as possible.
f<'a>(x: &'a T)Signals this function will accept an address (i.e., reference).
                    -> &'a S... and that it returns one.
'a will be determined automatically by the caller.
'a will be chosen as small as possible.
'a will be picked so that it satisfies input and output at call site.
More importantly, propagate borrow state according to lifetime names!
So while result address with 'a is used, input address with 'a is locked.
Here: while s from let s = f(&x) is around, x counts as 'borrowed'.
<'a, 'b: 'a>The lifetimes declared in S<> and f<> can also have bounds.
The <'a, 'b> part means the type will handle at least 2 addresses.
The 'b: 'a part is a lifetime bound, and means 'b must outlive 'a.
Any address in an &'b X must exist at least as long as any in an &'a Y.

* Technically the struct may not hold any data (e.g., when using the 'a only for PhantomData or function pointers) but still make use of the 'a for communicating and requiring that some of its functions require reference of a certain lifetime.

 

Unsafe, Unsound, Undefined

Unsafe leads to unsound. Unsound leads to undefined. Undefined leads to the dark side of the force.

Unsafe Code

  • Code marked unsafe has special permissions, e.g., to deref raw pointers, or invoke other unsafe functions.
  • Along come special promises the author must uphold to the compiler, and the compiler will trust you.
  • By itself unsafe code is not bad, but dangerous, and needed for FFI or exotic data structures.
// `x` must always point to race-free, valid, aligned, initialized u8 memory.
unsafe fn unsafe_f(x: *mut u8) {
    my_native_lib(x);
}

Undefined Behavior (UB)

  • As mentioned, unsafe code implies special promises to the compiler (it wouldn't need be unsafe otherwise).
  • Failure to uphold any promise makes compiler produce fallacious code, execution of which leads to UB.
  • After triggering undefined behavior anything can happen. Insidiously, the effects may be 1) subtle, 2) manifest far away from the site of violation or 3) be visible only under certain conditions.
  • A seemingly working program (incl. any number of unit tests) is no proof UB code might not fail on a whim.
  • Code with UB is objectively dangerous, invalid and should never exist.
if maybe_true() {
   let r: &u8 = unsafe { &*ptr::null() };    // Once this runs, ENTIRE app is undefined. Even if
} else {                                     // line seemingly didn't do anything, app might now run
    println!("the spanish inquisition");     // both paths, corrupt database, or anything else.
}

Unsound Code

  • Any safe Rust that could (even only theoretically) produce UB for any user input is always unsound.
  • As is unsafe code that may invoke UB on its own accord by violating above-mentioned promises.
  • Unsound code is a stability and security risk, and violates basic assumption many Rust users have.
fn unsound_ref<T>(x: &T) -> &u128 {      // Signature looks safe to users. Happens to be
    unsafe { mem::transmute(x) }         // ok if invoked with an &u128, UB for practically
}                                        // everything else.
 

Responsible use of Unsafe

  • Do not use unsafe unless you absolutely have to.
  • Follow the Nomicon, Unsafe Guidelines, always uphold all safety invariants, and never invoke UB.
  • Minimize the use of unsafe and encapsulate it in the small, sound modules that are easy to review.
  • Each unsafe unit should be accompanied by plain-text reasoning outlining its safety.
 

API Stability

These changes can break client code, compare RFC 1105. Major changes (🔴) are definitely breaking, while minor changes (🟡) might be breaking:

 
Crates
🔴 Making a crate that previously compiled for stable require nightly.
🟡 Altering use of Cargo features (e.g., adding or removing features).
 
Modules
🔴 Renaming / moving / removing any public items.
🟡 Adding new public items, as this might break code that does use your_crate::*.
 
Structs
🔴 Adding private field when all current fields public.
🔴 Adding public field when no private field exists.
🟡 Adding or removing private fields when at least one already exists (before and after the change).
🟡 Going from a tuple struct with all private fields (with at least one field) to a normal struct, or vice versa.
 
Enums
🔴 Adding new variants.
🔴 Adding new fields to a variant.
 
Traits
🔴 Adding a non-defaulted item, breaks all existing impl T for S {}.
🔴 Any non-trivial change to item signatures, will affect either consumers or implementors.
🟡 Adding a defaulted item; might cause dispatch ambiguity with other existing trait.
🟡 Adding a defaulted type parameter.
 
Traits
🔴 Implementing any "fundamental" trait, as not implementing a fundamental trait already was a promise.
🟡 Implementing any non-fundamental trait; might also cause dispatch ambiguity.
 
Inherent Implementations
🟡 Adding any inherent items; might cause clients to prefer that over trait fn and produce compile error.
 
Signatures in Type Definitions
🔴 Tightening bounds (e.g., <T> to <T: Clone>).
🟡 Loosening bounds.
🟡 Adding defaulted type parameters.
🟡 Generalizing to generics.
Signatures in Functions
🔴 Adding / removing arguments.
🟡 Introducing a new type parameter.
🟡 Generalizing to generics.
 
Behavioral Changes
🔴 / 🟡 Changing semantics might not cause compiler errors, but might make clients do wrong thing.
 

Misc

These are other great visual guides and tables.

A preview image.
Containers
A preview image.
Macro Railroad
A preview image.
Lifetimes
 
Cheat SheetsDescription
Rust Learning⭐Probably the best collection of links about learning Rust.
Functional Jargon in RustA collection of functional programming jargon explained in Rust.
Periodic Table of TypesHow various types and references correlate.
FuturesHow to construct and work with futures.
Rust Iterator Cheat SheetSummary of iterator-related methods from std::iter and itertools.
Type-Based Rust Cheat SheetLists common types and how they convert.
 

All major Rust books developed by the community.

Books ️📚Description
The Rust Programming LanguageStandard introduction to Rust, start here if you are new.
     API GuidelinesHow to write idiomatic and re-usable Rust.
     Asynchronous Programming in Rust 🚧Explains async code, Futures, ...
     Edition GuideWorking with Rust 2015, Rust 2018, and beyond.
     Guide to Rustc DevelopmentExplains how the compiler works internally.
     Little Book of Rust Macros 🚧Community's collective knowledge of Rust macros.
     Reference 🚧Reference of the Rust language.
     RFC Book Look up accepted RFCs and how they change the language.
     Rust CookbookCollection of simple examples that demonstrate good practices.
     Rustdoc BookTips how to customize cargo doc and rustdoc.
     RustonomiconDark Arts of Advanced and Unsafe Rust Programming.
     Unsafe Code Guidelines 🚧Concise information about writing unsafe code.
     Unstable BookInformation about unstable items, e.g, #![feature(...)].
The Cargo BookHow to use cargo and write Cargo.toml.
The CLI BookInformation about creating CLI tools.
The Embedded BookWorking with embedded and #![no_std] devices.
     The EmbedonomiconFirst #![no_std] from scratch on a Cortex-M.
The WebAssembly BookWorking with the web and producing .wasm files.
     The wasm-bindgen GuideHow to bind Rust and JavaScript APIs in particular.
 

Comprehensive lookup tables for common components.

Tables 📋Description
Rust ChangelogSee all the things that changed in a particular version.
Rust ForgeLists release train and links for people working on the compiler.
     Rust Platform SupportAll supported platforms and their Tier.
     Rust Component HistoryCheck nightly status of various Rust tools for a platform.
ALL the Clippy LintsAll the clippy lints you might be interested in.
Configuring RustfmtAll rustfmt options you can use in .rustfmt.toml.
Compiler Error IndexEver wondered what E0404 means?
 

Online services which provide information or tooling.

Services ⚙️Description
crates.ioAll 3rd party libraries for Rust.
std.rsShortcut to std documentation.
docs.rsDocumentation for 3rd party libraries, automatically generated from source.
lib.rsUnofficial overview of quality Rust libraries and applications.
Rust PlaygroundTry and share snippets of Rust code.
 

Printing & PDF

Want this Rust cheat sheet as a PDF download? Generate PDF (or select File > Print – might take 10s so) and then "Save as PDF". It looks great in both Firefox's and Chrome's PDF exports. Alternatively use the cached PDF.

Fork me on GitHub