Examples
This section provides practical examples demonstrating various features and patterns of the rsActor framework.
Available Examples
Core Patterns
- Basic Usage — Simple counter actor with tell and ask
- Async Worker — Actor performing periodic async work
- Blocking Task — CPU-intensive work in blocking contexts
- Actor with Timeout — Timeout handling in actor communication
Advanced Patterns
- Kill Demo — Graceful stop vs immediate kill, ActorResult inspection
- Ask Join — ask_join pattern with JoinHandle
- Handler Demo — Handler traits for type-erased heterogeneous collections
- Weak References — ActorWeak for breaking circular references
- Metrics — Per-actor performance metrics
- Tracing — Structured tracing and observability
Classic Problems
- Dining Philosophers — Classic concurrency problem solved with actors
Running Examples
All examples are in the examples/ directory:
# Core examples
cargo run --example basic
cargo run --example actor_async_worker
cargo run --example actor_blocking_task
# Advanced examples
cargo run --example kill_demo
cargo run --example ask_join_demo
cargo run --example handler_demo
cargo run --example weak_reference_demo
# Feature-gated examples
cargo run --example metrics_demo --features metrics
cargo run --example tracing_demo --features tracing
Example Structure
Each example follows this pattern:
- Actor Definition: Define the actor struct and implement the
Actortrait (or use#[derive(Actor)]) - Message Types: Define messages that the actor can handle
- Message Handlers: Use
#[message_handlers]with#[handler]attributes - Usage: Demonstrate spawning, messaging, and lifecycle management