| Crates.io | rustica |
| lib.rs | rustica |
| version | 0.11.1 |
| created_at | 2025-02-04 14:29:41.662843+00 |
| updated_at | 2025-12-17 15:15:42.261811+00 |
| description | Rustica is a functional programming library for the Rust language. |
| homepage | |
| repository | https://github.com/but212/rustica |
| max_upload_size | |
| id | 1542005 |
| size | 2,190,986 |
Rustica is a comprehensive functional programming library for Rust, bringing powerful abstractions from category theory and functional programming to the Rust ecosystem. It provides a rich set of type classes, data types, and utilities commonly found in functional programming languages.
Rustica enables idiomatic functional programming in Rust by providing:
Functor, Applicative, and MonadMaybe, Either, Choice, and IOStateT, ReaderT, and moreExcellent for:
Avoid for:
Whether you're coming from Haskell, Scala, or other functional languages, or just want to explore functional programming in Rust, Rustica provides the tools you need for learning and experimentation.
Add Rustica to your Cargo.toml:
[dependencies]
rustica = "0.11.1"
If you want to use async features, add the async feature:
[dependencies]
rustica = { version = "0.11.1", features = ["async"] }
Persistent vector collections are included by default. The full feature enables async and serde support.
You can combine multiple features as needed:
[dependencies]
rustica = { version = "0.11.1", features = ["full"] }
Then import the prelude to get started:
use rustica::prelude::*;
Rustica implements a wide range of type classes from category theory:
Basic Abstractions
Functor - For mapping over contained valuesApplicative - For applying functions in a contextMonad - For sequential computationsPure - For lifting values into a contextAlternative - For choice between computationsAlgebraic Structures
Semigroup - Types with an associative binary operationMonoid - Semigroups with an identity elementFoldable - For reducing structuresTraversable - For structure-preserving transformationsAdvanced Concepts
Bifunctor - For mapping over two type parametersContravariant - For reversing function applicationCategory - For abstract compositionArrow - For generalized computationComonad - For context-aware computationsMonadError - For error handling in monadic contextsRustica provides a rich collection of functional data types:
Core Types
Maybe<T> - For optional values (like Option<T>)Either<L, R> - For values with two possibilitiesId<T> - The identity monadValidated<E, T> - For accumulating validation errorsChoice<T> - For representing non-deterministic computations with alternativesEffect Types
IO<A> - For pure I/O operationsState<S, A> - For stateful computations with thread-safe implementationsReader<E, A> - For environment-based computationsWriter<W, A> - For logging operationsCont<R, A> - For continuation-based programmingAsyncM<A> - For asynchronous operations (requires async feature)Special Purpose
First, Last, Min, Max, etc.)Persistent Collections
PersistentVector<T> - An efficient immutable vector with structural sharing and small vector optimizationTransformers
StateT<S, M, A> - State monad transformer for combining state with other effectsReaderT<E, M, A> - Reader monad transformer for combining environment with other effectsOptics
Lens - For focusing on parts of structuresPrism - For working with sum typesIsoLens - Lawful, composable lenses based on isomorphisms for deep focusingIsoPrism - Lawful, composable prisms based on isomorphisms for sum typesRustica provides standardized error handling utilities that work across different functional types:
Core Functions
sequence - Combines a collection of Result values into a single Result containing a collectiontraverse - Applies a function that produces a Result to a collection, returning a single Resulttraverse_validated - Like traverse but collects all errors instead of failing fastType Conversion
ResultExt trait - Extends Result with methods like to_validated() and to_either()WithError trait - Generic trait for any type that can represent error statesResult, Either, and ValidatedError Types
ComposableError<E> - A structured error type that accumulates contextErrorPipeline - Functional error handling pipelineswith_context() and format_error_chain()Rustica provides an immutable persistent vector (RRB-Tree) for functional programming patterns.
Example Usage
use rustica::pvec::PersistentVector;
use rustica::pvec::pvec;
let v1: PersistentVector<i32> = pvec![1, 2, 3, 4, 5];
let v2 = v1.push_back(6);
let v3 = v1.update(0, 10);
assert_eq!(v1.get(0), Some(&1));
assert_eq!(v2.get(5), Some(&6));
assert_eq!(v3.get(0), Some(&10));
Rustica uses GitHub Actions for continuous integration, formatting, linting, and automated publishing to crates.io on tagged releases.
v0.11.1) is pushed, the version is checked and, if not already published, is automatically uploaded to crates.io.See CHANGELOG.md for a complete list of recent changes and enhancements.
use rustica::prelude::*;
// Working with Maybe (like Option)
let maybe_value = Maybe::Just(42);
let doubled = maybe_value.fmap(|x| x * 2);
assert_eq!(doubled.unwrap(), 84);
// Working with Either for error handling
let result: Either<String, &str> = Either::Right("success");
let processed = result.fmap(|s| s.to_uppercase());
assert_eq!(processed.unwrap(), "SUCCESS");
// Using Choice for multiple alternatives
let choices = Choice::new(1, [2, 3]);
let results = choices.fmap(|x| x * 2);
assert_eq!(results.iter().collect::<Vec<_>>(), vec![&2, &4, &6]);
use rustica::datatypes::state::State;
// A simple counter
let counter = State::new(|count: i32| (count + 1, count));
// Run the state computation
let (new_count, result) = counter.run_state(0);
assert_eq!(new_count, 1);
assert_eq!(result, 0);
use rustica::datatypes::io::IO;
// Pure IO description
let read_line = IO::new(|| "Hello from IO!".to_string());
// Execute the IO operation
let result = read_line.run();
assert_eq!(result, "Hello from IO!");
Rustica is inspired by functional programming libraries in other languages:
Rustica is licensed under the Apache License, version 2.0. See the LICENSE file for details.
For detailed documentation, please visit docs.rs/rustica