Add rxRust to your Cargo.toml.
[dependencies]
rxrust = "1.0.0-beta.11"Here is a minimal example using the Local context, which is optimized for single-threaded environments (like WASM or UI main threads).
use rxrust::prelude::*;
fn main() {
// 1. Create an Observable that emits a single value (42)
Local::of(42)
// 2. Transform the value
.map(|v| v * 2)
// 3. Subscribe to consume the value
.subscribe(|v| println!("Value: {}", v));
}RxRust programming revolves around three key components:
- Observable (The Source): Represents a stream of events over time. It's lazy—nothing happens until you subscribe.
- Operators (The Pipeline): Pure functions that transform, filter, or combine streams (e.g.,
map,filter,merge). - Subscription (The Link): Connects an Observer to an Observable, starting the execution.
Unlike standard Iterators, Observables push data. You often need to manage the lifecycle of a subscription (e.g., to cancel a network request or stop a timer).
use rxrust::prelude::*;
fn main() {
// A 'Subject' allows us to imperatively push values into a stream
let mut subject = Local::subject();
// Create a subscription
let subscription = subject.clone()
.map(|x: i32| x * 2)
.subscribe(|v| println!("Got: {}", v));
// Push values
subject.next(1); // Output: Got: 2
subject.next(2); // Output: Got: 4
// Cancel the subscription
subscription.unsubscribe();
// This value will be ignored
subject.next(3);
}Now that you understand the basic flow, dive into the core concepts:
- Context & Threading: Learn when to use
LocalvsShared. - Operators: Explore the rich vocabulary of stream transformations.