Skip to content

Instantly share code, notes, and snippets.

@darddan
Created September 16, 2021 18:35
Show Gist options
  • Save darddan/9c1fb775b3271b9bbe13ba875e12d156 to your computer and use it in GitHub Desktop.
Save darddan/9c1fb775b3271b9bbe13ba875e12d156 to your computer and use it in GitHub Desktop.
|> in rust
pub trait Pipe<Output> {
fn pipe(self, pipe_fn: fn(Self) -> Output) -> Output;
}
impl <T, Output> Pipe<Output> for T {
fn pipe(self, pipe_fn: fn(Self) -> Output) -> Output {
pipe_fn(self)
}
}
fn main() {
let value = 2
.pipe(|val| val+2)
.pipe(|val| val * 3)
.pipe(|val| format!("value = {}", val));
println!("{}", value);
}
@darddan
Copy link
Author

darddan commented Sep 16, 2021

Or something like:

    2
        .pipe(|val| val*2)
        .pipe(|val| val*3)
        .pipe(|val| println!("Number {}", val))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment