Pipe Trait
Make it possible to chain regular functions.
APIs
By adding use pipe_trait::*
, 9 methods are added to all types:
identifier | pipe syntax | traditional syntax |
---|---|---|
Pipe::pipe |
x.pipe(f) |
f(x) |
Pipe::pipe_ref |
x.pipe_ref(f) |
f(&x) |
Pipe::pipe_mut |
x.pipe_mut(f) |
f(&mut x) |
Pipe::pipe_as_ref |
x.pipe_as_ref(f) |
f(x.as_ref()) |
Pipe::pipe_as_mut |
x.pipe_as_mut(f) |
f(x.as_mut()) |
Pipe::pipe_deref |
x.pipe_deref(f) |
f(&x) |
Pipe::pipe_deref_mut |
x.pipe_deref_mut(f) |
f(&mut x) |
Pipe::pipe_borrow |
x.pipe_borrow(f) |
f(x.borrow()) |
Pipe::pipe_borrow_mut |
x.pipe_borrow_mut(f) |
f(x.borrow_mut()) |
Read the docs for more information.
Usage Examples
Same type
use *;
let inc = ;
let double = ;
let square = ;
let a = .pipe.pipe.pipe;
let b = square;
assert_eq!;
Type transformation
use *;
let x = 'x';
let a = x
.pipe // (char, char, char)
.pipe // [(char, char, char); 2]
.pipe; // String
let b = "[('x', 'x', 'x'), ('x', 'x', 'x')]";
assert_eq!;
Pipe amongst method chain
use *;
my_future
.pipe
.await
.pipe
.inc
.pipe
.double
.pipe
.square
.pipe
.get
.pipe;
Explicit type annotation
use *;
let x = "abc".to_string;
let a = x
.
.chars
.
.;
let b = vec!;
assert_eq!;