Patterns

Pattern typeExampleNotes
Literal100 "name"Matches an exact value; the name of a const is also allowed
Range0 ... 100 'a' ... 'z'Matches any value in range, including the end value
Wildcard_Matches any value and ignores it
Variablename mut countLike _ but moves or copies the value into a new local variable
ref variableref field ref mut fieldBorrows a reference to the matched value instead of moving or copying it
Binding with subpatternval @ 0 ... 99 ref circle @ Shape::Circle { .. }Matches the pattern to the right of @, using the variable name to the left
Enum patternSome(value) None Pet::Orca 
Tuple pattern(key, value) (r, g, b) 
Struct patternColor(r, g, b) Point { x, y } Card { suit: Clubs, range: n } Account { id, name, .. } 
Reference&value &(k, v)Matches only reference values
Multiple patterns'a' \| 'A'In match only (not valid in let, etc.)
Guard expressionsx if x * x <= r2In match only (not valid in let, etc.)

Operator Overloading

Unary Operators

TraitOperatorEquivalent
std::ops::Neg-xx.neg()
std::ops::Not!xx.not()

Arithmetic Operators

TraitOperatorEquivalent
std::ops::Addx + yx.add(y)
std::ops::Subx - yx.sub(y)
std::ops::Mulx * yx.mul(y)
std::ops::Divx / yx.div(y)
std::ops::Remx % yx.rem(y)
std::ops::AddAssignx += yx.add_assign(y)
std::ops::SubAssignx -= yx.sub_assign(y)
std::ops::MulAssignx *= yx.mul_assign(y)
std::ops::DivAssignx /= yx.div_assign(y)
std::ops::RemAssignx %= yx.rem_assign(y)

Bitwise Operators

TraitOperatorEquivalent
std::ops::BitAndx & yx.bitand(y)
std::ops::BitOr`xy`
std::ops::BitXorx ^ yx.bitxor(y)
std::ops::Shlx << yx.shl(y)
std::ops::Shrx >> yx.shr(y)
std::ops::BitAndAssignx &= yx.bitand_assign(y)
std::ops::BitOrAssign`x= y`
std::ops::BitXorAssignx ^= yx.bitxor_assign(y)
std::ops::ShlAssignx <<= yx.shl_assign(y)
std::ops::ShrAssignx >>= yx.shr_assign(y)

Comparison Operators

TraitOperatorEquivalent
std::ops::PartialEqx == yx.eq(&y)
std::ops::PartialEqx != yx.ne(&y)
std::ops::PartialOrdx < yx.lt(y)
std::ops::PartialOrdx > yx.gt(y)
std::ops::PartialOrdx <= yx.le(y)
std::ops::PartialOrdx >= yx.ge(y)

Indexing Operators

TraitOperatorEquivalent
std::ops::Indexx[y]x.index(y)
std::ops::Index&x[y]*x.index(y)
std::ops::IndexMut&mut x[y]*x.index_mut(y)

Utility Traits

TraitDescription
DropDestructors. Cleanup code that Rust runs automatically whenever a value is dropped.
SizedMarker trait for types with a fixed size known at compile time, as oppose to types (such as slices) that are dynamically sized.
CloneTypes that support cloning values.
CopyMarker trait for types that can be cloned simply by making a byte-for-byte copy of the memory containing the value.
Deref, DerefMutTraits for smart pointer types.
DefaultTypes that have a sensible "default value".
AsRef, AsMutConversion traits for borrowing one type of reference from another.
Borrow, BorrowMutConversion traits like AsRef and AsMut that additionally guarantee consistent hashing, ordering, and equality.
From, IntoConversion traits for transforming one type of value into another.
ToOwnedConversion trait for converting a reference to an owned value.

Common Standard Library Iterators

Free Functions

ExpressionNotes
std::iter::empty()Returns None immediately.
std::iter::once(5)Produces the given value, and then ends.
std::iter::repeat("#9")Produces the given value forever.

std::ops::Range

ExpressionNotes
1..10Endpoints must be an integer type to be iterable. Range includes start value, and excludes end value.

std::ops::RangeFrom

ExpressionNotes
1..Unbounded iteration. Start must be an integer. May panic or overflow if the value reaches the limit of the type.

Option<T>

ExpressionNotes
Some(10).iter()Behaves like a vector whose length is either 0 (None) or 1 (Some(v)).

Result<T, E>

ExpressionNotes
Ok("blah").iter()Similar to Option, producing Ok values.

Vec<T> and &[T]

ExpressionNotes
``TODO

String and &str

ExpressionNotes
``TODO

std::collections::{HashMap, BTreeMap}

ExpressionNotes
``TODO

std::collections::{HashSet, BTreeSet}

ExpressionNotes
``TODO

std::sync::mpsc::Receiver

ExpressionNotes
``TODO

std::io::Read

ExpressionNotes
``TODO

std::io::BufRead

ExpressionNotes
``TODO

std::fs::ReadDir

ExpressionNotes
std::fs::read_dir(path)Produces directory entries.

std::net::TcpListener

ExpressionNotes
listener.incoming()Produces incoming network connections.

Filesystem Access Functions

The following are some of the functions in std::fs and their approximate Unix equivalents. All of these functions return io::Result values. All of these functions call out directly to the operating system.

Creating and deleting

UnixFunctionReturns
mkdircreate_dir(path)Result<()>
mkdir -pcreate_dir_all(path)Result<()>
rmdirremove_dir(path)Result<()>
rm -rremove_dir_all(path)Result<()>
unlinkremove_file(path)Result<()>

Copying, moving, and linking

UnixFunctionReturns
cp -pcopy(src_path, dest_path)Result<u64>
renamerename(src_path, dest_pathResult<()>
linkhard_link(src_path, dest_path)Result<()>

Inspecting

UnixFunctionReturns
realpathcanonicalize(path)Result<PathBuf>
statmetadata(path)Result<Metadata>
lstatsymlink_metadata(path)Result<Metadata>
lsread_dir(path)Result<ReadDir>
readlinkread_link(path)Result<PathBuf>

Permissions

UnixFunctionReturns
chmodset_permissions(path, perm)Result<()>