3

My idea is to filter some elements depending on some pattern matching:

pub struct NodeHeading {
    pub level: u32,
    pub setext: bool,
}

pub enum NodeValue {
    Document,
    Heading(NodeHeading),
}

fn main() {
    // let's suppose that nodes variable is already defined
    let headings = find_headings(&nodes, ¿EXPRESSION_HERE?);

    // for convenience, I won't declare lifetimes in this example
    pub fn find_headings(
        nodes: &Vec<&AstNode>,
        expr: ¿WHAT_TYPE_HERE?,
    ) -> Vec<&AstNode> {
        let headings: Vec<&AstNode> = nodes
            .into_iter()
            .filter(|x| match x {
                expr => true,
                _ => false,
            })
            .collect();
        headings
    }
}

Can this pattern be used dynamically? Could it arrive as an argument to the find_headings function? I haven't found anything related to this so I guess the answer will be no.

If it is possible, what is the correct type?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
robertohuertasm
  • 846
  • 9
  • 17
  • 1
    I believe your question is already answered by [How can I store a pattern in a variable in Rust?](https://stackoverflow.com/q/42793606/155423). (TL;DR: no). If you disagree, please [edit] your question to explain how this differs from that Q&A. Otherwise, we can mark this question as already answered. – Shepmaster Apr 29 '18 at 21:22
  • 1
    You may also be interested in [Why is it discouraged to accept a reference to a String (&String), Vec (&Vec) or Box (&Box) as a function argument?](https://stackoverflow.com/q/40006219/155423). – Shepmaster Apr 29 '18 at 21:24
  • 3
    @Shepmaster thank you very much for the information you provided. Both links are superuseful and have answered my question. We can mark, indeed, this question as already answered. – robertohuertasm Apr 29 '18 at 21:42

0 Answers0