mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-06 19:17:14 -06:00
finished ch10
This commit is contained in:
14
Traits.md
14
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
|
||||
|
||||
|
Reference in New Issue
Block a user