Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision | ||
programming:rust [2020/11/29 18:12] – [Error Handling] mh | programming:rust [2020/12/13 18:22] – [Traits] mh | ||
---|---|---|---|
Line 258: | Line 258: | ||
v.push(5); | v.push(5); | ||
v.push(88); | v.push(88); | ||
- | //The compiler | + | //The compiler |
</ | </ | ||
Line 415: | Line 415: | ||
===== Error Handling ===== | ===== Error Handling ===== | ||
- | Switching from an '' | + | <WRAP round info> |
+ | **Switching from an '' | ||
+ | </ | ||
=== Unrecoverable errors : panic! === | === Unrecoverable errors : panic! === | ||
Line 446: | Line 449: | ||
Using '' | Using '' | ||
- | Error handling inside a function can be // | + | Error handling inside a function can be // |
+ | |||
+ | Returning a '' | ||
+ | |||
+ | Read [[https:// | ||
+ | ===== Generics, Traits, Lifetimes ===== | ||
+ | |||
+ | ==== Generics ==== | ||
+ | |||
+ | Generics are useful for removing duplicate code and write functions or structs that can operate on **abstract types** instead of concrete types like '' | ||
+ | |||
+ | Generics often use one letter names like '' | ||
+ | |||
+ | <code rust> | ||
+ | fn the_function< | ||
+ | //do something in the function with a slice of values of type T then return a reference to a value of type T | ||
+ | } | ||
+ | |||
+ | struct the_struct< | ||
+ | field1: T, | ||
+ | field2: T, | ||
+ | } | ||
+ | //This struct has two fields of type T | ||
+ | </ | ||
+ | |||
+ | Generics can be used in methods as well, or concrete types can be used to define methods that only apply if the generic is of that particular type and won't be available to any other types. | ||
+ | |||
+ | Generics in Rust are very efficient at run-time because the compiler replaces all calls using generics by definitions using concrete types in a process named // | ||
+ | |||
+ | ==== 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. | ||
===== Misc ===== | ===== Misc ===== | ||
- | ==== The ? operator ==== | ||
- | A shortcut for propagating errors back to the calling function. | ||
- | [[https:// | ||
==== Creating custom types to validate certain conditions ==== | ==== Creating custom types to validate certain conditions ==== |