Wondering what an example would be of a non-generic inner function mentioned in Rust for Rustaceans

Chapter 2: Types

Non-Generic Inner Functions

Often, much of the code in a generic method is not type-dependent. Consider, for example, the implementation of HashMap::insert. The code to compute the hash of the supplied key depends on the key type of the map, but the code to walk the buckets of the map to find the insertion point may not. In cases like this, it would be more efficient to share the generated machine code for the non-generic parts of the method across monomorphizations, and only generate distinct copies where this is actually needed. One pattern you can use for cases like this is to declare a non-generic helper function inside the generic method that performs the shared operations. This leaves only the type-dependent code for the compiler to copy-paste for you while allowing the helper function to be shared.

What would be an example of this pattern?

You can look up the source for HashMap::insert in the std-lib docs page.
For a different example:

fn path_bingo<P: AsRef<Path>>(path: P) {
    fn path_bingo_inner(path: &Path) {
        // Do the complex stuff
    }
    path_bingo_inner(path.as_ref());
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.