<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: artech-git</title>
    <description>The latest articles on DEV Community by artech-git (@artechgit).</description>
    <link>https://dev.to/artechgit</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F486358%2Ff6ec7b49-375e-4e6a-96f2-0600a4109f72.png</url>
      <title>DEV Community: artech-git</title>
      <link>https://dev.to/artechgit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/artechgit"/>
    <language>en</language>
    <item>
      <title>Rust Deref Coercion 🦀 simplifying Borrowing and Dereferencing</title>
      <dc:creator>artech-git</dc:creator>
      <pubDate>Wed, 17 May 2023 13:27:32 +0000</pubDate>
      <link>https://dev.to/artechgit/rust-deref-coercion-simplifying-borrowing-and-dereferencing-1a4a</link>
      <guid>https://dev.to/artechgit/rust-deref-coercion-simplifying-borrowing-and-dereferencing-1a4a</guid>
      <description>&lt;p&gt;If you are a newbie or a experienced rust developer you must have come across a special term which represent a amazing features of which is &lt;code&gt;deref coercion&lt;/code&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Rust is a powerful and expressive programming language known for its strong emphasis on memory safety and ownership. One of the language's notable features is deref coercion, which allows Rust to automatically convert types through dereference operations. Deref coercion plays a vital role in simplifying borrowing and dereferencing, making code more concise and readable. In this article, we will explore the concept of deref coercion, its benefits, and how it contributes to Rust's safety guarantees.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Deref and Borrowing:
&lt;/h2&gt;

&lt;p&gt;Before diving into deref coercion, it's essential to understand the concepts of borrowing and dereferencing in Rust. &lt;/p&gt;

&lt;p&gt;Borrowing is a mechanism that allows temporary access to a value without taking ownership. A borrow can be of two types only&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;mutable borrow &lt;code&gt;&amp;amp;val&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;non mutable borrow &lt;code&gt;&amp;amp;mut val&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Rust, we can borrow a value using references denoted by the &lt;code&gt;&amp;amp;&lt;/code&gt; symbol.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// create a borrow of `val`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, dereferencing &lt;code&gt;*&lt;/code&gt; refers to the process of accessing the value behind a reference or a pointer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//original value read is "32" here &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deref Coercion
&lt;/h2&gt;

&lt;p&gt;Deref coercion is Rust's ability to automatically apply dereference operations when calling methods or functions. &lt;/p&gt;

&lt;p&gt;It allows the programmer to write code that operates on a value directly, even if they only have a reference to it. &lt;br&gt;
Deref coercion is achieved through the &lt;code&gt;Deref&lt;/code&gt; trait present std crate for the types which implement it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;call_me&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; 

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;boxed_type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"meow"&lt;/span&gt;&lt;span class="nf"&gt;.to_string&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="nf"&gt;call_me&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;boxed_type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;observe the &lt;code&gt;boxed_type&lt;/code&gt; is a different type altogther yet we allowed to pass it as &amp;amp; to the method we are making the call for &lt;/p&gt;

&lt;p&gt;since &lt;code&gt;Box&lt;/code&gt; type implement &lt;code&gt;Deref&lt;/code&gt; trait which allows it to return the type of generic directly from the &lt;code&gt;deref_target&lt;/code&gt; item type &lt;/p&gt;

&lt;p&gt;and since &lt;code&gt;&amp;amp;String&lt;/code&gt; type implements the &lt;code&gt;Deref&amp;lt;Target = str&amp;gt;&lt;/code&gt; we reach the &lt;code&gt;&amp;amp;str&lt;/code&gt; directly&lt;/p&gt;

&lt;p&gt;The compiler automatically inserts the necessary dereference operations, simplifying the code and reducing the need for explicit dereference syntax.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deref Trait and Operator Overloading
&lt;/h2&gt;

&lt;p&gt;At the heart of deref coercion is the Deref trait. This trait is defined in the Rust standard library and allows types to customize the behavior of the dereference operator (*). By implementing the Deref trait for a type, we provide the necessary instructions on how to dereference instances of that type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deref Coercion Rules:
&lt;/h2&gt;

&lt;p&gt;Rust follows specific rules for deref coercion to ensure type safety. When the compiler encounters a type mismatch, it tries to find a series of Deref trait implementations that can bridge the gap between the expected and actual types. The rules for deref coercion are as follows:&lt;/p&gt;

&lt;p&gt;If the type T implements the Deref trait to produce type U, and U implements the Deref trait to produce type V, Rust will automatically dereference T to get a value of type V when needed.&lt;/p&gt;

&lt;p&gt;Deref coercion only applies to cases where the target type is a reference (&amp;amp;T). It does not work for owned types like Box or Vec. The goal is to avoid unnecessary cloning or moving of values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Deref Coercion:
&lt;/h2&gt;

&lt;p&gt;Deref coercion brings several benefits to Rust programming:&lt;/p&gt;

&lt;p&gt;Improved Readability: By allowing implicit dereferencing, deref coercion simplifies code by reducing the need for explicit dereference operations. This leads to more readable and concise code.&lt;/p&gt;

&lt;p&gt;Reduced Boilerplate: Deref coercion eliminates the need for manually writing dereference syntax when working with types that implement the Deref trait. This reduces unnecessary verbosity and boilerplate code.&lt;/p&gt;

&lt;p&gt;Flexibility and Interoperability: Deref coercion enables seamless interoperability between types that can be dereferenced. It allows different types to be used interchangeably, as long as they implement the necessary Deref trait.&lt;/p&gt;

&lt;p&gt;Safety and Type Checking: Rust's strong type system ensures that deref coercion follows a set of rules, preventing unsafe conversions. The compiler performs static type checking to ensure that the necessary Deref trait implementations exist and are valid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Deref coercion is a powerful feature in Rust that simplifies borrowing and dereferencing. By leveraging the Deref trait and compiler rules, Rust can automatically apply dereference operations when needed, reducing code verbosity and improving code readability.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Creating a heterogenous type for holding several objects of different types in RUST</title>
      <dc:creator>artech-git</dc:creator>
      <pubDate>Tue, 05 Jul 2022 00:50:10 +0000</pubDate>
      <link>https://dev.to/artechgit/creating-a-heterogenous-type-for-holding-several-objects-of-different-types-in-rust-242c</link>
      <guid>https://dev.to/artechgit/creating-a-heterogenous-type-for-holding-several-objects-of-different-types-in-rust-242c</guid>
      <description>&lt;p&gt;Type available within Rust supports composition of homogenous types, these are availabe in types such as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// an vector&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;   &lt;span class="c1"&gt;// an array&lt;/span&gt;
&lt;span class="p"&gt;();&lt;/span&gt;      &lt;span class="c1"&gt;// tuple &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which can be used depending on various scenarios required ,&lt;br&gt;
though you can use it as a heterogenous type but it is naturally constrained by the &lt;a href="https://doc.rust-lang.org/std/marker/trait.Sized.html"&gt;&lt;code&gt;Sized&lt;/code&gt;&lt;/a&gt; trait which is applied to it during compile time which constrain us to further use it dynamically.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;The key thing to note here is we can segregate our types based upon some common relation we can derive from them this will be the key idea to follow along towards achieving so&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;but when it comes to supporting the heterogenous types in rust things get little bit tricky here, to compose a type which can accommodate the objects from several types available using &lt;code&gt;dyn&lt;/code&gt; trait which points the object in vtable which is behind a pointer&lt;br&gt;
you can read &lt;a href="https://doc.rust-lang.org/std/keyword.dyn.html"&gt;here&lt;/a&gt;, &lt;/p&gt;

&lt;p&gt;we want our type to be resizable during runtime therefore I will choose to go with &lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;/code&gt; type which can hold our objects &lt;/p&gt;

&lt;p&gt;since whichever types applies this trait can directly reference a object using two techniques &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1) using a reference to the object&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;   &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="n"&gt;yourTrait&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2) placing the object behind a Box smart pointer&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="n"&gt;youTrait&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;let's say the name of your trait is &lt;code&gt;Foo&lt;/code&gt; and it declares one method to be implement&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now we will create some random types which will implement this &lt;br&gt;
are as follows&lt;br&gt;
(for the sake of simplicity I have defined a tuple struct here but your type can be much more complicated than this ... as long as you implement the trait you should be fine)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"type A: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="na"&gt;.0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"type B: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="na"&gt;.0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;C&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"type C: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="na"&gt;.0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here you will observe one strange thing which is all the types implement the same exact definition of the trait methods but this can be different depending upon the operations you wanna do with that type.&lt;/p&gt;




&lt;p&gt;now lets begin declaring a type which can simply hold them &lt;br&gt;
according the first method&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;1) using the direct reference to the object *&lt;/em&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  one important thing to note here is, the reference you gonna provide for your types will either be mutable or immutable only !
&lt;/h4&gt;

&lt;p&gt;which simply mean, it applies to all the trait objects uniformly and u can't have some trait objects passed on as mutably and others as immutably.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;//some random trait objects declared&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;43&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nf"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;73&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nf"&gt;C&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

     &lt;span class="c1"&gt;// now we will compose our homogenous type based on the previously types&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;objs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;objs&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="nf"&gt;.call&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;since our objects &lt;code&gt;a, b, c&lt;/code&gt; holds a reference to the types &lt;code&gt;A, B, C&lt;/code&gt; respectively we can pass them directly here to construct our homogenous type out of them&lt;br&gt;
output :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;type &lt;/span&gt;A: 43
&lt;span class="nb"&gt;type &lt;/span&gt;B: 73
&lt;span class="nb"&gt;type &lt;/span&gt;C: 95
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;2)  Using Box smart pointer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;now we will try out another interesting method, where we will place our objects behind a &lt;a href="https://doc.rust-lang.org/std/boxed/index.html"&gt;Box&lt;/a&gt; smart pointer in this case you  don't have to worry about initializing your traits objects  during compile itself, instead you will place them behind a pointer pointing to the memory location on heap&lt;br&gt;
since in this technique the rules of borrow doesn't apply there it is quite convinence to work with&lt;br&gt;
also the plus side is our objects can be init. during runtime which is a added advantage&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;//here we place our objects behind a box pointer &lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;43&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;73&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;C&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 

    &lt;span class="c1"&gt;//our type hold a boxed trait object as type to accept&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;objs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;objs&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="nf"&gt;.call&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here the output will be exactly same as the previous one but with more features of box pointer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;type &lt;/span&gt;A: 43
&lt;span class="nb"&gt;type &lt;/span&gt;B: 73
&lt;span class="nb"&gt;type &lt;/span&gt;C: 95
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  there you .. go congratulations you successfully learned how to create a new type &amp;gt;&amp;gt; 
&lt;/h2&gt;

&lt;p&gt;Thank you 😊 so much for reading till here 🥂🎉🎊!&lt;/p&gt;

&lt;p&gt;if you have any comments, suggestion's, doubt or advice feel free  to post in comment section&lt;/p&gt;

</description>
      <category>rust</category>
      <category>webdev</category>
      <category>systems</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
