DEV Community

Discussion on: Functional Programming in Rust

Collapse
 
natserract profile image
Natserract • Edited

Whoa, yes i missconception about currying, thankyou for your comment, i will edit this ;)

Collapse
 
natserract profile image
Natserract

Finally:

#[derive(Debug)]
struct States<'a> {
    a: &'a i32,
    b: &'a i32,
}

trait Currying {
    type ReturnType: Fn(i32) -> i32;
    fn add(self) -> Self::ReturnType;
}

impl Currying for States<'static>{
    type ReturnType = Box<dyn Fn(i32) -> i32>;

    fn add(self) -> Self::ReturnType {
        Box::new(move|x| {
            x * self.a
        })
    }
}

let r_value: States = States {
    a: &100,
    b: &100
};

let r1 = r_value.add();
let r2 = r1(5);

assert_eq!(500, r2);