OK, thanks. Just to show the use case, here’s what I’m using a closure for. This is a typical “find” operation in a DOM-type tree structure. “Element” here is from Florob/RustyXML.
//
// find_all -- find all elements for which test function returns true.
//
// Someday this will accept unboxed lambdas for testfn,
// but that's not working yet in Rust 1.0 pre-alpha
//
pub fn find_all<'a>(tree: &'a xml::Element, testfn: fn(&xml::Element) -> bool, finds: &mut Vec<&'a xml::Element>, recurse: bool) {
if testfn(tree) // if match
{ finds.push(tree); // save if match on name
if recurse == false { return } // explore finds?
}
for child in tree.children.iter() {
match *child { // child is an Xml enum
xml::Xml::ElementNode(ref childelt) => {
find_all(childelt, testfn, finds, recurse ); }, // recurse
_ => () // ignore non ElementNode children
} // end match child
}
}
Usage currently looks like
let mut items = Vec::<&xml::Element>::new(); // accumulate ITEM entries
fn isitem(e: &xml::Element) -> bool { e.name == "item" };// someday this will be an unboxed lambda
find_all(tree, isitem, &mut items, false); // find all ITEM elements
Works fine, but requires defining a named function.
As I understand it, once unboxed lambdas are implemented, this can be replaced with the more concise form
let mut items = Vec::<&xml::Element>::new();
find_all(tree, |e| e.name == "item", &mut items, false);
That’s clearly a lambda that needs no boxing; it imports nothing from outer scopes.
That will be convenient; this idiom comes up in most HTML/XML/JSON tree processing. Is that a correct understanding of what’s coming?