[][src]Crate tap

A simple crate exposing tapping functionality for all types, and extended functionality for Option, Result & Future.

The tap operation takes, and then returns, full ownership of the variable being tapped. This means that the closure may have mutable access to the variable, even if the variable is otherwise immutable.

Examples

Logging error values:

let values: [Result<i32, &str>; 4] = [Ok(3), Err("foo"), Err("bar"), Ok(8)];

let _ = values.iter().filter_map(|result|
    // print error information before discarding them
    result.tap_err(|error| eprintln!("Invalid entry: {}", error)).ok()
);

Chaining methods:

fn get_numbers() -> Vec<u32> {
   vec![4, 9, 1, 17, 3]
}
 
let mut old = get_numbers();
old.sort();
 
// can now be written like this instead
let new = get_numbers().tap(|data| data.sort());
 
assert_eq!(old, new)

Reducing the amount of mutable variables:

let tapped = [1, 2, 3]; // does not need to be mutable, preventing accidental mutations
let tapped = tapped.tap(|arr| {
    for elt in arr.iter_mut() {
        *elt *= 2;
    }
});
 
// instead of
let mut untapped = [1, 2, 3];
for elt in untapped.iter_mut() {
    *elt *= 2;
}
assert_eq!(tapped, untapped);

Traits

TapBooleanOps

Tap operations for bool.

TapFutureOps

Tap operations for Future, requires the feature future.

TapNomOps

Tap operations for nom's IResult, requires the feature nom3.

TapOps

Tap operations for all types.

TapOptionOps

Tap operations for Option.

TapResultOps

Tap operations for Result.