Logo
Logo

Atharva Pandey/Rust Ultimate: The Ultimate Rust Cheatsheet You'll Ever Need

Created Tue, 20 Feb 2024 18:30:00 +0000 Modified Tue, 20 Feb 2024 18:30:00 +0000


Whether you’re a seasoned developer juggling multiple programming languages or a newcomer to Rust, it’s not uncommon to hit a roadblock trying to recall specific syntax or optimize your code with Rust’s powerful features. This comprehensive Rust cheatsheet is designed to be your go-to reference, enabling you to harness Rust’s capabilities fully without getting slowed down by syntax uncertainties.

Vector & LinkedList Operations

Rust’s Vec<T> and LinkedList<T> are versatile for handling collections. Here’s how to use them effectively:

| Data Structure | Method        | Time Complexity | Space Complexity | Description                                          |
|----------------|---------------|-----------------|------------------|------------------------------------------------------|
| `Vec<T>`       | `new()`       | O(1)            | O(1)             | Creates a new empty vector.                          |
|                | `push(value)` | Amortized O(1)  | O(1)             | Adds an element to the end of the vector.            |
|                | `pop()`       | O(1)            | O(1)             | Removes the last element from the vector and returns it. |
|                | `get(index)`  | O(1)            | O(1)             | Returns a reference to the element at the specified index. |

LinkedList<T> & VecDeque<T> Operations

These structures are ideal for scenarios requiring dynamic insertions and deletions.

| Data Structure    | Method              | Time Complexity  | Space Complexity | Description                                      |
|-------------------|---------------------|-------------------|------------------|--------------------------------------------------|
| `LinkedList<T>`   | `new()`             | O(1)              | O(1)             | Creates a new empty linked list.                 |
|                   | `push_front(value)` | O(1)              | O(1)             | Inserts an element at the beginning of the list. |
|                   | `push_back(value)`  | O(1)              | O(1)             | Inserts an element at the end of the list.       |
|                   | `pop_front()`       | O(1)              | O(1)             | Removes and returns the first element of the list. |
|                   | `pop_back()`        | O(1)              | O(1)             | Removes and returns the last element of the list. |

Mastering HashMap and HashSet in Rust

Efficient data lookup and storage are crucial, and Rust’s HashMap<K, V> and HashSet<T> are up to the task:

HashMap Operations

| Data Structure | Method        | Time Complexity | Space Complexity | Description                                 |
|----------------|---------------|-----------------|------------------|---------------------------------------------|
| `HashMap<K,V>` | `new()`       | O(1)            | O(1)             | Creates a new empty hash map.               |
|                | `insert(k,v)` | Average O(1)    | O(1)             | Inserts a key-value pair into the map.      |
|                | `get(&k)`     | Average O(1)    | O(1)             | Returns a reference to the value corresponding to the key. |
|                | `remove(&k)`  | Average O(1)    | O(1)             | Removes a key from the map and returns its value. |

HashSet Operations

| Data Structure | Method            | Time Complexity | Space Complexity | Description                              |
|----------------|-------------------|-----------------|------------------|------------------------------------------|
| `HashSet<T>`   | `new()`           | O(1)            | O(1)             | Creates a new empty hash set.            |
|                | `insert(value)`   | Average O(1)    | O(1)             | Adds a value to the set.                 |
|                | `contains(&value)`| Average O(1)    | O(1)             | Checks if the set contains a value.      |
|                | `remove(&value)`  | Average O(1)    | O(1)             | Removes a value from the set.            |

String and Iterator Manipulations

Handling strings and iterators efficiently is a cornerstone of Rust programming:

String Operations

| Data Structure | Method            | Time Complexity | Space Complexity | Description                                      |
|----------------|-------------------|-----------------|------------------|--------------------------------------------------|
| `String`       | `new()`           | O(1)            | O(1)             | Creates a new empty string.                      |
|                | `push_str(s)`     | Amortized O(1)  | O(1)             | Appends a string slice to the end of the string. |
|                | `pop()`           | O(1)            | O(1)             | Removes the last character from the string and returns it. |
|                | `trim()`          | O(n)            | O(1)             | Returns a string slice with leading and trailing whitespace removed. |
|                | `split(delimiter)`| O(n)            | O(1)             | Returns an iterator over substrings of the string, split by the given delimiter. |

Iterator Operations

| Data Structure | Method          | Time Complexity | Space Complexity | Description                               |
|----------------|-----------------|-----------------|------------------|-------------------------------------------|
| Iterators      | `map(f)`        | Lazy            | O(1)             | Applies function `f` to each element.     |
|                | `filter(p)`     | Lazy            | O(1)             | Filters elements based on predicate `p`.  |
|                | `fold(init, f)` | O(n)            | O(1)             | Reduces the iterator to a single value.   |
|                | `collect()`     | O(n)            | O(n)             | Transforms the iterator into a collection.|
|                | `zip(it)`       | Lazy            | O(1)             | Zips two iterators into a single iterator of pairs. |
|                | `chain(it)`     | Lazy            | O(1)             | Chains two iterators into a single iterator. |

Advanced Rust: Concurrency, Asynchronous Programming, and More

Rust’s safe concurrency and async programming capabilities set it apart:

Concurrency Primitives

| Data Structure     | Method            | Time Complexity | Space Complexity | Description                                    |
|--------------------|-------------------|-----------------|------------------|------------------------------------------------|
| `Thread`           | `spawn(f)`        | O(1)            | O(1)             | Creates a new thread of execution.             |
| `Mutex<T>`         | `lock()`          | O(1)            | O(1)             | Locks the mutex, blocking until available.     |
| `Arc<T>`           | `new(data)`       | O(1)            | O(1)             | Creates a new atomic reference-counted pointer.|
| `mpsc::Sender<T>`  | `send(value)`     | O(1)            | O(1)             | Sends a value to the receiver.                 |
| `mpsc::Receiver<T>`| `recv()`          | O(1)            | O(1)             | Blocks until a value is received.              |

Asynchronous Programming

| Data Structure / Trait | Method           | Time Complexity | Space Complexity | Description                         |
|------------------------|------------------|-----------------|------------------|-------------------------------------|
| `std::future::Future`  | `await`          | O(1)            | O(1)             | Awaits the completion of an async operation. |
| `std::task::Poll`      | Used in `poll`   | O(1)            | O(1)             | Represents the return value of `Future::poll`. |

File I/O and System Operations

Rust simplifies file and system operations with its comprehensive standard library:

File Operations

| Data Structure | Method          | Time Complexity | Space Complexity | Description                           |
|----------------|-----------------|-----------------|------------------|---------------------------------------|
| `File`         | `open(path)`    | O(1)*           | O(1)             | Opens a file in read-only mode.       |
|                | `create(path)`  | O(1)*           | O(1)             | Creates a new file, truncating existing one. |
|                | `read(&mut buf)`| O(n)*           | O(1)             | Reads bytes into the buffer.          |
|                | `write(&buf)`   | O(n)*           | O(1)             | Writes buffer's bytes to the file.    |

FileSystem Operations

| Data Structure / Trait | Method                  | Time Complexity | Space Complexity | Description                      |
|------------------------|-------------------------|-----------------|------------------|----------------------------------|
| `std::fs::File`        | `create(path)`          | O(1)*           | O(1)             | Creates or truncates a file.     |
| `std::fs::DirBuilder`  | `create(path)`          | O(1)*           | O(1)             | Creates a directory.             |
| `std::fs`              | `read_dir(path)`        | O(1)*           | O(1)             | Reads the contents of a directory. |
|                        | `remove_file(path)`     | O(1)*           | O(1)             | Removes a file.                  |
|                        | `copy(source, dest)`    | O(n)*           | O(1)             | Copies a file from source to destination. |

Deep Dive into Advanced Topics

Rust isn’t just about the basics. Dive into advanced numerics, error handling, meta-programming, and more to truly master the language:

Advanced Numerics and Math

| Data Structure / Trait | Method | Time Complexity | Space Complexity | Description |
| ------------------------| -----------------------| -----------------| ------------------| ------------------------------------|
| `std::cmp::Ord` | `cmp(&self, other)` | O(1) | O(1) | Compares two values.               |
| `std::iter::Sum` | `sum<I: Iterator>` | O(n) | O(1) | Sums items in an iterator.         |
| `std::iter::Product` | `product<I: Iterator>` | O(n) | O(1) | Multiplies items in an iterator.   |

Error Handling and Debugging

Rust provides robust tools for error handling and debugging, ensuring your code is not only efficient but also reliable:
\

| Feature / Trait       | Method / Use Case     | Time Complexity | Space Complexity | Description                       |
|-----------------------|-----------------------|-----------------|------------------|-----------------------------------|
| `std::result::Result` | `map_err(f)`          | O(1)            | O(1)             | Maps an `Err` value using a function. |
| `std::option::Option` | `and_then(f)`         | O(1)            | O(1)             | Calls a function on an `Option` value or returns `None`. |
| `std::panic`          | `panic!("msg")`       | O(1)            | O(1)             | Triggers a panic with a custom message. |
| `std::dbg!`           | `dbg!(&val)`          | O(n)            | O(1)             | Prints and returns the value for debugging. |
| `std::error::Error`   | `description()`       | O(1)            | O(1)             | Provides a description of the error. |

Conclusion: Your Journey with Rust

This cheatsheet is a gateway to exploring Rust’s depth, from fundamental data structures and concurrency to advanced system operations and asynchronous programming. Rust is a language that continues to evolve, supported by an innovative community. This guide is a solid foundation, but the true exploration begins with your projects and contributions.

Contribute and Grow

I welcome contributions to this cheatsheet. If you have suggestions, additional tips, or new Rust features to include, please share. Your input is invaluable in making this resource better for everyone in our Rust journey. Let’s collaborate to keep this cheatsheet comprehensive and up-to-date, supporting Rustaceans at all levels.