<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">
    <channel>
        <title>SysAdmin Journal · Rust</title>
        <link>https://sysadmin-journal.com/tag/rust</link>
        <description>Posts tagged with Rust</description>
        <language>en</language>
        <lastBuildDate>Sat, 22 Feb 2020 08:03:13 +0000</lastBuildDate>
        <atom:link href="https://sysadmin-journal.com/tag/rust/rss" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        <item>
            <title>Anatomy of a generic function in Rust</title>
            <link>https://sysadmin-journal.com/anatomy-of-a-generic-rust-function</link>
            <guid isPermaLink="true">https://sysadmin-journal.com/anatomy-of-a-generic-rust-function</guid>
            <pubDate>Sat, 22 Feb 2020 08:03:13 +0000</pubDate>
            <dc:creator>Ish Sookun</dc:creator>
            <category>Rust</category>
            <description>It can handle different input types and thus it&#039;s called a generic function. The generic type is represented by the capital letter T in this example. T is an arbitrary placeholder. It could be have been another letter, X, Y or V...</description>
            <content:encoded><![CDATA[<p>Consider the following function in Rust:</p><pre><code class="language-rust">fn additems&lt;T: Add&lt;Output = T&gt;&gt;(i: T, j: T) -&gt; T {
    i + j
}</code></pre><p>It can handle different input types and thus it's called a <em><strong>generic function</strong></em>. The generic data type is represented by the capital letter T in this example. T is an arbitrary placeholder. It could be have been another letter, X, Y or V, but  when using T it can be easier to remeber that it refers to a "type". </p><p>I don't usually write code but I do enjoy reading and here the syntax of the function definition can be daunting at first. Let's have a look at a simpler version.</p><pre><code class="language-rust">fn additems(i: i32, j: i32) -&gt; i32 {
    i + j
}</code></pre><p>We define a function called <code>additems</code> that accepts two arguments, each of 32-bit signed integer type and the function returns a value of the same type. If 64-bit integers or float values are passed on to the function it will throw a <code>mismatched types</code> error.</p><pre><code class="language-rust">error[E0308]: mismatched types
  --&gt; src/main.rs:13:24
   |
13 |     let sum = additems(5.0,6.0);
   |                        ^^^ expected `i32`, found floating-point number

error[E0308]: mismatched types
  --&gt; src/main.rs:13:28
   |
13 |     let sum = additems(5.0,6.0);
   |                            ^^^ expected `i32`, found floating-point number

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`</code></pre><p>By using generics we can make the function <code>additems</code> accept both float and integer values. Following an example from <a href="https://doc.rust-lang.org/rust-by-example/generics.html">doc.rust-lang.org</a> one would tend to try specifying the generic type T as &lt;T&gt; and start using it.</p><pre><code class="language-rust">fn additems&lt;T&gt;(i: T, j: T) -&gt; T { ... }</code></pre><p>Alas, the compiler would complain about binary operation not possible on type B.</p><pre><code class="language-rust">error[E0369]: binary operation `+` cannot be applied to type `T`
 --&gt; src/main.rs:9:7
  |
9 |     i + j
  |     - ^ - T
  |     |
  |     T
  |
  = note: `T` might need a bound for `std::ops::Add`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0369`.
error: could not compile `namaste`.
</code></pre><p>The compiler is helpful enough to hint that we need to implement the <code>std::ops::Add</code> trait. Adding the following line <code>use std::ops::Add;</code> brings the <strong>Add</strong> trait into scope.</p><p>(More about these traits can be found in the Rust documentation about the <a href="https://doc.rust-lang.org/std/ops/index.html">overloadable operators module</a>.)</p><p>The T type is then bound by the <strong>Add</strong> trait as <code>&lt;T: Add&lt;Output = T&gt;&gt;</code> and the complete block becomes:</p><pre><code class="language-rust">use std::ops::Add;

fn main() {
   let sum = additems(5,6);
   println!("sum: {}", sum);
}

fn additems&lt;T: Add&lt;Output = T&gt;&gt;(i: T, j: T) -&gt; T {
    i + j
}</code></pre><p>The function <code>additems</code> can now accept two integers or two float values and do an <em>addition</em> operation on them.</p>]]></content:encoded>
        </item>
    </channel>
</rss>
