Hello,

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 :

    struct Person    {
        firstname: ~str,
        lastname: ~str,
    }

    let p1 = Person    {firstname: ~"Michael", lastname: ~"Bevilacqua"};
    let p2 = Person {firstname: ~"Pedro", lastname: ~"Vasquez"};
    let p3 = Person {firstname: ~"Robert", lastname: ~"Aarons"};

    //mutable version.
    let mut people = ~[~p3, ~p2, ~p1];

    //convert comparision to ordering.
    fn compare<T:Ord + Eq>(a: &T, b: &T) -> Ordering {
        if a < b {Less}
        else if a == b {Equal}
        else {Greater}
    }

    fn firstname_comparaison(p1: &~Person, p2: &~Person) -> Ordering{
        compare(&p1.firstname, &p2.firstname)
    }

    fn lastname_comparaison(p1: &~Person, p2: &~Person) -> Ordering{
        compare(&p1.lastname, &p1.lastname)
    }

fn make_comparision(comp1: fn(&~Person, &~Person)->Ordering, comp2: fn(&~Person, &~Person)->Ordering) -> |&~Person, &~Person|->Ordering {
        |p1: &~Person, p2: &~Person| ->Ordering{
            let c = comp1(p1, p2);
            if (c == Equal) {
                comp2(p1, p2)
            }
            else    {
                c
            }
        }
    }

people.sort_by(make_comparision(firstname_comparaison, lastname_comparaison));

error: cannot infer an appropriate lifetime due to conflicting requirements
       &'a |p1: &~Person, p2: &~Person| ->Ordering{
           let c = comp1(p1, p2);
             if (c == Equal) {
                comp2(p1, p2)
             }
             else    {

Perhaps someone can help me in finding a way to construct a closure and use it later.

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

Reply via email to