I tried to implement the Reader Monad in Rust:
pub struct Reader<'a, R, T>(Box<dyn Fn(R) > T + 'a>);
impl<'a, R, T> Reader<'a, R, T> {
pub fn new(f: impl Fn(R) > T + 'a) > Reader<'a, R, T> {
Self(Box::new(f))
}
pub fn run(self, r: R) > T {
self.0(r)
}
pub fn then<U>(self, f: impl Fn(T) > U + 'a) > Reader<'a, R, U> {
Reader::new(r: R f(self.0(r)))
}
}
The compiler complains that:
error[E0309]: the parameter type `R` may not live long enough
> src/reader.rs:13:9

3  impl<'a, R, T> Reader<'a, R, T> {
  help: consider adding an explicit lifetime bound...: `R: 'a`
...
13  Reader::new(r: R f(self.0(r)))
 ^^^^^^^^^^^ ...so that the type `[closure@src/reader.rs:13:21: 13:40]` will meet its required lifetime bounds...

note: ...that is required by this bound
> src/reader.rs:4:37

4  pub fn new(f: impl Fn(R) > T + 'a) > Reader<'a, R, T> {
 ^^
I don't understand why R
may not live long enough...