Commonly used Rust Data Structures and Concepts from the Standard Library
Notes preparing for the coding interview using rustÂ
(data structures and concepts commonly seen from rust standard library)
Vectors (Vec): Vectors are dynamic arrays that can grow or shrink in size as needed. They are commonly used to store collections of values.
Hash maps (HashMap): Hash maps are associative arrays that map keys to values. They are commonly used to store key-value pairs and to implement caches and dictionaries.
Strings (String and str): Strings are sequences of characters. They are commonly used to represent text data.
Iterators (Iterator): Iterators are objects that produce a sequence of values, one at a time. They are commonly used to iterate over collections of values and to perform operations such as filtering, mapping, and reducing.
Collections: In rust, a collection is a data structure that contains multiple values. Some common collection types in Rust include Vec, HashMap, HashSet, BTreeMap, and BTreeSet. These types are part of the standard library and provide a wide range of functionality for storing, organizing, and manipulating collectons of values.
Collections are widely used in coding interviews to solve problems that involve grouping, counting, sorting, or searching for values. Understanding how to use collections effectively is an important skill for solving coding interview challenges in Rust.
Tuples ((T1, T2, ..., Tn)): Tuples are fixed-size collections of values of different types. They are commonly used to group related values together.
Result (Result): The Result type represents the result of a computation that may succeed or fail with an error. It is commonly used for error handling.
Option (Option): The Option type represents an optional value that may or may not be present. It is commonly used to handle nullable values and to represent the result of computations that may fail.
Slices ([T]): Slices are dynamically-sized views into contiguous sequences of values. They are commonly used to represent arrays or parts of arrays.
Vectors (Vec):
.push(): Adds an element to the end of the vector.
.pop(): Removes the last element from the vector and returns it.
.insert(): Inserts an element at a specific position in the vector.
.remove(): Removes an element at a specific position in the vector and returns it.
.len(): Returns the number of elements in the vector.
.is_empty(): Returns true if the vector is empty, false otherwise.
.get(): Returns a reference to an element at a specific position in the vector, or None if the position is out of bounds.
Hash maps (HashMap):
.insert(): Inserts a key-value pair into the hash map.
.remove(): Removes a key-value pair from the hash map and returns its value, or None if the key is not present.
.get(): Returns a reference to the value associated with a key, or None if the key is not present.
.contains_key(): Returns true if the hash map contains a key-value pair with the specified key, false otherwise.
.len(): Returns the number of key-value pairs in the hash map.
.is_empty(): Returns true if the hash map is empty, false otherwise.
Iterators (Iterator):
.next(): Returns the next value in the iterator, or None if there are no more values.
.map(): Applies a function to each value in the iterator and returns a new iterator over the resulting values.
.filter(): Filters values from the iterator based on a predicate and returns a new iterator over the remaining values.
.fold(): Reduces the iterator to a single value by applying a binary operation to each value and an accumulator.
.collect(): Collects all values from the iterator into a collection.
Strings (String and str):
.push_str(): Appends a string slice to a string.
.pop(): Removes the last character from a string and returns it, or None if the string is empty.
.len(): Returns the length of a string in bytes.
.is_empty(): Returns true if a string is empty, false otherwise.
.contains(): Returns true if a string contains a specified pattern, false otherwise.
5. Slices ([T]):
.len(): Returns the number of elements in a slice.
.is_empty(): Returns true if a slice is empty, false otherwise.
.first(): Returns a reference to the first element of a slice, or None if it is empty.
.last(): Returns a reference to the last element of a slice, or None if it is empty.
.split_at(): Splits a slice into two subslices at a specific position.
6. Tuples ((T1, T2, ..., Tn)): Tuples do not have any inherent methods. Instead, you can use pattern matching to extract their elements or access them directly using dot notation (e.g., tuple.0, tuple.1, etc.).
7. Option (Option):
.is_some(): Returns true if an option contains some value, false otherwise. -.is_none() : Returns true if an option contains no value, false otherwise -.map(): Maps an Option<T> to Option<U> by applying function to contained value -.and_then(): Calls function with wrapped value and returns result -.unwrap_or(): Unwraps an option yielding content of Some or default value
8. Result (Result): -.is_ok():Returns true if result is Ok variant -.is_err():Returns true if result is Err variant -.map():Maps Result<T,E> to Result<U,E> by applying function to contained Ok value leaving Err value untouched -.map_err():Maps Result<T,E> to Result<T,F> by applying function to contained Err value leaving Ok value untouched -.unwrap_or():Unwraps result yielding content of Ok or default value
9. Collections: Collections are data structures that contain multiple values. Some common collection types include Vec, HashMap, HashSet, BTreeMap and BTreeSet. Each collection type has its own set of methods for adding, removing, and accessing elements. Some common methods include .len(), .is_empty(), .insert(), .remove(), and .get().