# Pipe method: Do you think something like this should be in standard library?

**URL:** <https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408>\
**Category:** libs\
**Created:** [May 22, 2020, 11:19am UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408 "2020-05-22T11:19:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![KSXGitHub](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ksxgithub/32/5582_2.png) [@KSXGitHub](https://internals.rust-lang.org/u/KSXGitHub)\
**Post date:** [May 22, 2020, 11:19am UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/1 "2020-05-22T11:19:47Z")

</div>

I wanted something similar to a pipe-forward operator, so I created [a very simple `pipe` method](https://github.com/KSXGitHub/pipe-trait). It is very simple and useful, I think it would be nice to have it built-in.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [May 22, 2020, 12:26pm UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/2 "2020-05-22T12:26:45Z")

</div>

There's already a `dbg!` macro that does this.

---

<div class="post-metadata">

**Author:** ![KSXGitHub](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ksxgithub/32/5582_2.png) [@KSXGitHub](https://internals.rust-lang.org/u/KSXGitHub)\
**Post date:** [May 22, 2020, 12:41pm UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/3 "2020-05-22T12:41:30Z")

</div>

Did you confuse something? Or was I not clear enough? How does one use `dbg!` to pipe values?

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [May 22, 2020, 12:42pm UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/4 "2020-05-22T12:42:59Z")

</div>

I've meant for `pipe(log)`.

For more general piping, see:

> **[Pipeline operator |\>](https://users.rust-lang.org/t/pipeline-operator/21011)**
>
> Is there anything in Rust for chaining the operation like the pipeline operator |\> used in Julia and F# |\>(x, f) Applies a function to the preceding argument. This allows for easy function chaining. Examples ≡≡≡≡≡≡≡≡≡≡ julia\>...

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [May 22, 2020, 5:15pm UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/5 "2020-05-22T17:15:50Z")

</div>

This is very similar to Kotlin's `let` and `run` extension functions.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [May 22, 2020, 11:42pm UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/6 "2020-05-22T23:42:36Z")

</div>

I think this `.pipe()` method somehow needs to prove itself, maybe if it proves to be popular and significantly useful in some parts of the ecosystem?

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [May 23, 2020, 12:38am UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/7 "2020-05-23T00:38:15Z")

</div>

Just so I get this correctly, since you didn't mention it, this is about allowing to write `x.pipe(f)` instead of `f(x)`, right?

---

<div class="post-metadata">

**Author:** ![KSXGitHub](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ksxgithub/32/5582_2.png) [@KSXGitHub](https://internals.rust-lang.org/u/KSXGitHub)\
**Post date:** [May 23, 2020, 2:23am UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/8 "2020-05-23T02:23:23Z")

</div>

pipe is useful enough in languages such as F# and LISP that they have pipe operator/macro.

---

<div class="post-metadata">

**Author:** ![KSXGitHub](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ksxgithub/32/5582_2.png) [@KSXGitHub](https://internals.rust-lang.org/u/KSXGitHub)\
**Post date:** [May 23, 2020, 2:23am UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/9 "2020-05-23T02:23:37Z")

</div>

yes

---

<div class="post-metadata">

**Author:** ![tspiteri](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tspiteri/32/7283_2.png) [@tspiteri](https://internals.rust-lang.org/u/tspiteri)\
**Post date:** [May 23, 2020, 4:03pm UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/10 "2020-05-23T16:03:42Z")

</div>

This looks like the [apply crate](https://crates.io/crates/apply). While I like it, I don't think it has seen enough use to justify inclusion in the standard library.

---

<div class="post-metadata">

**Author:** ![cliff](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cliff/32/2650_2.png) [@cliff](https://internals.rust-lang.org/u/cliff)\
**Post date:** [May 23, 2020, 4:06pm UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/11 "2020-05-23T16:06:13Z")

</div>

> [@KSXGitHub](#):
>
> pipe is useful enough in languages such as F# and LISP that they have pipe operator/macro.

F# and lisp are also both functional languages, and rust is not, so not all idioms will translate nicely. For example, pipe combines cleanly with an operator based map / fold / etc, but how would it interact with the method based version that rust has on iterators?

This seems like an excellent idea to prove out in an external crate, using generic traits or macros. If the community finds it generally beneficial, it may be worth introducing custom syntax for it.

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [May 23, 2020, 4:29pm UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/12 "2020-05-23T16:29:23Z")

</div>

My personal experience and understanding of other languages is that idioms such as currying, partial application, pipe operators, and the endless array of other irregular application operators are far, _far_ more useful in "pure functional languages" where it's actually possible to implement them in a fully generic way on any function and any arguments.

In Rust, I'm fairly sure this can never be as generic or as useful as it is in those pure functional languages because Rust doesn't have a GC, has both value and reference types, has both mutable and immutable types, and has the whole lifetime and ownership system which affects what function signatures are legal to call where, among other things. For instance, in languages that have a GC, partial application is straightforward to implement for any function because you simply don't GC the applied arguments until after the function finally gets called. These are also a lot of the reasons why functions and closures are (and need to be) two completely different things in Rust (as well as C++), unlike pure functional Lisp/F#/etc and even "impure functional" languages like Javascript (is there a term for this?).

Or more simply: I don't think there's any way around the fact that a function call in Rust implies way more machinery, and often cannot be pulled apart into tiny independently executable pieces.

This still makes sense as a crate, and is likely useful when working with Rust code that is designed to be used with more functional idioms, but I just don't see this ever applying to the whole language enough to make sense in `std`.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [August 21, 2020, 4:29pm UTC](https://internals.rust-lang.org/t/pipe-method-do-you-think-something-like-this-should-be-in-standard-library/12408/13 "2020-08-21T16:29:29Z")

</div>

Closed on August 21, 2020, 4:29pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
