Rust Cheatsheet
48 essential methods for coding interviews
Showing 48 of 48 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
push | vec.push(value) | Appends an element to the back of the Vec | O(1) amortized | essential |
pop | vec.pop() -> Option<T> | Removes and returns the last element, or None if empty | O(1) | essential |
len | vec.len() -> usize | Returns the number of elements in the Vec | O(1) | essential |
is_empty | vec.is_empty() -> bool | Returns true if the Vec has no elements | O(1) | essential |
get | vec.get(index) -> Option<&T> | Returns a reference to the element at index, or None if out of bounds | O(1) | essential |
sort | vec.sort() | Sorts the Vec in ascending order (requires Ord trait) | O(n log n) | essential |
sort_by | vec.sort_by(|a, b| a.cmp(b)) | Sorts with a custom comparator function | O(n log n) | essential |
binary_search | vec.binary_search(&value) -> Result<usize, usize> | Searches for element in sorted slice, returns Ok(index) or Err(insertion_point) | O(log n) | essential |
windows | slice.windows(size) -> impl Iterator | Returns an iterator over overlapping windows of given size | O(n) for full iteration | essential |
chars | string.chars() -> impl Iterator<Item=char> | Returns an iterator over the characters of the string | O(n) for full iteration | essential |
split | string.split(pattern) -> impl Iterator<Item=&str> | Splits string by a pattern and returns an iterator | O(n) | essential |
trim | string.trim() -> &str | Returns string slice with leading and trailing whitespace removed | O(n) | essential |
parse | string.parse::<T>() -> Result<T, ParseError> | Parses string into another type (like i32, f64) | O(n) | essential |
insert | map.insert(key, value) -> Option<V> | Inserts a key-value pair, returns old value if key existed | O(1) average | essential |
get | map.get(&key) -> Option<&V> | Returns a reference to the value for the key, or None | O(1) average | essential |
entry | map.entry(key).or_insert(default) | Gets entry for in-place manipulation, inserting default if absent | O(1) average | essential |
insert (HashSet) | set.insert(value) -> bool | Adds value to set, returns true if it was not present | O(1) average | essential |
contains (HashSet) | set.contains(&value) -> bool | Returns true if the set contains the value | O(1) average | essential |
map (Iterator) | iter.map(|x| transform) -> impl Iterator | Transforms each element using the closure | O(n) | essential |
filter (Iterator) | iter.filter(|x| predicate) -> impl Iterator | Keeps only elements that satisfy the predicate | O(n) | essential |
fold | iter.fold(init, |acc, x| combine) -> T | Reduces iterator to single value using accumulator | O(n) | essential |
collect | iter.collect::<Collection>() -> Collection | Transforms iterator into a collection | O(n) | essential |
enumerate | iter.enumerate() -> impl Iterator<Item=(usize, T)> | Returns iterator yielding (index, value) pairs | O(n) | essential |
find | iter.find(|x| predicate) -> Option<T> | Returns first element satisfying predicate, or None | O(n) | essential |
sum | iter.sum::<T>() -> T | Sums all elements (requires Sum trait) | O(n) | essential |
max / min | iter.max() -> Option<T> | Returns maximum/minimum element | O(n) | essential |
unwrap | option.unwrap() -> T | Returns inner value, panics if None/Err | O(1) | essential |
unwrap_or | option.unwrap_or(default) -> T | Returns inner value or default if None/Err | O(1) | essential |
map (Option) | option.map(|x| transform) -> Option<U> | Transforms the inner value if Some, otherwise returns None | O(1) | essential |
and_then | option.and_then(|x| Some(y)) -> Option<U> | Chains Option-returning operations (flatMap equivalent) | O(1) | essential |
is_some / is_none | option.is_some() -> bool | Returns true if Option contains a value / is empty | O(1) | essential |
clone | value.clone() -> T | Creates a deep copy of the value (requires Clone trait) | O(n) | essential |
iter / iter_mut / into_iter | collection.iter() | iter_mut() | into_iter() | Creates iterators: borrowing, mutable borrowing, or consuming | O(1) to create | essential |
reverse | vec.reverse() | Reverses the order of elements in place | O(n) | common |
contains | slice.contains(&value) -> bool | Returns true if the slice contains the value | O(n) | common |
swap | vec.swap(i, j) | Swaps two elements in the Vec | O(1) | common |
retain | vec.retain(|x| predicate) | Keeps only elements that satisfy the predicate | O(n) | common |
dedup | vec.dedup() | Removes consecutive duplicate elements | O(n) | common |
chunks | slice.chunks(size) -> impl Iterator | Returns an iterator over non-overlapping chunks | O(n) for full iteration | common |
contains_key | map.contains_key(&key) -> bool | Returns true if the map contains the key | O(1) average | common |
remove | map.remove(&key) -> Option<V> | Removes key from map, returning the value if present | O(1) average | common |
intersection | set1.intersection(&set2) -> impl Iterator | Returns an iterator over values in both sets | O(min(n, m)) | common |
zip | iter1.zip(iter2) -> impl Iterator<Item=(A, B)> | Zips two iterators into pairs | O(n) | common |
take | iter.take(n) -> impl Iterator | Returns iterator over first n elements | O(n) | common |
skip | iter.skip(n) -> impl Iterator | Skips first n elements | O(n) | common |
any | iter.any(|x| predicate) -> bool | Returns true if any element satisfies predicate | O(n) | common |
all | iter.all(|x| predicate) -> bool | Returns true if all elements satisfy predicate | O(n) | common |
flatten | iter.flatten() -> impl Iterator | Flattens nested iterators into a single iterator | O(n) | common |