Error code E0401
Inner items do not inherit the generic parameters from the items they are embedded in.
Erroneous code example:
ⓘ
Nor will this:
ⓘ
Or this:
ⓘ
Items nested inside other items are basically just like top-level items, except that they can only be used from the item they are in.
There are a couple of solutions for this.
If the item is a function, you may use a closure:
For a generic item, you can copy over the parameters:
Be sure to copy over any bounds as well:
This may require additional type hints in the function body.
In case the item is a function inside an impl
, defining a private helper
function might be easier:
For default impls in traits, the private helper solution won't work, however closures or copying the parameters should still work.