10 releases

0.2.0 Apr 10, 2024
0.1.8 Mar 12, 2024
0.1.7 Jan 25, 2024

#573 in Rust patterns

Download history 21/week @ 2024-01-23 3/week @ 2024-02-13 98/week @ 2024-02-20 44/week @ 2024-02-27 137/week @ 2024-03-12 4/week @ 2024-03-19 6/week @ 2024-04-02 119/week @ 2024-04-09

125 downloads per month

MIT license

10KB
143 lines

The pipe operator in Rust

This crate is exactly what you expect it to be, the pipe operator in Rust.

Usage

You can construct a "pipeline" by passing any expression and at least a single pipe into the ::pipeop::pipe! macro. There are some special things you can do but, in its most basic form the macro tries to literally call your pipe with the item in the pipeline.

const fn add_one(to: i32) -> i32 {
    to + 1
}

let result = pipe!(1 |> add_one |> add_one);
assert!(result, 3);

Invoking methods on the item in the pipeline

You can invoke methods on the item in the pipeline at any time by prefixing the pipe with a ..

This example calls the add method on the item in the pipelines with 1 as the single argument.

use std::ops::Add;
pipe!(1 |> .add(1));

Closure based pipes

You can also use closures as pipes, so you don't have to define a whole new function for every simple operation. Both types of closures are valid, you can have a closure that just evaluates an expression, or you can have a whole block of code.

pipe!("Hello!"
    |> .to_uppercase()
    |> |item| in println!("{}", item)
);

You can also make closure based pipes look a little nicer by using this syntax instead.

pipe!("Hello!"
    |> .to_uppercase()
    |> item in println!("{}", item)
);

You can of course accept a pattern in this "weird closure". This example extracts the inner bool value from the Test instance in the pipeline with pattern matching.

struct Test(bool);
let result = pipe!(Test(true) |> Test(it) in it);
assert_eq!(result, true);

No runtime deps