Generic Types, Traits, and Lifetimes

    Similar to the way a function takes parameters with unknown values to run the same code on multiple concrete values, functions can take parameters of some generic type instead of a concrete type, like or String. In fact, we’ve already used generics in Chapter 6 with Option<T>, Chapter 8 with Vec<T> and HashMap<K, V>, and Chapter 9 with Result<T, E>. In this chapter, you’ll explore how to define your own types, functions, and methods with generics!

    First, we’ll review how to extract a function to reduce code duplication. Next, we’ll use the same technique to make a generic function from two functions that differ only in the types of their parameters. We’ll also explain how to use generic types in struct and enum definitions.

    Then you’ll learn how to use traits to define behavior in a generic way. You can combine traits with generic types to constrain a generic type to only those types that have a particular behavior, as opposed to just any type.

    Finally, we’ll discuss lifetimes, a variety of generics that give the compiler information about how references relate to each other. Lifetimes allow us to borrow values in many situations while still enabling the compiler to check that the references are valid.

    Before diving into generics syntax, let’s first look at how to remove duplication that doesn’t involve generic types by extracting a function. Then we’ll apply this technique to extract a generic function! In the same way that you recognize duplicated code to extract into a function, you’ll start to recognize duplicated code that can use generics.

    Consider a short program that finds the largest number in a list, as shown in Listing 10-1.

    Listing 10-1: Code to find the largest number in a list of numbers

    This code stores a list of integers in the variable number_list and places the first number in the list in a variable named . Then it iterates through all the numbers in the list, and if the current number is greater than the number stored in largest, it replaces the number in that variable. However, if the current number is less than or equal to the largest number seen so far, the variable doesn’t change, and the code moves on to the next number in the list. After considering all the numbers in the list, largest should hold the largest number, which in this case is 100.

    To find the largest number in two different lists of numbers, we can duplicate the code in Listing 10-1 and use the same logic at two different places in the program, as shown in Listing 10-2.

    Filename: src/main.rs

    Listing 10-2: Code to find the largest number in two lists of numbers

    Although this code works, duplicating code is tedious and error prone. We also have to update the code in multiple places when we want to change it.

    In Listing 10-3, we extracted the code that finds the largest number into a function named largest. Unlike the code in Listing 10-1, which can find the largest number in only one particular list, this program can find the largest number in two different lists.

    Filename: src/main.rs

    Listing 10-3: Abstracted code to find the largest number in two lists

    The largest function has a parameter called list, which represents any concrete slice of i32 values that we might pass into the function. As a result, when we call the function, the code runs on the specific values that we pass in. Don’t worry about the syntax of the loop for now. We aren’t referencing a reference to an i32 here; we’re pattern matching and destructuring each &i32 that the for loop gets so that item will be an i32 inside the loop body. We’ll cover pattern matching in detail in .

    In sum, here are the steps we took to change the code from Listing 10-2 to Listing 10-3:

    Next, we’ll use these same steps with generics to reduce code duplication in different ways. In the same way that the function body can operate on an abstract list instead of specific values, generics allow code to operate on abstract types.