Skip to content

Instantly share code, notes, and snippets.

@damienstanton
Last active March 7, 2021 21:04
Show Gist options
  • Save damienstanton/bf9ba93d65ac7c6b49e18c80c634ab8a to your computer and use it in GitHub Desktop.
Save damienstanton/bf9ba93d65ac7c6b49e18c80c634ab8a to your computer and use it in GitHub Desktop.
Rust Wrapper Type Reference

Note that this is meant to be a common subset of useful types, not an exhaustive list.

prelude

  • Box<T>, casually referred to as a 'box', provides the simplest form of heap allocation in Rust. Boxes provide ownership for this allocation, and drop their contents when they go out of scope. Boxes also ensure that they never allocate more than isize::MAX bytes.

std::sync

  • Arc: Atomically Reference-Counted pointer, which can be used in multithreaded environments to prolong the lifetime of some data until all the threads have finished using it.
  • Barrier: Ensures multiple threads will wait for each other to reach a point in the program, before continuing execution all together.
  • Condvar: Condition Variable, providing the ability to block a thread while waiting for an event to occur.
  • mpsc: Multi-producer, single-consumer queues (channels), used for message-based communication. Can provide a lightweight inter-thread synchronisation mechanism, at the cost of some extra memory.
  • Mutex: Mutual Exclusion mechanism, which ensures that at most one thread at a time is able to access some data.
  • Once: Used for thread-safe, one-time initialization of a global variable.
  • RwLock: Provides a mutual exclusion mechanism which allows multiple readers at the same time, while allowing only one writer at a time. In some cases, this can be more efficient than a mutex.

std::cell

  • Cell: A mutable memory location.
  • Ref: Wraps a borrowed reference to a value in a RefCell box. A wrapper type for an immutably borrowed value from a RefCell<T>.
  • RefCell: A mutable memory location with dynamically checked borrow rules
  • RefMut: A wrapper type for a mutably borrowed value from a RefCell<T>.
  • UnsafeCell: The core primitive for interior mutability in Rust.

std::rc

  • Rc A single-threaded reference-counting pointer. 'Rc' stands for 'Reference Counted'.
  • Weak Weak is a version of Rc that holds a non-owning reference to the managed allocation. The allocation is accessed by calling upgrade on the Weak pointer, which returns an Option<Rc<T>>.
// TODO: examples, if needed anywhere
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment