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?