I'm trying to overload the right shift operator (>>
) in rust to implement a Maybe bind.
enum Maybe<T> {
Nothing,
Just(T)
}
/// Maybe bind. For example:
///
/// ```
/// Just(1) >> |x| Just(1 + x) >> |x| Just(x * 2)
/// ```
impl<'a, T, U> Shr<|&T|: 'a -> Maybe<U>, Maybe<U>> for Maybe<T> {
fn shr(&self, f: &|&T| -> Maybe<U>) -> Maybe<U> {
match *self {
Nothing => Nothing,
Just(ref x) => (*f)(x)
}
}
}
fn main() {}
However, I'm running into an error trying to invoke the closure:
<anon>:15:28: 15:32 error: closure invocation in a `&` reference
<anon>:15 Just(ref x) => (*f)(x)
^~~~
error: aborting due to previous error
playpen: application terminated with error code 101
Program ended.
Why is it erroneous to invoke a borrowed closure, and how can I resolve the issue and implement the bind?
I found a similar question on stackoverflow, but rust has changed enough since then so that it no longer works.