Files
.gitea
.github
.obsidian
HelloWorld
actix_web_learning
adder
branches
functions_rust
guessing_game
hello
hello-async
hello_cargo
loops
minigrep
ownership
rectangles
.gitignore
Advanced Features.md
Advanced Functions and Closures.md
Advanced Traits.md
Advanced Types.md
Any Number of Futures.md
Applying Async.md
Async, Await, Futures and Streams.md
Cargo Workspaces.md
Cargo and Cratesio.md
Characteristics of OO Languages.md
Closures.md
Collection of Common Data Structs.md
Concurrency.md
Constants.md
Crates.md
Custome Build Profiles.md
Data Types.md
Deref Trait.md
Drop Trait.md
Enums.md
Error Handling.md
Extending Cargo.md
For later baja.md
Futures and Async.md
Futures in Sequence.md
Futures, Tasks and Threads Together.md
Generic Types Traits and Lifetimes.md
Generics.md
Hash.md
Implementing OO Design Pattern.md
Improving The IO Project.md
Install Binaries.md
Iterators and Closures.md
Iterators.md
Leaky Reference Cycles.md
Lifetimes.md
Macros.md
Modules and Use.md
OOP Programming Features.md
Packages.md
Passing Data Between Threads.md
Paths.md
Pattern Matching.md
Pattern Syntax.md
Places Patterns Can Be Used.md
Primitives.md
Project Organization.md
Publishing libraries.md
README.md
Reducing_Code_Duplication.md
Ref Cell Mutability.md
Reference Counter Smart Pointer.md
Refutability.md
RustBrock.code-workspace
Shared State Concurrency.md
Simultaneous Code Running.md
Smart Pointers.md
String.md
Structures.md
Sync and Send.md
Test Controls.md
Test_Organization.md
Tests.md
The Performance Closures and Iterators.md
Trait Objects that allow for Values of Different Types.md
Traits for Async.md
Traits.md
Unsafe Rust.md
Using Box on the Heap.md
Variables.md
Vector.md
Writing_Tests.md
data_types.md
ownership.md
RustBrock/Generic Types Traits and Lifetimes.md
2025-02-05 11:14:05 -07:00

50 lines
1.7 KiB
Markdown

# 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](Reducing_Code_Duplication.md)
1. [Generics](Generics.md)
1. [Traits](Traits.md)
2. [Lifetimes](Lifetimes.md)
## Generic Type Parameters, Trait Bounds, and Lifetimes Together
Here is an example with all of them together
```rust
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