<commit_before>% Generics

Sometimes, when writing a function or data type, we may want it to work for
multiple types of arguments. Luckily, Rust has a feature that gives us a better
way: generics. Generics are called ‘parametric polymorphism’ in type theory,
which means that they are types or functions that have multiple forms (‘poly’
is multiple, ‘morph’ is form) over a given parameter (‘parametric’).

Anyway, enough with type theory, let’s check out some generic code. Rust’s
standard library provides a type, `Option<T>`, that’s generic:

```rust
enum Option<T> {
    Some(T),
    None,
}
```

The `<T>` part, which you’ve seen a few times before, indicates that this is
a generic data type. Inside the declaration of our enum, wherever we see a `T`,
we substitute that type for the same type used in the generic. Here’s an
example of using `Option<T>`, with some extra type annotations:

```rust
let x: Option<i32> = Some(5);<commit_msg>book: remove a sentence that is not clear<commit_after>% Generics

Sometimes, when writing a function or data type, we may want it to work for
multiple types of arguments. In Rust, we can do this with generics.
Generics are called ‘parametric polymorphism’ in type theory,
which means that they are types or functions that have multiple forms (‘poly’
is multiple, ‘morph’ is form) over a given parameter (‘parametric’).

Anyway, enough with type theory, let’s check out some generic code. Rust’s
standard library provides a type, `Option<T>`, that’s generic:

```rust
enum Option<T> {
    Some(T),
    None,
}
```

The `<T>` part, which you’ve seen a few times before, indicates that this is
a generic data type. Inside the declaration of our enum, wherever we see a `T`,
we substitute that type for the same type used in the generic. Here’s an
example of using `Option<T>`, with some extra type annotations:

```rust
let x: Option<i32> = Some(5);
{'fraction_non_alphanumeric': 0.08663497094558902, 'fraction_numerical': 0.003169572107765452, 'mean_word_length': 4.570588235294117, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1172414', 'n_tokens_mistral': 555, 'n_tokens_neox': 525, 'n_words': 294}