What is the rationale for not being able to use the outer type parameter within an inner function?


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

<pre class="lang-none prettyprint-override">can't use type parameters from outer function; try using a local type parameter instead.

Why is this an error?


From <a href="https://doc.rust-lang.org/error-index.html#E0401" rel="nofollow">Rust Compiler Error Index</a>:



Inner items do not inherit type parameters from the functions they are embedded in.


Items inside functions are basically just like top-level items, except that they can only be used from the function they are in.

There are a couple of solutions for this.


For a generic item, you can copy over the parameters:



I would conclude from this that items inside functions are also <em>compiled as</em> 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:

struct Foo<T>(T); impl <T> Foo<T> { pub fn bar() { fn baz() { } baz(); } } fn main() { Foo::<i32>::bar(); Foo::<u32>::bar(); }

Compiling this and calling nm on the generated output shows two definitions of bar, and a single definition of baz.

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.


  • Inviting facebook friends in Air app for iOS
  • ggplot2/R output pdf too large
  • RestSharp PUT XML, RestSharp is sending it as GET?
  • Full Height Image
  • Show and remove “No Record Found” message in jqgrid 4.6.0
  • How to build executable with pyinstaller that uses pycryptodome?
  • Printing a flowdocument with dynamic data in WPF
  • Reading large text file very slow
  • query a json key in postgres json field
  • PySpark: Get first Non-null value of each column in dataframe
  • What is the diff. between default.properties and project.properties?
  • iOS - open another application that exists on the phone?
  • Detect when MathJax has finished loading in UIWebView
  • Making query to find nearest multiple(Lat,Long) from the single(Lat,Long)
  • Sorting Custom Listview Items Using Spinner Android
  • How to convert days into months using datetime in Python3?
  • Signed Java web start application with Glassfish 4.1 and Java7
  • Add checkbox dynamically using angular 2
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • How to join two tables from different databases
  • Disable account chooser FirebaseUI React
  • SQL Server 2012 not showing unicode character in results
  • What is the difference between dynamically creating a script tag and statically embed a script tag?
  • Java .policy file - how to prevent java.util.Date() from being accessible
  • Year over Year Stats from a Crossfilter Dataset
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • How to turn off notice reporting in xampp?
  • How to use Streams api peek() function and make it work?
  • Stacked bar chart with continuous time-axis as x-axis
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Why isn't stemDocument stemming?
  • Google App Engine Datastore: Dealing with eventual consistency
  • How to decleare char *const argv[] in swift [duplicate]
  • How to get rgb from transparent pixel in js
  • ssh remote server login script
  • Excel VBA : conditional formatting of sheet1 cells from sheet2 values in excel 2007
  • How to call different template for different category archive page in woocommerce
  • How to use FirstOrDefault inside Include
  • WPF custom control and direct content support
  • Access to a Matlab gui from the web