Pattern type | Example | Notes |
Literal | 100 "name" | Matches an exact value; the name of a const is also allowed |
Range | 0 ... 100 'a' ... 'z' | Matches any value in range, including the end value |
Wildcard | _ | Matches any value and ignores it |
Variable | name mut count | Like _ but moves or copies the value into a new local variable |
ref variable | ref field ref mut field | Borrows a reference to the matched value instead of moving or copying it |
Binding with subpattern | val @ 0 ... 99 ref circle @ Shape::Circle { .. } | Matches the pattern to the right of @ , using the variable name to the left |
Enum pattern | Some(value) None Pet::Orca | |
Tuple pattern | (key, value) (r, g, b) | |
Struct pattern | Color(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 expressions | x if x * x <= r2 | In match only (not valid in let , etc.) |
Trait | Operator | Equivalent |
std::ops::Neg | -x | x.neg() |
std::ops::Not | !x | x.not() |
Trait | Operator | Equivalent |
std::ops::Add | x + y | x.add(y) |
std::ops::Sub | x - y | x.sub(y) |
std::ops::Mul | x * y | x.mul(y) |
std::ops::Div | x / y | x.div(y) |
std::ops::Rem | x % y | x.rem(y) |
std::ops::AddAssign | x += y | x.add_assign(y) |
std::ops::SubAssign | x -= y | x.sub_assign(y) |
std::ops::MulAssign | x *= y | x.mul_assign(y) |
std::ops::DivAssign | x /= y | x.div_assign(y) |
std::ops::RemAssign | x %= y | x.rem_assign(y) |
Trait | Operator | Equivalent |
std::ops::BitAnd | x & y | x.bitand(y) |
std::ops::BitOr | `x | y` |
std::ops::BitXor | x ^ y | x.bitxor(y) |
std::ops::Shl | x << y | x.shl(y) |
std::ops::Shr | x >> y | x.shr(y) |
std::ops::BitAndAssign | x &= y | x.bitand_assign(y) |
std::ops::BitOrAssign | `x | = y` |
std::ops::BitXorAssign | x ^= y | x.bitxor_assign(y) |
std::ops::ShlAssign | x <<= y | x.shl_assign(y) |
std::ops::ShrAssign | x >>= y | x.shr_assign(y) |
Trait | Operator | Equivalent |
std::ops::PartialEq | x == y | x.eq(&y) |
std::ops::PartialEq | x != y | x.ne(&y) |
std::ops::PartialOrd | x < y | x.lt(y) |
std::ops::PartialOrd | x > y | x.gt(y) |
std::ops::PartialOrd | x <= y | x.le(y) |
std::ops::PartialOrd | x >= y | x.ge(y) |
Trait | Operator | Equivalent |
std::ops::Index | x[y] | x.index(y) |
std::ops::Index | &x[y] | *x.index(y) |
std::ops::IndexMut | &mut x[y] | *x.index_mut(y) |
Trait | Description |
Drop | Destructors. Cleanup code that Rust runs automatically whenever a value is dropped. |
Sized | Marker trait for types with a fixed size known at compile time, as oppose to types (such as slices) that are dynamically sized. |
Clone | Types that support cloning values. |
Copy | Marker trait for types that can be cloned simply by making a byte-for-byte copy of the memory containing the value. |
Deref , DerefMut | Traits for smart pointer types. |
Default | Types that have a sensible "default value". |
AsRef , AsMut | Conversion traits for borrowing one type of reference from another. |
Borrow , BorrowMut | Conversion traits like AsRef and AsMut that additionally guarantee consistent hashing, ordering, and equality. |
From , Into | Conversion traits for transforming one type of value into another. |
ToOwned | Conversion trait for converting a reference to an owned value. |
Expression | Notes |
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. |
Expression | Notes |
1..10 | Endpoints must be an integer type to be iterable. Range includes start value, and excludes end value. |
Expression | Notes |
1.. | Unbounded iteration. Start must be an integer. May panic or overflow if the value reaches the limit of the type. |
Expression | Notes |
Some(10).iter() | Behaves like a vector whose length is either 0 (None ) or 1 (Some(v) ). |
Expression | Notes |
Ok("blah").iter() | Similar to Option , producing Ok values. |
Expression | Notes |
std::fs::read_dir(path) | Produces directory entries. |
Expression | Notes |
listener.incoming() | Produces incoming network connections. |
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.
Unix | Function | Returns |
mkdir | create_dir(path) | Result<()> |
mkdir -p | create_dir_all(path) | Result<()> |
rmdir | remove_dir(path) | Result<()> |
rm -r | remove_dir_all(path) | Result<()> |
unlink | remove_file(path) | Result<()> |
Unix | Function | Returns |
cp -p | copy(src_path, dest_path) | Result<u64> |
rename | rename(src_path, dest_path | Result<()> |
link | hard_link(src_path, dest_path) | Result<()> |
Unix | Function | Returns |
realpath | canonicalize(path) | Result<PathBuf> |
stat | metadata(path) | Result<Metadata> |
lstat | symlink_metadata(path) | Result<Metadata> |
ls | read_dir(path) | Result<ReadDir> |
readlink | read_link(path) | Result<PathBuf> |
Unix | Function | Returns |
chmod | set_permissions(path, perm) | Result<()> |