Defining closures to query another object

I have some querying struct that I use to query some queried struct. The query requires information from the fields from the querying struct. The queried struct receives the closure and applies it on itself and gives some response.

An example snippet, it's not runnable but it's a simplified version of what I'm trying to do:

struct Queried {
    field: Vec<(String, i32)>,
}

impl Queried {
    pub fn filter_with_closure<T>(&self, filter: T) -> Vec<(String, i32)> {
        for (title, num) in field.iter() {
            if filter(title, num) {
                // do something
            }
        }
        // return those satisfied by filter
    }
}

struct Querying {
    a: A,
    queried: &Queried,
}

impl Querying {
    pub fn random_func(&self) {
        let closure = |(title, num)| -> bool {title == a.name && num == a.year};
        let result = self.queried.filter_with_closure(closure);
        // do something with result
    }
}

I have many different closures that I'd like to use and am finding myself rewriting them over and over. I'm considering defining them individually somewhere (how to go about that?) and then in the Querying method build a Vec<Fn(title, num)> consisting of the individual closures, send them to the Queried and have the Queried apply each condition.

Is there a way to define a closure/method in the Querying struct and be able to pass that as a function/closure to the filter_with_closure method?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.