> I'am learning the functional programming paradigm with rust and to help me I
> decide to translate the pattern of the book "Functional Programming Patterns
> in Scala and Clojure" in Rust. In this work I have a problem to return a
> closure (or a function) as a return value and I didn't find any solution. I
> understand the problem but I can't find a solution. The code is :

Closures in Rust are stack allocated, so you can't return them from a
function since the function's stack will be gone. You can use either a
proc() or a ~Trait object. A proc can only be called once, but a trait
object can be called many times. If you don't need to close over any
state (which it appears you don't from your example), then you can
return bare functions.

Here's a trait object example (untested and incomplete):

trait Comparison {
  fn compare(&self, p1: &Person, p2: &Person) -> Ordering;
}

fn make_comparison() -> ~Comparison {
  struct ClosedOverState {
     ...
  }
  impl Comparison for ClosedOverState {
    fn compare(...) -> Ordering {
       .... // access state through self.foo
    }
  }

  ~ClosedOverState {
    foo: 0,
  }
}

It can be simplified with macros.

jack.
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to