Collection of immutable and persistent data structures written in Rust, inspired by the standard libraries found in Haskell, Closure and OCaml

Overview

PRust: (P)ersistent & Immutable Data Structures in (Rust)

This library houses a collection of immutable and persistent data structures, inspired by the standard libraries found in Haskell, OCaml, Closure and Okasaki's Purely Functional Data Structures book.

It does NOT contain:

  • Unsafe memory access (no unsafe use)
  • Methods taking mutable references
  • External dependencies

What's Prust Good For?

Persistent Data Structures: Ever wanted to undo an operation on a data structure? With persistent data structures, you can access prior versions, essentially maintaining a history of change made as needed.

// Insert words
let mut trie = Trie::empty().insert("apple").insert("app").insert("banana");

// Snapshot the current trie. This operation is lightweight, allocating only a couple of bytes long.
let snapshot = trie.clone();

// Insert more words
trie = trie.insert("grape").insert("banana-split");

// Check for words in current trie
assert_eq!(trie.search("grape"), true);

// Restore trie to a previous of moment in time
trie = snapshot;

// Word was not present at snapshop moment
assert_eq!(trie.search("grape"), false);

Immutable Data Structures: Data structures, once created, do not change. Instead of modifying, a new "view" is created.

// deque: [2, 1]
let deque: Deque<i32> = Deque::empty().push_front(1).push_front(2);

// Even with pop_back, deque is still unaltered, since it's immutable
let (value, _) = deque.pop_back().unwrap();
assert_eq!(*value, 1);
assert_eq!(deque.length(), 2);

// The "new" deque with pop_back is returned as part of the call
let (value, deque_updated) = deque.pop_front().unwrap();
assert_eq!(*value, 2);
assert_eq!(deque_updated.length(), 1);

Multithreading Friendliness: Thanks to their immutability, Prust's data structures are inherently thread-safe. See more on section below.

Avaliable data structures

  • Trie (aka Prefix Tree)
  • Hash Map / Hash Set (based on Trie)
  • AVL tree
  • Ordered Map / Ordered Set (based on AVL)
  • Stack (aka Cons List)
  • Deque

Thread Safety

For performance reasons, thread safety is opt in. To enable the thread_safe feature, add the following to your Cargo.toml:

[dependencies.prust_lib]
version = "version"
features = ["thread_safe"]

This switches the reference counting from std::rc::Rc to std::sync::Arc.

How Does Prust Work?

Instead of in-place updates, whenever a mutable-like operation is invoked (e.g., adding a value to a set), Prust returns a "copy" of the new updated structure, leaving the original untouched. This ensures both persistence (by retaining prior versions) and immutability (since the original remains unchanged).

This is acomplished by reusing most parts of the original structure and only building new pieces relevant to the updated version.

A colorary is that cloning these structures is efficient as they are usually represented by only a few pointers.

You might also like...
📦 A Python package manager written in Rust inspired by Cargo.
📦 A Python package manager written in Rust inspired by Cargo.

huak About A Python package manager written in Rust. The Cargo for Python. ⚠️ Disclaimer: huak is currently in its proof-of-concept (PoC) phase. Huak

📦 A Python package manager written in Rust inspired by Cargo.
📦 A Python package manager written in Rust inspired by Cargo.

huak About A Python package manager written in Rust. The Cargo for Python. ⚠️ Disclaimer: huak is currently in its Alpha phase. Huak aims to support a

A utility written in Rust for dumping binary information out of Mach-O files inspired by objdump

Mach-O Dump (macho-dump) An objdump like tool for exploring and manipulating Mach-O files. Note: This project is in an early stage and most of the fea

Markdown to HTML converter written in Rust. Inspired by Katsuki Yuri's Makudaun Tool.
Markdown to HTML converter written in Rust. Inspired by Katsuki Yuri's Makudaun Tool.

Makurust Makurust is a powerful tool written in Rust that allows you to effortlessly convert your Markdown files into static HTML pages. Inspired by T

Interface definition generator and standard for Move packages.

Move IDL Interface definition generator and standard for Move packages. Documentation is currently extremely sparse but will be improved in the near f

epNFTs, the first partial program-owned NFT Standard powered by Instruction Introspection and Transfer Hooks!

epNFT Standard: A Comprehensive Guide Introduction to epNFTs Welcome to the epNFT-standard repository, where we explore the first program-owned NFT st

A dark and light Neovim theme written in fennel, inspired by IBM Carbon.
A dark and light Neovim theme written in fennel, inspired by IBM Carbon.

oxocarbon.nvim Note: The old rust version can be found on the rust branch of this repository Oxocarbon is looking for ports! If you're a user of anoth

Work-in-progress Rust application that converts C++ header-only libraries to single self-contained headers.

unosolo Work-in-progress Rust application that converts C++ header-only libraries to single self-contained headers. Disclaimer This is my first Rust p

Shared Rust libraries for Hyperledger Indy.

indy-shared-rs Shared Rust libraries for Hyperledger Indy. indy-credx: Indy verifiable credential issuance and presentation (aka Anoncreds) indy-data-

Owner
Victor Colombo
Victor Colombo
Safe OCaml-Rust Foreign Function Interface

ocaml-rust This repo contains code for a proof of concept for a safe OCaml-Rust interop inspired by cxx. This is mostly optimized for calling Rust cod

Laurent Mazare 23 Dec 27, 2022
Immutable strings, in Rust.

Immutable Strings Inspired by the bytes crate, which offers zero-copy byte slices, this crate does the same but for strings. It is backed by standard

Patrick Elsen 202 Apr 6, 2023
Simple calculator REPL, similar to bc(1), with syntax highlighting and persistent history

eva simple calculator REPL, similar to bc(1), with syntax highlighting and persistent history installation Homebrew $ brew install eva crates.io $ car

Akshay 632 Jan 4, 2023
Semi-persistent, scoped test directories

Semi-persistent, scoped test directories This crate aims to make it easier to use temporary directories in tests, primarily by calling the testdir!()

Floris Bruynooghe 4 Nov 19, 2022
My solutions for the Advent of Code 2021 in Scala, Python, Haskell and Rust.

Advent of Code 2021 These are my Advent of Code 2021 solutions written in Scala 3, Haskell, Python and Rust. Day Title L1 L2 L3 L4 01 Sonar Sweep Scal

Axel Suárez 2 Oct 14, 2022
Requestty - An easy-to-use collection of interactive cli prompts inspired by Inquirer.js.

Requestty requestty (request-tty) is an easy-to-use collection of interactive cli prompts inspired by Inquirer.js. Easy-to-use - The builder API and m

null 160 Dec 28, 2022
Bevy Meshem is a Rust crate designed to provide meshing algorithms for voxel grids, enabling you to create cohesive 3D mesh structures from a grid of cubic voxels

Bevy Meshem Crates.io, docs Bevy Compatibility: Bevy Version bevy_meshem 0.11 main Bevy Meshem is a Rust crate designed to provide meshing algorithms

Adam 4 Aug 16, 2023
Generate and translate standard uuids into shorter formats and back.

short-uuid Generate and translate standard UUIDs into shorter or just different formats and back. A port of the JavaScript npm package short-uuid so b

Radim Höfer 3 Feb 28, 2024
A Rust program/library to write a Hello World message to the standard output.

hello-world Description hello-world is a Rust program and library that prints the line Hello, world! to the console. Why was this created? This progra

null 0 May 11, 2022
Expose standard or fully custom USB peripherals (gadgets) through a USB device controller (UDC) on Linux using Rust.

usb-gadget This library allows implementation of USB peripherals, so called USB gadgets, on Linux devices that have a USB device controller (UDC). Bot

Sebastian Urban 21 Oct 30, 2023