RustBrock/Generic Types Traits and Lifetimes.md
2025-02-05 11:14:05 -07:00

1.7 KiB

Generic Types, Traits, and Lifetimes

Generics

A tools for effectively handling the duplication of concepts.

One of these tools is generics

They are abstract stand-ins for concrete types or other properties

We can express the behavior of generics or how they relate to other generics without knowing what type that will be in place when compiling and running the code.

Functions can either take parameters of some generic type or a concrete type (like a i32 or String) in the same way they take parameters with unknown values to run the same code on multiple concrete values

One example where concrete values were already used was with Option<T>, Vec<T>, HashMap<K, V> and Result<T, E>

This chapter covers 3 things

  1. Reducing Code duplication
  2. Generics
  3. Traits
  4. Lifetimes

Generic Type Parameters, Trait Bounds, and Lifetimes Together

Here is an example with all of them together

use std::fmt::Display;

fn longest_with_an_announcement<'a, T>(
    x: &'a str,
    y: &'a str,
    ann: T,
) -> &'a str
where
    T: Display,
{
    println!("Announcement! {ann}");
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

This is the longest function that returns the longer of the two string slices.

Now it has a parameter called ann of the generic type T which can only be any type that implements the Display trait as specified by the where clause

The ann parameter will be printed using {}, which is why the Display trait is necessary

Because lifetimes are a type of generic the declarations of the lifetime parameter 'a and the generic type parameter T go in the same list inside the angle brackets after the function name