I'm trying to create a recursive inner function that will print all elements in a linked list:
fn print_stack(&self) {
fn print_nodes(head: &Option<Box<Node<T>>>) {
match head {
Some(ref p) => {
println!("{:?}",p.data);
print_nodes(head.next);
},
}
};
print_nodes(&self.head);
}
The compiler generates the following error
can't use type parameters from outer function; try using a local type parameter instead.
Why is this an error?
From Rust Compiler Error Index:
I would conclude from this that items inside functions are also compiled as top-level items. Allowing references to the outer function type parameters would allow two different function definitions with the same name, without any type parameters on the function itself to disambiguate, so would require name mangling changes. Making a small test program confirms this:
Compiling this and calling
nm
on the generated output shows two definitions ofbar
, and a single definition ofbaz
.It's possible to change Rust in the way you expect, but it would need to be designed first, and the perceived benefits must outweigh the cost to implement it. Given the available workarounds, the perceived benefits may be small. In your case, as indicated on that page, the workaround is to specify the type parameters for the inner function as well.