Differences
This shows you the differences between two versions of the page.
| programming:rust [2020/12/13 17:51] – [Vectors] mh | programming:rust [2025/06/02 21:23] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 217: | Line 217: | ||
| === ' | === ' | ||
| - | In general, '' | + | In general, |
| Combining '' | Combining '' | ||
| Line 415: | Line 415: | ||
| ===== Error Handling ===== | ===== Error Handling ===== | ||
| - | Switching from an '' | + | <WRAP round info> |
| + | **Switching from an '' | ||
| + | </ | ||
| === Unrecoverable errors : panic! === | === Unrecoverable errors : panic! === | ||
| Line 476: | Line 479: | ||
| ==== Traits ==== | ==== Traits ==== | ||
| + | |||
| + | <WRAP round info> | ||
| + | A **trait** tells the Rust compiler about functionality a particular type has and can share with other types. | ||
| + | </ | ||
| + | |||
| + | === Defining === | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | <code rust> | ||
| + | pub trait Summary { | ||
| + | fn summarize(& | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Each type implementing the '' | ||
| + | |||
| + | To implement a trait on a type : | ||
| + | |||
| + | <code rust> | ||
| + | impl Summary for SomeStruct { | ||
| + | fn summarize(& | ||
| + | //some code | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | If the trait had a default behavior, this implementation will override it. | ||
| + | |||
| + | Default implementations can also call other methods in the same trait, even if those other methods don't have a default implementation. | ||
| + | |||
| + | === Traits as Parameters === | ||
| + | |||
| + | Traits can be used to define functions that accept many different types who all implement a certain trait. The function is defined using that trait as a parameter, instead of concrete types. [[https:// | ||
| + | |||
| + | === Blanket implementations === | ||
| + | |||
| + | They are extensively used in the Rust Standard librarby and allow implementing a trait for any type that implements another trait. [[https:// | ||
| + | |||
| + | ==== Lifetimes ==== | ||
| ===== Misc ===== | ===== Misc ===== | ||