Introduction

Rustica brings powerful abstractions from category theory and functional programming to the Rust ecosystem, providing a rich set of type classes, data types, and utilities commonly found in functional programming languages.

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 composable, type-safe code.

Getting Started

Add Rustica to your Cargo.toml:

[dependencies]
rustica = "0.7.1"

Import the prelude to get started:

use rustica::prelude::*;

Quick Example: Transform data with functional style

let value = Maybe::just(42);
let doubled = value.fmap(|x| x * 2);
assert_eq!(doubled, Maybe::just(84));

Key Features

Type Classes

Core abstractions like Functor, Applicative, and Monad

Data Types

Functional data structures: Maybe, Either, Validated, and more

Transformers

Monad transformers: StateT, ReaderT, WriterT

Error Handling

Composable error handling with Validated and Either

Immutable Data

Persistent data structures with structural sharing

Examples

Error Handling with Validated

Collect multiple validation errors:

let name = validate_name("A");           // Invalid
let email = validate_email("invalid");   // Invalid

let format_user = |n: &String, e: &String| {
    format!("User: {}, Email: {}", n, e)
};

let result = Validated::lift2(&name, &email, format_user);
assert!(result.is_invalid());
assert_eq!(result.unwrap_invalid().len(), 2); // Both errors collected

Composing Operations

Chain operations with monadic bind:

let result: Either<&str, i32> = Either::right(5)
    .bind(|x| Either::right(x + 1))
    .bind(|x| Either::right(x * 2));

assert_eq!(result, Either::right(12));

Feature Flags