mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-06-15 13:04:18 -06:00
finished ch10
This commit is contained in:
parent
89f9705f62
commit
b00b78a773
26
.obsidian/workspace.json
vendored
26
.obsidian/workspace.json
vendored
@ -13,12 +13,26 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "markdown",
|
"type": "markdown",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "Lifetimes.md",
|
"file": "Generic Types Traits and Lifetimes.md",
|
||||||
"mode": "source",
|
"mode": "source",
|
||||||
"source": false
|
"source": false
|
||||||
},
|
},
|
||||||
"icon": "lucide-file",
|
"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",
|
"active": "caf0233e624d6c1c",
|
||||||
"lastOpenFiles": [
|
"lastOpenFiles": [
|
||||||
"2025-02-04.md",
|
"Traits.md",
|
||||||
"Lifetimes.md",
|
"Generics.md",
|
||||||
"Generic Types Traits and Lifetimes.md",
|
"Generic Types Traits and Lifetimes.md",
|
||||||
|
"Lifetimes.md",
|
||||||
|
"2025-02-04.md",
|
||||||
"data_types.md",
|
"data_types.md",
|
||||||
"Collection of Common Data Structs.md",
|
"Collection of Common Data Structs.md",
|
||||||
"Constants.md",
|
"Constants.md",
|
||||||
@ -176,7 +192,6 @@
|
|||||||
"Data Types.md",
|
"Data Types.md",
|
||||||
"Enums.md",
|
"Enums.md",
|
||||||
"Error Handling.md",
|
"Error Handling.md",
|
||||||
"Generics.md",
|
|
||||||
"Hash.md",
|
"Hash.md",
|
||||||
"Modules and Use.md",
|
"Modules and Use.md",
|
||||||
"ownership.md",
|
"ownership.md",
|
||||||
@ -188,7 +203,6 @@
|
|||||||
"Reducing_Code_Duplication.md",
|
"Reducing_Code_Duplication.md",
|
||||||
"String.md",
|
"String.md",
|
||||||
"Structures.md",
|
"Structures.md",
|
||||||
"Traits.md",
|
|
||||||
"Variables.md",
|
"Variables.md",
|
||||||
"Vector.md",
|
"Vector.md",
|
||||||
"Reducing.md",
|
"Reducing.md",
|
||||||
|
@ -18,4 +18,33 @@ This chapter covers 3 things
|
|||||||
1. [Reducing Code duplication](Reducing_Code_Duplication.md)
|
1. [Reducing Code duplication](Reducing_Code_Duplication.md)
|
||||||
1. [Generics](Generics.md)
|
1. [Generics](Generics.md)
|
||||||
1. [Traits](Traits.md)
|
1. [Traits](Traits.md)
|
||||||
1. [Lifetimes](Lifetimes.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
|
18
Lifetimes.md
18
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
|
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
|
## 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
|
14
Traits.md
14
Traits.md
@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
This is used to defined shared behavior
|
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
|
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.
|
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
|
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
|
`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
|
In order to do this we need a summary from each type, this would be done by calling a `summarize` method on an instance
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user