Expand description
§Rustica
Rustica is a comprehensive Rust library that provides functional programming abstractions and utilities, enabling clean, composable, and type-safe code.
§Overview
Functional programming emphasizes immutable data, first-class functions, and composable abstractions. Rustica brings these concepts to Rust with a focus on pragmatism and performance, providing:
- Type-safe functional abstractions like
Functor
,Applicative
, andMonad
- Practical data types such as
Maybe
,Either
, andValidated
- Optics for data manipulation via
Lens
andPrism
- Composable operations for error handling and data transformation
- Zero-cost abstractions wherever possible
- Advanced monad transformers:
StateT
,ReaderT
,WriterT
,ContT
§Getting Started
Add Rustica to your Cargo.toml
:
[dependencies]
rustica = "0.7"
Import common traits and types through the prelude:
use rustica::prelude::*;
§Examples
§Basic Functor Usage
use rustica::prelude::*;
// Using fmap with Option
let value: Option<i32> = Some(42);
let doubled: Option<i32> = value.fmap(|x| x * 2);
assert_eq!(doubled, Some(84));
// With Either for error handling
let success: Either<String, i32> = Either::right(42);
let mapped: Either<String, String> = success.fmap(|n| n.to_string());
assert_eq!(mapped.unwrap_right(), "42");
§Error Handling with Validated
use rustica::datatypes::validated::Validated;
use rustica::traits::applicative::Applicative;
use rustica::traits::functor::Functor;
fn validate_name(name: &str) -> Validated<Vec<String>, String> {
if name.len() >= 2 {
Validated::valid(name.to_string())
} else {
Validated::invalid(vec!["Name too short".to_string()])
}
}
fn validate_email(email: &str) -> Validated<Vec<String>, String> {
if email.contains('@') {
Validated::valid(email.to_string())
} else {
Validated::invalid(vec!["Invalid email format".to_string()])
}
}
// Collect all validation errors
let name = validate_name("A");
let email = validate_email("invalid-email");
// Combine validations and format the result only when both are valid
let format_user = |n: &String, e: &String| format!("User: {}, Email: {}", n, e);
let combined = Validated::lift2(&name, &email, format_user);
assert!(combined.is_invalid());
assert_eq!(combined.unwrap_invalid().len(), 2); // Both errors are collected
§Feature Flags
Rustica provides several feature flags to customize the library for your needs:
full
: Enables all features belowpvec
: Includes persistent vector implementation
§Structure
The library is organized into the following main components:
traits
: Fundamental traits for functional programming conceptsdatatypes
: Implementations of various functional data typestransformers
: Monad transformers and related utilitiesutils
: Utility functions and helpers for common operationsprelude
: A convenient module that re-exports commonly used itemspvec
: Persistent vector implementation (feature-gated)
Modules§
- datatypes
- Implementations of functional data types.
- prelude
- Convenient re-exports of commonly used items.
- pvec
- Persistent vector implementation with structural sharing.
- traits
- Core traits for functional programming abstractions.
- transformers
- Monad transformers and related utilities.
- utils
- Utility functions and helpers for common operations.
Macros§
- pvec
- Creates a new
PersistentVector
with the provided elements.