From b00b78a773e796a36f65408c996a8d42ce3ec2d4 Mon Sep 17 00:00:00 2001 From: darkicewolf50 Date: Wed, 5 Feb 2025 11:14:05 -0700 Subject: [PATCH] finished ch10 --- .obsidian/workspace.json | 26 ++++++++++++++++------ Generic Types Traits and Lifetimes.md | 31 ++++++++++++++++++++++++++- Lifetimes.md | 18 ++++++++++++++++ Traits.md | 14 ++++++------ 4 files changed, 75 insertions(+), 14 deletions(-) diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index 4614abd..b760af3 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -13,12 +13,26 @@ "state": { "type": "markdown", "state": { - "file": "Lifetimes.md", + "file": "Generic Types Traits and Lifetimes.md", "mode": "source", "source": false }, "icon": "lucide-file", - "title": "Lifetimes" + "title": "Generic Types Traits and Lifetimes" + } + }, + { + "id": "53b36d00b704136e", + "type": "leaf", + "state": { + "type": "markdown", + "state": { + "file": "Traits.md", + "mode": "source", + "source": false + }, + "icon": "lucide-file", + "title": "Traits" } } ] @@ -166,9 +180,11 @@ }, "active": "caf0233e624d6c1c", "lastOpenFiles": [ - "2025-02-04.md", - "Lifetimes.md", + "Traits.md", + "Generics.md", "Generic Types Traits and Lifetimes.md", + "Lifetimes.md", + "2025-02-04.md", "data_types.md", "Collection of Common Data Structs.md", "Constants.md", @@ -176,7 +192,6 @@ "Data Types.md", "Enums.md", "Error Handling.md", - "Generics.md", "Hash.md", "Modules and Use.md", "ownership.md", @@ -188,7 +203,6 @@ "Reducing_Code_Duplication.md", "String.md", "Structures.md", - "Traits.md", "Variables.md", "Vector.md", "Reducing.md", diff --git a/Generic Types Traits and Lifetimes.md b/Generic Types Traits and Lifetimes.md index 9267bbe..3934b59 100644 --- a/Generic Types Traits and Lifetimes.md +++ b/Generic Types Traits and Lifetimes.md @@ -18,4 +18,33 @@ This chapter covers 3 things 1. [Reducing Code duplication](Reducing_Code_Duplication.md) 1. [Generics](Generics.md) 1. [Traits](Traits.md) -1. [Lifetimes](Lifetimes.md) \ No newline at end of file +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 \ No newline at end of file diff --git a/Lifetimes.md b/Lifetimes.md index b47878e..e16ccdc 100644 --- a/Lifetimes.md +++ b/Lifetimes.md @@ -442,3 +442,21 @@ There are two input lifetimes, so Rust applies the first lifetime elision rule a Then because, because one of the parameters is `&self` the return type gets the lifetime of `&self` and all lifetimes have been dealt with ## The Static Lifetime +One special lifetime is the `'static`, which denotes that the affected reference *can* live for the entire duration of the program + +All string literals have the `'static` lifetime always + +Here is how we can annotate it +```rust +let s: &'static str = "I have a static lifetime."; +``` + +The string literal is stored in the program's binary which is always available + +You might see suggestions to use `'static` lifetime in the error message. + +Think about whether or not the value being referenced/the reference will always be valid before adding it + +Most of the time the suggestion comes from attempting to create a dangling reference or a mismatch of the available lifetimes, instead of adding a `'static` lifetime annotation + +Instead the solution is to fix those problems \ No newline at end of file diff --git a/Traits.md b/Traits.md index ad71040..3146dc6 100644 --- a/Traits.md +++ b/Traits.md @@ -2,11 +2,11 @@ This is used to defined shared behavior -A *trait* defines the fnctionality a particular type has and can share with other tpyes. +A *trait* defines the functionality a particular type has and can share with other types. -Traits are ued to define shared behavior in an abstract way. +Traits are used to define shared behavior in an abstract way. -We can use *trait bounds* to specifty that a generic type can be any type that has certain behavior +We can use *trait bounds* to specify that a generic type can be any type that has certain behavior Note: Traits are similar to a feature often called *interfaces* in other languages, but there are some differences in Rust @@ -16,15 +16,15 @@ A type's behavior consists of the methods we can call on that type Some types can share the same behavior if we can call the same methods on all of those types. -Trait definitions are a way to gropu method singatures together to define a set of behaviors necessary to accomplish something. +Trait definitions are a way to group method signatures together to define a set of behaviors necessary to accomplish something. -Here is an exmaple to lay this out +Here is an example to lay this out lets say you have multiple structs that hold various kinds and amounts of text -`NewsArticle` struct that holds a news story filed in a particular locaion +`NewsArticle` struct that holds a news story filed in a particular location `Tweet` that can have, at most, 280 characters along with metadata that indicates whether it was a new tweet, a retweet, or a reply to another tweet -We want to make a meida aggregator library crate named `aggregator` that can display summaries of data that could be stored in a `NewsArticle` or `Tweet` +We want to make a media aggregator library crate named `aggregator` that can display summaries of data that could be stored in a `NewsArticle` or `Tweet` In order to do this we need a summary from each type, this would be done by calling a `summarize` method on an instance