Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

operator_tap

crates.io ci codecov license

Book Page - Operator Source - Subscriber Source

The tap operator lets you forward upstream values to a destination observer.

The destination could be anything, a PrintObserver to log upstream values to the console, or even a subject to trigger another pipeline.

Good to know

  • Keep in mind that the destination observer passed in will not get upgraded1, meaning a tap operator will never call unsubscribe on the destination, even if it’s a subscriber that upgrades to itself and has an unsubscribe implementation. However, the error and complete signals are forwarded. If you want to avoid forwarding error and complete, use tap_next instead.

See Also

  • TapNextOperator - Run a callback for each next value while letting signals pass through.
  • OnNextOperator - Invoke a callback for each value that can also decide whether to forward it.
  • OnSubscribeOperator - Run a callback when a subscription is established.
  • FinalizeOperator - Execute cleanup when the observable finishes or unsubscribes.

Example

cargo run -p rx_core --example operator_tap_example
(1..=3)
    .into_observable()
    .tap(PrintObserver::new("tap_destination"))
    .subscribe(PrintObserver::new("tap_operator"));

Output:

tap_destination - next: 1
tap_operator - next: 1
tap_destination - next: 2
tap_operator - next: 2
tap_destination - next: 3
tap_operator - next: 3
tap_destination - completed
tap_operator - completed
tap_operator - unsubscribed

  1. Documentation on UpgradeableObserver