Do both the composition and pipe forward operators (like in other languages) exist in Rust? If so, what do they look like and one should one be preferred to the other? If one does not exist, why is this operator not needed?
相关问题
- Share Arc between closures
- Function references: expected bound lifetime param
- Pattern matching on slices
- How can I iteratively call Sha256::digest, passing
- Destructure a vector into variables and give away
相关文章
- How can I convert a f64 to f32 and get the closest
- What is a good way of cleaning up after a unit tes
- How can I unpack (destructure) elements from a vec
- How to import macros in Rust?
- How to get struct field names in Rust? [duplicate]
- Confusion between [T] and &[T]
- How do I initialize an opaque C struct when using
- What's the most idiomatic way to test two Opti
These operators do not exist in Rust as far as I know. So far I haven't felt much of a need for them, and there is also an effort to keep the core Rust syntax fairly small. For example, Rust used to have explicit message passing operators, but these were removed.
You might be able to use operator overloading to come up with something similar if you want, or just write your own compose or pipe forward functions. I wouldn't be surprised if the Rust team were open to including these in the standard library.
There is no such operator built-in, but it's not particularly hard to define:
The
Wrapped
new-type struct is purely to allow theShr
instance, because otherwise we would have to implement it on a generic (i.e.impl<A, B> Shr<...> for A
) and that doesn't work.Note that idiomatic Rust would call this the method
map
instead of using an operator. SeeOption::map
for a canonical example.