Sto provando a sovraccaricare lo right shift operator (>>
) in ruggine a implement a Maybe bind.Rust Shr Operator
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() {}
Tuttavia, sono in esecuzione in un errore nel tentativo di invocare la chiusura:
<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.
Perché è errato invocare una chiusura in prestito, e come posso risolvere il problema e implementare la legano ?
Ho trovato uno similar question sullo stackoverflow, ma la ruggine è cambiata abbastanza da allora in modo che non funzioni più.
Sei consapevole che questo è esattamente ciò che 'Option.and_then' fa (anche se consuma' self' e così è in grado di passare l'oggetto per valore piuttosto che per riferimento, che è distintamente superiore)? –