Loading...
Preparing your coding drills
Comprehensive reference for 31 Rust methods across 7 categories.
Vec::push
Appends an element to the back of the vector. Panics if the number of elements overflows a usize.
Vec::pop
Removes the last element from the vector and returns it wrapped in Some, or None if the vector is empty.
Vec::len
Returns the number of elements in the vector, also referred to as its length.
Vec::iter
Returns an iterator over immutable references to each element of the vector. The vector cannot be modified during iteration.
Vec::sort
Sorts the vector in-place in ascending order. This sort is stable (preserves the order of equal elements) and uses a modified merge sort.
Vec::contains
Returns true if the vector contains an element equal to the given value. Uses linear search.
String::from
Creates a new heap-allocated String from a string slice (&str). This is one of the most common ways to create owned strings in Rust.
str::split
Returns an iterator over substrings of the string slice, separated by characters matched by a pattern.
str::trim
Returns a string slice with leading and trailing whitespace removed. Does not allocate a new string.
str::replace
Replaces all matches of a pattern with another string and returns a new String.
str::to_uppercase
Returns the uppercase equivalent of this string slice as a new String, according to Unicode rules.
Iterator::map
Takes a closure and creates an iterator that calls that closure on each element. The map iterator is lazy and does nothing until consumed.
Iterator::filter
Creates an iterator that yields only the elements for which the closure returns true. The closure receives a reference to each element.
Iterator::collect
Consumes the iterator and collects the resulting values into a collection. The target type can often be inferred by the compiler.
Iterator::fold
Applies a closure to each element, threading an accumulator value through the computation. Returns the final accumulated value.
Iterator::enumerate
Creates an iterator that yields pairs of (index, element), where the index starts at 0.
Iterator::zip
Zips two iterators into a single iterator of pairs. The resulting iterator ends when either input iterator is exhausted.
HashMap::insert
Inserts a key-value pair into the map. If the map already contained a value for this key, the old value is returned wrapped in Some.
HashMap::get
Returns a reference to the value corresponding to the key, or None if the key is not present.
HashMap::remove
Removes a key from the map, returning the stored value if the key was previously in the map.
HashMap::contains_key
Returns true if the map contains a value for the specified key.
Option::unwrap
Returns the contained Some value. Panics if the value is None with a generic panic message.
Option::unwrap_or
Returns the contained Some value, or a provided default value if the Option is None.
Option::map
Maps an Option<T> to Option<U> by applying a function to the contained value (if Some), or returns None (if None).
Option::and_then
Returns None if the Option is None, otherwise calls the closure with the wrapped value and returns the result. Also known as flatmap in other languages.
Result::unwrap
Returns the contained Ok value. Panics if the value is an Err, with the Err value as the panic message.
Result::map
Maps a Result<T, E> to Result<U, E> by applying a function to the contained Ok value, leaving an Err value untouched.
Result::and_then
Calls the closure if the Result is Ok, otherwise returns the Err value unchanged. Useful for chaining operations that can each fail.
slice::binary_search
Binary searches a sorted slice for a given element. Returns Ok(index) if found, Err(index) if not found where index is where the element could be inserted to maintain sort order.
slice::chunks
Returns an iterator over consecutive non-overlapping sub-slices of the given chunk size. The last chunk may have fewer elements if the slice length is not evenly divisible.
slice::windows
Returns an iterator over all contiguous windows (overlapping sub-slices) of a given length. The windows overlap by window_size - 1 elements.