programming:rust

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:rust [2020/12/01 20:02] – [Generics] mhprogramming:rust [2021/05/25 22:07] (current) – [Enums] mh
Line 217: Line 217:
 === 'match' workflow === === 'match' workflow ===
  
-In general, ''Option<T>'' is used when you want code that will handle each variant. The ''match'' expression is a control flow construct that does just this when used with enums: it will run different code depending on which variant of the enum it has, and that code can use the data inside the matching value.+In general, we use an ''Option<T>'' when we want to handle all the cases. The ''match'' expression is a control flow construct that does just this when used with enums: it will run different code depending on which variant of the enum it has, and that code can use the data inside the matching value.
  
 Combining ''match'' and ''enum'' is useful in many situations: ''match'' against an ''enum'', bind a variable to the data inside, and then execute code based on it. Combining ''match'' and ''enum'' is useful in many situations: ''match'' against an ''enum'', bind a variable to the data inside, and then execute code based on it.
Line 258: Line 258:
 v.push(5); v.push(5);
 v.push(88); v.push(88);
-//The compiler will infer the i32 type from the values pushed in the vector+//The compiler can infer the i32 type from the values pushed in the vector so the <i32> is not an obligation
 </code> </code>
  
Line 415: Line 415:
 ===== Error Handling ===== ===== Error Handling =====
  
-Switching from an ''.expect()'' call to a ''match'' expression is how you generally move from crashing on an error to handling the error.+<WRAP round info> 
 +**Switching from an ''.expect()'' call to a ''match'' expression is how you generally move from crashing on an error to handling the error.** 
 +</WRAP> 
  
 === Unrecoverable errors : panic! === === Unrecoverable errors : panic! ===
Line 451: Line 454:
  
 Read [[https://doc.rust-lang.org/book/ch09-03-to-panic-or-not-to-panic.html|Chapter 9.3]] for more on when to ''panic!'' or not. Read [[https://doc.rust-lang.org/book/ch09-03-to-panic-or-not-to-panic.html|Chapter 9.3]] for more on when to ''panic!'' or not.
-===== Generics =====+===== 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 ''i32'' or ''String'' or ''char'' Generics are useful for removing duplicate code and write functions or structs that can operate on **abstract types** instead of concrete types like ''i32'' or ''String'' or ''char''
  
 +Generics often use one letter names like ''T'' and are defined as such (function and struct) :
 +
 +<code rust>
 +fn the_function<T>(value: &[T]) -> &T {
 +  //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<T> {
 +  field1: T,
 +  field2: T,
 +}
 +//This struct has two fields of type T
 +</code>
 +
 +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 //monomorphization//. [[https://doc.rust-lang.org/book/ch10-01-syntax.html#performance-of-code-using-generics|More info]]
 +
 +==== Traits ====
 +
 +<WRAP round info>
 +A **trait** tells the Rust compiler about functionality a particular type has and can share with other types.
 +</WRAP>
 +
 +=== Defining ===
 +
 +[[https://doc.rust-lang.org/book/ch10-02-traits.html#defining-a-trait|Chapter 10.2]] details how to create a trait like this one :
 +
 +<code rust>
 +pub trait Summary {
 +    fn summarize(&self) -> String;
 +}
 +</code>
 +
 +Each type implementing the ''Summary'' trait must define it's own custom behavior for the body of the ''summarize'' method. The compiler will enforce this. Note that instead of a '';'' semicolon a default behavior could be set on the trait declaration within ''{}'' curly brackets.
 +
 +To implement a trait on a type :
 +
 +<code rust>
 +impl Summary for SomeStruct {
 +    fn summarize(&self) -> String {
 +        //some code
 +    }
 +}
 +</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://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters|Check here]] for more info.
 +
 +=== Blanket implementations ===
 +
 +They are extensively used in the Rust Standard librarby and allow implementing a trait for any type that implements another trait. [[https://doc.rust-lang.org/book/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods|More info]]
 +
 +==== Lifetimes ====
 ===== Misc ===== ===== Misc =====
  
  • programming/rust.1606849372.txt.gz
  • Last modified: 2020/12/01 20:02
  • by mh