Crates.io | piping |
lib.rs | piping |
version | 1.0.0 |
source | src |
created_at | 2023-05-25 22:31:43.504129 |
updated_at | 2023-05-26 06:56:43.931119 |
description | Pipeline syntax in Rust. `4isize |> double(__) |> __ as usize`. |
homepage | |
repository | https://github.com/Kyza/piping |
max_upload_size | |
id | 874671 |
size | 10,207 |
piping
provides a pipe!
macro that allows you to use the pipeline operator in Rust.
let wrapped = orig_and_double(add(2, num)).1 as isize;
let piped = pipe! {
num |> add(2, __) |> orig_and_double(__),
(_, doubled) |> doubled as isize,
};
Documentation is provided on docs.rs.
fn add(a: usize, b: usize) -> usize {
a + b
}
fn orig_and_double(num: usize) -> (usize, usize) {
(num, num * 2)
}
let num = 4;
let piped = pipe! {
num |> add(2, __) |> orig_and_double(__),
(_, doubled) |> doubled as isize,
};
// Expands to...
let piped = {
let __ = num;
let __ = add(2, __);
let (_, doubled) = orig_and_double(__);
doubled as isize
};
You can pass any expression in as the input.
Notice that you can chain pipelines with ,
s to destructure the result of the previous pipeline.
The macro also tries to optimize the generated code to minimize the amount of reassigning done.