<?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>Forem: Kushal Joshi</title>
    <description>The latest articles on Forem by Kushal Joshi (@kushalj).</description>
    <link>https://forem.com/kushalj</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%2F284078%2F2bf8ba29-b07c-407e-814b-7322e21dc1d4.png</url>
      <title>Forem: Kushal Joshi</title>
      <link>https://forem.com/kushalj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kushalj"/>
    <language>en</language>
    <item>
      <title>Cap'n Proto - RPC at the speed of Rust - Part 2 of 2</title>
      <dc:creator>Kushal Joshi</dc:creator>
      <pubDate>Sun, 12 Jun 2022 15:20:54 +0000</pubDate>
      <link>https://forem.com/kushalj/capn-proto-rpc-at-the-speed-of-rust-part-2-of-2-3n3a</link>
      <guid>https://forem.com/kushalj/capn-proto-rpc-at-the-speed-of-rust-part-2-of-2-3n3a</guid>
      <description>&lt;p&gt;This is Part 2 of a two part article on Cap'n Proto (capnp). I'd strongly suggest reading &lt;a href="https://dev.to/kushalj/capn-proto-rpc-at-the-speed-of-rust-part-1-4joo"&gt;Part 1&lt;/a&gt; so Part 2 makes sense.&lt;/p&gt;

&lt;p&gt;The code is available here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/kushalj/capnproto-demo" rel="noopener noreferrer"&gt;https://github.com/kushalj/capnproto-demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Title photo by &lt;a href="https://unsplash.com/@andersjilden?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Anders Jildén&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/motion?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;As a reminder, this is our example capnp schema:&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="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0xb068ff5fb1c4f77e&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;Rust&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"rust.capnp"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;Rust&lt;/span&gt;&lt;span class="nf"&gt;.parentModule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Float32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Float32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;PointTracker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;addPoint&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;totalPoints&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;UInt64&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;h1&gt;
  
  
  Setting up the client and server
&lt;/h1&gt;

&lt;p&gt;The objective here it so use the Cap'n Proto RPC features and not explore setting up a server so the Tokio code below is copied from the capnp-rpc examples and modified slightly towards simplification so we can explore the capnp-rpc features and see them in the context of the Tokio code.&lt;/p&gt;

&lt;p&gt;Starting with the code at the end of &lt;a href="https://dev.to/kushalj/capn-proto-rpc-at-the-speed-of-rust-part-1-4joo"&gt;Part 1&lt;/a&gt;, we have this as our current server.rs:&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;#![allow(dead_code)]&lt;/span&gt;

&lt;span class="nd"&gt;#[path&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"./schema/point_capnp.rs"&lt;/span&gt;&lt;span class="nd"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;point_capnp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;point_demo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;server&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;point_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;write_to_stream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;message&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new_default&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;demo_point&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="py"&gt;.init_root&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nn"&gt;point&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.set_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5_f32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.set_y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;10_f32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// This Result should be consumed properly in an actual app&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;write_message&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;mut&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdout&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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Save the point&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;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"point.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&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;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;write_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Read the point from file&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;point_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"point.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="c1"&gt;// We want this to panic in our demo incase there is an issue&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;point_reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                &lt;span class="nn"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;read_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;point_file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;message&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;ReaderOptions&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;.unwrap&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;demo_point&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;point&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;point_reader&lt;/span&gt;&lt;span class="nf"&gt;.get_root&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&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;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;(x = {}, y = {})"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.get_x&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.get_y&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nf"&gt;Ok&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;The code is suppressing Results with unwraps and is dirtier than a cloth seat on UK public transport but that's ok - we are just using this to gain intuition. When the time comes to use capnp in production, you will not code like this. Please don't code like this.&lt;/p&gt;

&lt;p&gt;Moving on, Cap'n Proto (capnp) facilitates RPC via a tiny built-in concurrency event-loop, very similar to JavaScripts's called KJ. An odd discovery for me, as these are my initials - but the docs say they don't stand for anything and were chosen as they were easy to type. 😕 Why not give it &lt;em&gt;some&lt;/em&gt; kind of useful name? Names are strongly related to learning and knowledge transition. This mildly irritating - but not really that important as, fortunately, the Rust version of capnp has the sensible name &lt;code&gt;RPCSystem&lt;/code&gt; which makes the code a little more readable.&lt;/p&gt;

&lt;p&gt;Within the capnproto-rust repo, there is a capnp-rpc folder with some example of how to use the RPC system. The Docs refer to something called EzyRPCServer and this no longer exists in the code but there are some examples of Tokio based async servers, which is a good thing as that is how we should build this anyway (with Tokio), rather than abstracting away some magical server code.&lt;/p&gt;

&lt;p&gt;We can start by changing main.rs to be a simplified version of the capnp-rpc hello-world example:&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;#[tokio::main(flavor&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"current_thread"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&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;args&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;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="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;args&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s"&gt;"client"&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nn"&gt;client&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"server"&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nn"&gt;server&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&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;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"usage: {} [client | server] ADDRESS"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&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;So now we need to adjust Cargo.toml:&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="o"&gt;...&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dependencies&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;capnp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.14"&lt;/span&gt;
&lt;span class="n"&gt;capnp&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;rpc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.14"&lt;/span&gt;
&lt;span class="n"&gt;tokio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;features&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"rt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"macros"&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
&lt;span class="n"&gt;tokio&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;util&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.6.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;features&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"compat"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;futures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.3.0"&lt;/span&gt;

&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...and add a skeleton main() to server.rs:&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;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;net&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ToSocketAddrs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;...&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&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;args&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;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="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;args&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;3&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;"usage: {} server ADDRESS:PORT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(());&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;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="nf"&gt;.to_socket_addrs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"could not parse address"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;task&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;LocalSet&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;.run_until&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;move&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;_listener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;net&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;TcpListener&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;bind&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;addr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="o"&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;"Server running"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;loop&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="k"&gt;.await&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(_listener has an underscore to remove warnings. We will remove this underscore shortly)&lt;/p&gt;

&lt;p&gt;...and finally we need to add skeleton a client.rs:&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;#![allow(dead_code)]&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;futures&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AsyncReadExt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;net&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ToSocketAddrs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[path&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"./schema/point_capnp.rs"&lt;/span&gt;&lt;span class="nd"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;point_capnp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&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;args&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;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="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;args&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;3&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;"usage: {} client HOST:PORT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(());&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;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="nf"&gt;.to_socket_addrs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"could not parse address"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;task&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;LocalSet&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;.run_until&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;move&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;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;net&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;TcpStream&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;connect&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;addr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="o"&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;"Connected to TCP Stream"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="nf"&gt;.set_nodelay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_reader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_writer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                &lt;span class="nn"&gt;tokio_util&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;compat&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;TokioAsyncReadCompatExt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;compat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.split&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="c1"&gt;// RPC code&lt;/span&gt;

            &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="k"&gt;.await&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(_reader and _writer have underscores to remove warnings. We will remove these underscores shortly)&lt;/p&gt;

&lt;p&gt;These are the basics (from the capnp example code) to create a network connection and pass a message (I've excluded capnp code so we can build that up gradually and you can get an idea of what is boiler-plate and what is specific to our capnp client/server example).&lt;/p&gt;

&lt;p&gt;Let's test the client:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ cargo run client 0.0.0.0:8080
   Compiling capnproto-demo v0.1.0 &lt;span class="o"&gt;(&lt;/span&gt;/Users/kushaljoshi/code/rust/capnproto/capnproto-demo&lt;span class="o"&gt;)&lt;/span&gt;
    Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;1.46s
     Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/capnproto-demo client &lt;span class="s1"&gt;'0.0.0.0:8080'&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
Error: Os &lt;span class="o"&gt;{&lt;/span&gt; code: 61, kind: ConnectionRefused, message: &lt;span class="s2"&gt;"Connection refused"&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;Connection refused&lt;/code&gt; error makes sense as the Server is not running - and functions as an adequate failing test to let us know the client tried to connect. We can open another terminal and run the server from the same folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ cargo run server 0.0.0.0:8080
    Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;0.09s
     Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/capnproto-demo server &lt;span class="s1"&gt;'0.0.0.0:8080'&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
Server running

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...and now the client again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ cargo run client 0.0.0.0:8080
    Finished dev [unoptimized + debuginfo] target(s) in 0.09s
     Running `target/debug/capnproto-demo client '0.0.0.0:8080'`
Connected to TCP Stream
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's better. This seems to indicate that the client is connecting to the server (there is no error on connection), so we can assume we have a basic server and a client that can connect to it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Magical flying clients
&lt;/h1&gt;

&lt;p&gt;At this point the &lt;a href="https://capnproto.org/cxxrpc.html" rel="noopener noreferrer"&gt;capnp docs describe how to use the RPC framework in C++ only&lt;/a&gt; at this time, so I had to dive into the code sample and breakdown the intention of the code and rummage around to see what is happening just under the surface. I tried to refer back to the C++ docs but there are numerous differences in usage and implementation.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/capnproto/capnproto-rust/tree/master/capnp-rpc" rel="noopener noreferrer"&gt;capnp-rpc README&lt;/a&gt; has some information that helps and contains the following magical description: ""&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Then you can convert your object into a capability client like this:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(capnp example)&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;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;foo_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;capnp_rpc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new_client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MyBar&lt;/span&gt; &lt;span class="p"&gt;{});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This new client can now be sent across the network. You can use it as the bootstrap capability when you construct an RpcSystem, and you can pass it in RPC method arguments and results.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Say what now?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So my &lt;em&gt;server&lt;/em&gt; makes a &lt;em&gt;client&lt;/em&gt; and sends the &lt;em&gt;client&lt;/em&gt; to the... &lt;em&gt;client&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;Ok sure, I think I follow. Let's make the server-client thing then:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The generated code also includes a Server trait for each of your interfaces. To create an RPC-enabled object, you must implement that trait.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So for this schema (capnp example):&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="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0xa7ed6c5c8a98ca40&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;Bar&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;baz&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Int32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Int32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;Qux&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;quux&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bar&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Bar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Int32&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;I need to build this (capnp example):&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;struct&lt;/span&gt; &lt;span class="n"&gt;MyBar&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;foo_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Server&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;MyBar&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;baz&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;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;foo_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;BazParams&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;foo_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;BazResults&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&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;// `pry!` is defined in capnp_rpc. It's analogous to `try!`.&lt;/span&gt;
         &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.set_y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;pry!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="nf"&gt;.get_x&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

         &lt;span class="nn"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;ok&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;That looks ok - the generated code seems to have types for the params and results of the baz interface method already. Let's start with that for our &lt;code&gt;PointTracker&lt;/code&gt; interface. We will call it &lt;code&gt;PointTrackerImpl&lt;/code&gt; rather than MyPoint (which is also the convention in the rest of the examples):&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;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;PointTrackerImpl&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;points&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="n"&gt;Point&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nn"&gt;point_tracker&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Server&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;PointTrackerImpl&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;add_point&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;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;point_tracker&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AddPointParams&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;point_tracker&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AddPointResults&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;point_client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;pry!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="nf"&gt;.get_p&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;received_point&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;point_client&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.points&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;received_point&lt;/span&gt;&lt;span class="nf"&gt;.get_x&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
                &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;received_point&lt;/span&gt;&lt;span class="nf"&gt;.get_y&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="n"&gt;results&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.set_total_points&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.points&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nn"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;ok&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;(This code is in server.rs)&lt;/p&gt;

&lt;p&gt;and now we can point at this point-tracker from our server code, using the boiler-plate from the hello-world example:&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;use&lt;/span&gt; &lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capability&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;capnp_rpc&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;pry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rpc_twoparty_capnp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;twoparty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RpcSystem&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;futures&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;AsyncReadExt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FutureExt&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;net&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ToSocketAddrs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;span class="c1"&gt;// existing code...&lt;/span&gt;

&lt;span class="o"&gt;...&lt;/span&gt;


&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&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;args&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;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="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;args&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;3&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;"usage: {} server ADDRESS:PORT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(());&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;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="nf"&gt;.to_socket_addrs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"could not parse address"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;task&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;LocalSet&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;.run_until&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;move&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;listener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;net&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;TcpListener&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;bind&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;addr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="c1"&gt;// Cap'n Proto point_tracker client initialised here&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;point_tracker_client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;point_tracker&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                &lt;span class="nn"&gt;capnp_rpc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new_client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PointTrackerImpl&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Vec&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="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;"Server running"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;loop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="nf"&gt;.accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="nf"&gt;.set_nodelay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                    &lt;span class="nn"&gt;tokio_util&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;compat&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;TokioAsyncReadCompatExt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;compat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.split&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;network&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;twoparty&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;VatNetwork&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="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="nn"&gt;rpc_twoparty_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Side&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="nn"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(),&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;rpc_system&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                    &lt;span class="nn"&gt;RpcSystem&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="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="n"&gt;network&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;point_tracker_client&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="py"&gt;.client&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

                &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;task&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;spawn_local&lt;/span&gt;&lt;span class="p"&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;pin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rpc_system&lt;/span&gt;&lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;_&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="p"&gt;})&lt;/span&gt;
        &lt;span class="k"&gt;.await&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(This code is in server.rs)&lt;/p&gt;

&lt;p&gt;And our server is nearly done!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fesd4xev9k8iadhykrcz7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fesd4xev9k8iadhykrcz7.png" alt="Image description" width="800" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Vat?
&lt;/h2&gt;

&lt;p&gt;If that was a little bit like drawing an owl, I understand - It's ok, we're going to break that last bunch of code down a bit and explain what the Cap'n is doing.&lt;/p&gt;

&lt;p&gt;The Promise we are using above is part of the RPC system and facilitates the async functionality within the RPC runtime. The &lt;em&gt;actual&lt;/em&gt; response, as you will be able to figure out eventually after some head-scratching, is the &lt;code&gt;results.get()&lt;/code&gt; function and the other code it contains.&lt;/p&gt;

&lt;p&gt;So in the &lt;code&gt;add_point()&lt;/code&gt; &lt;code&gt;point_tracker::Server&lt;/code&gt; trait implementation of &lt;code&gt;PointerTrackerImpl&lt;/code&gt; we are doing the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get the point-client from the request which should give us a Result&lt;/li&gt;
&lt;li&gt;If the Result is ok, grab the x &amp;amp; y  coordinates for the point directly from the message&lt;/li&gt;
&lt;li&gt;Save the total number of points as a new message&lt;/li&gt;
&lt;li&gt;Send the message as the response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This part is a little fiddly and I had to fart around with the available helper code and examples to shape this. I'm not 100% happy with it but it will do as an example for our purposes here.&lt;/p&gt;

&lt;p&gt;So this is our "Server" but it needs to be Served somehow. In the main() function of server.rs we set up something a little odd now. &lt;em&gt;A two-party-vat network&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To explain this I will first share some information from the capnp docs site. Half way through a list of features on the RPC Protocol page there is a vague reference:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Three-way interactions. A network of Cap’n Proto vats (nodes) can pass object references to each other and automatically form direct connections as needed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In case you missed it, this is the (only?) reference to the 'Vat' word we can see in the code. Apparently a Vat is a node. Cool.  Not sure why they didn't call a node a.. "Node". 🤷 But ok. Now we know.&lt;/p&gt;

&lt;p&gt;So this magical code...&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;network&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;twoparty&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;VatNetwork&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="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="nn"&gt;rpc_twoparty_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Side&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="nn"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;default&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;...takes an input stream &amp;amp; output stream (the TCPListener stream that we wrapped with Tokio (out of scope of this article - please go explore Tokio articles) a Side (Server or Client) and some internal options. It's easier to see this with the Types annotated by Rust Analyzer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5w94xegrdp7vop30vt38.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5w94xegrdp7vop30vt38.png" alt="Image description" width="800" height="198"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It returns a capnp RPC vat (node) network.&lt;/p&gt;

&lt;p&gt;Then we set up the RPC system:&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;rpc_system&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
   &lt;span class="nn"&gt;RpcSystem&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="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="n"&gt;network&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;point_tracker_client&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="py"&gt;.client&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A lot going on there, Let's get the Rust Analyzer view again:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpp2c8iua6t17kqrskx5c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpp2c8iua6t17kqrskx5c.png" alt="Image description" width="800" height="55"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok we have a new RPC System that contains a pointer (Box) to the VatNetwork we made above. It takes a "bootstrap" param which is our server-client thing we made earlier. And we may need to cycle around the loop again if the &lt;code&gt;spawn&lt;/code&gt;'d task ends, so we clone it.&lt;/p&gt;

&lt;p&gt;Finally we spawn the task and can call &lt;code&gt;map()&lt;/code&gt; on the RPC System to start it. Another mild oddity but I can live with it. I guess it make sense if I want to process the return value of the RPC System for some reason; then this nests it neatly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the RPC Client
&lt;/h2&gt;

&lt;p&gt;The client code is similarly built except there is no loop. I left a comment in the code above that says &lt;code&gt;//RPC code&lt;/code&gt; and left a space for the capnp-rpc code. This code looks as follows:&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;rpc_network&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="nn"&gt;twoparty&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;VatNetwork&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="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nn"&gt;rpc_twoparty_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Side&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nn"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(),&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;rpc_system&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;RpcSystem&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="n"&gt;rpc_network&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;None&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;point_tracker&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;point_tracker&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                &lt;span class="n"&gt;rpc_system&lt;/span&gt;&lt;span class="nf"&gt;.bootstrap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;rpc_twoparty_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Side&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;task&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;spawn_local&lt;/span&gt;&lt;span class="p"&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;pin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rpc_system&lt;/span&gt;&lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;|&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;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;point_tracker&lt;/span&gt;&lt;span class="nf"&gt;.add_point_request&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="c1"&gt;// let's make a Point:&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;message&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new_default&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;new_point&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="py"&gt;.init_root&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nn"&gt;point&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;new_point&lt;/span&gt;&lt;span class="nf"&gt;.set_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5_f32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;new_point&lt;/span&gt;&lt;span class="nf"&gt;.set_y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;10_f32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.set_p&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_point&lt;/span&gt;&lt;span class="nf"&gt;.into_reader&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;?&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;reply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="nf"&gt;.send&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="py"&gt;.promise&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&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;"Total points in Point Tracker: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.get_total_points&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;So again we are making a two-party-vat-network (node network) but this time we are using the capnp config Side enum of Client. We then boot the RPC System with the network and a &lt;code&gt;None&lt;/code&gt; because we don't have server code on this side to attach.&lt;/p&gt;

&lt;p&gt;In fact this is a mental hook for understanding what's happening under the surface: The &lt;code&gt;None&lt;/code&gt; is replaced by a &lt;em&gt;capability&lt;/em&gt; on the server side (our point-tracker server-client thingy, which is a &lt;code&gt;capability::Client&lt;/code&gt;) - but because we are currently building the client side of the RPC, there is no capability to bootstrap the RPC system with. We just want to connect and call a remote procedure - but before that we need the interface from the other side.&lt;/p&gt;

&lt;p&gt;The next line confusingly also says "bootstrap"  (same name as the RpcSystem::new() param). Yes, there are two things called Client and two things called bootstrap. Serious name-grokking challenge in this project but it's still evolving so I will appreciate what it does and not how it looks...&lt;/p&gt;

&lt;p&gt;Anyway, back to &lt;code&gt;rpc_system.bootstrap()&lt;/code&gt;. This one is interesting - it's the opposite of the other bootstrap (which is a weak argument for the function having the same name as a param...). This function sends a vat (node) ID and responds with the "bootstrap interface" of the other side! This is why the result is attached to the point_tracker variable.&lt;/p&gt;

&lt;p&gt;You might notice that we have to explicitly state the type as &lt;code&gt;point_tracker::Client&lt;/code&gt;. This does actually make sense. If you remove the explicit type, this happens, two lines down:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5iwb32v6lc0kvj2cjhc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5iwb32v6lc0kvj2cjhc.png" alt="Image description" width="800" height="233"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The compiler has no idea if point_tracker &lt;em&gt;has&lt;/em&gt; an &lt;code&gt;add_point_request()&lt;/code&gt; function and what it might return - because this is just code. Nothing has been sent yet!&lt;/p&gt;




&lt;h1&gt;
  
  
  Let's send some points!
&lt;/h1&gt;

&lt;p&gt;Once the RPC System is up and running (with the &lt;code&gt;.map()&lt;/code&gt; as before), we can send our message.&lt;/p&gt;

&lt;p&gt;To make the request we use the &lt;code&gt;add_point_request()&lt;/code&gt; function which is provided in the generated code.&lt;/p&gt;

&lt;p&gt;Unfortunately a lot of the examples use on-the-fly creation of the capnp message they are sending. This is not practical if the message needs to be created by some other code in our application. So in the example, I'm manually creating a message and initialising it with point::Builder, as we did in &lt;a href="https://dev.to/kushalj/capn-proto-rpc-at-the-speed-of-rust-part-1-4joo"&gt;Part 1&lt;/a&gt;, and then setting this as the message in the request.&lt;/p&gt;

&lt;p&gt;Finally, we send the message and use the RPC promise system as our async approach to await the response. And that's it!&lt;/p&gt;

&lt;p&gt;If we run the server in one terminal window, we get the confirmation message that it is running:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6x4ou6as39v7twa6hv3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6x4ou6as39v7twa6hv3.png" alt="Image description" width="800" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And if we  run the client from another terminal window, we can start filling up the Point Tracker with points!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyo5vqb6higd6law8qcqf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyo5vqb6higd6law8qcqf.png" alt="Image description" width="800" height="133"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can keep running the client and we can see the total points increasing as the server stores the points we are sending.&lt;/p&gt;

&lt;h1&gt;
  
  
  Final Words
&lt;/h1&gt;

&lt;p&gt;This write up took a little longer than I planned because, to be frank, the Cap'n Proto documentation (for Rust at least) is a bit pants at the moment (a UK colloquialism for "it needs some work"). However, I really like this project and really, I've just touched the surface of what it's capable of in this small tour. With some better documentation and some evolution around its  RPC features, I think this is going to be one to watch. It feels oddly cool to have the ability to extract data directly from network messages. I'm going to integrate capnp into a project I'm working on and I may do a second write-up at that point.&lt;/p&gt;

&lt;p&gt;There should be a "so what" at the end I guess. There is: From an abstract perspective, network data requires conversion to a serialised stream of bytes that can be sent via some protocol that can reconstruct (deserialise) that data back into something a network node can consume for its data processing purposes. As we progress down the road of gigantic user data (my phone has half a TB of memory, don't tell me we're not going there - and Genshin Impact does a multi-gigabyte update every time you load it) combined with an increasing need to provide information &lt;em&gt;immediately&lt;/em&gt;, there is one step in the process we will be able to remove completely with Cap'n Proto - the deconstruction/reconstruction steps. Similarly to Rust's energy saving side effect, this probably makes Cap'n Proto the most energy efficient protocol out there. I can imagine the Cap'n is going to be a big deal just a little further down that road.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>data</category>
      <category>network</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Cap'n Proto - RPC at the speed of Rust - Part 1 of 2</title>
      <dc:creator>Kushal Joshi</dc:creator>
      <pubDate>Sun, 15 May 2022 13:29:48 +0000</pubDate>
      <link>https://forem.com/kushalj/capn-proto-rpc-at-the-speed-of-rust-part-1-4joo</link>
      <guid>https://forem.com/kushalj/capn-proto-rpc-at-the-speed-of-rust-part-1-4joo</guid>
      <description>&lt;p&gt;We're entering into a kind of niche subject here but I want to make it more accessible if I can. At the time of writing this post, at &lt;a href="https://keyrock.eu/" rel="noopener noreferrer"&gt;my work&lt;/a&gt; we've been using GRPC for a few years, because at the time when we made the decision, it was simple, fast, had some Rust support, and corporate sponsorship (it used to be that G stood for Google...). It's not that we've reached the end of using GRPC but I keep wondering what else is possible, as our work context requires us to go faster and faster.&lt;/p&gt;

&lt;p&gt;Title photo by &lt;a href="https://unsplash.com/@emrekaratas?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Emre Karataş&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/motion?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GRPC is a binary RPC protocol that serialises pretty damn quick and transfers data more than fast enough across our global system. We could use something faster but, to date, we've had other fish to fry in the area of optimisation.&lt;/p&gt;

&lt;p&gt;However, today I have some personal projects that need a data protocol and my leaning is to go with RPC. I need &lt;em&gt;speed&lt;/em&gt; and I want it to be well supported and simple to integrate.&lt;/p&gt;

&lt;p&gt;While it would be easy to use GRPC again, I want to try out another RPC protocol - &lt;a href="https://capnproto.org" rel="noopener noreferrer"&gt;Cap'n Proto&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Why? because the author of Cap'n Proto was one of the original authors of ProtoBuf 2 (Protocol Buffers 2) which is the open source serialisation format used in GRPC. Also because Cap'n Proto claims to have no serialisation/deserialisation at all once a message is created, which means it should be very fast for transferring data around a distributed system. This includes consideration for saving that same data structure that doesn't need serialisation. It's apparently all handled by the protocol definition, all the way down to the &lt;a href="https://en.wikipedia.org/wiki/Endianness" rel="noopener noreferrer"&gt;endian&lt;/a&gt; consideration for saved data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe6wfnrs5rc67b5g31bwi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe6wfnrs5rc67b5g31bwi.png" alt="Image description" width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://capnproto.org" rel="noopener noreferrer"&gt;Cap'n Proto site&lt;/a&gt; is both funny and full of information as to why it was created - but I can't immediately find a way to get an example up and running or to understand how to convert my app's types to Cap'n Proto types. I think everything I need should be there as I can see there is a section on Encoding which should explain this.&lt;/p&gt;

&lt;p&gt;The only hurdle I have is that while the documentation is extensive it is a little confusing in places and mainly focuses on C++ and the C++ RPC system which is a little different to the Rust code. There are Rust examples in the &lt;a href="https://github.com/capnproto/capnproto-rust" rel="noopener noreferrer"&gt;official repo&lt;/a&gt; which I will try and leverage here.&lt;/p&gt;




&lt;h2&gt;
  
  
  Installing Cap'n Proto
&lt;/h2&gt;

&lt;p&gt;There is a note on their site that Homebrew can be used to install on a Mac. But at the time of writing I couldn't figure out &lt;em&gt;what&lt;/em&gt; to install.&lt;/p&gt;

&lt;p&gt;After some hunting I found that we need the relevant tool to process the Cap'n Proto (capnp) Schema files: &lt;a href="https://capnproto.org/capnp-tool.html" rel="noopener noreferrer"&gt;https://capnproto.org/capnp-tool.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I found this can be installed on a Mac with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;capnp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don't have Homebrew for you Mac, go here: &lt;a href="https://brew.sh/" rel="noopener noreferrer"&gt;https://brew.sh/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you don't have a Mac, there are installation instructions here: &lt;a href="https://capnproto.org/install.html" rel="noopener noreferrer"&gt;https://capnproto.org/install.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;once installed we can make sure it runs and look at the help:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;capnp &lt;span class="nt"&gt;--help&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&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;Usage: capnp &lt;span class="o"&gt;[&lt;/span&gt;&amp;lt;option&amp;gt;...] &amp;lt;&lt;span class="nb"&gt;command&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&amp;lt;arg&amp;gt;...]

Command-line tool &lt;span class="k"&gt;for &lt;/span&gt;Cap&lt;span class="s1"&gt;'n Proto development and debugging.

Commands:
  compile  Generate source code from schema files.
  convert  Convert messages between binary, text, JSON, etc.
  decode   DEPRECATED (use `convert`)
  encode   DEPRECATED (use `convert`)
  eval     Evaluate a const from a schema file.
  id       Generate a new unique ID.

See '&lt;/span&gt;capnp &lt;span class="nb"&gt;help&lt;/span&gt; &amp;lt;&lt;span class="nb"&gt;command&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;' for more information on a specific command.

Options:
    -I&amp;lt;dir&amp;gt;, --import-path=&amp;lt;dir&amp;gt;
        Add &amp;lt;dir&amp;gt; to the list of directories searched for non-relative imports
        (ones that start with a '&lt;/span&gt;/&lt;span class="s1"&gt;').
    --no-standard-import
        Do not add any default import paths; use only those specified by -I.
        Otherwise, typically /usr/include and /usr/local/include are added by
        default.
    --verbose
        Log informational messages to stderr; useful for debugging.
    --version
        Print version information and exit.
    --help
        Display this help text and exit.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok it's working. What do I do now?&lt;/p&gt;

&lt;p&gt;I guess we can start with a message example.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://capnproto.org/language.html" rel="noopener noreferrer"&gt;docs&lt;/a&gt; say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cap’n Proto messages are strongly-typed and not self-describing. You must define your message structure in a special language, then invoke the Cap’n Proto compiler.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok let's have a look at the &lt;a href="https://capnproto.org/capnp-tool.html" rel="noopener noreferrer"&gt;compiler tool docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It says I can do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;capnp compile &lt;span class="nt"&gt;-oc&lt;/span&gt;++ myschema.capnp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is fine but I want Rust, not C++ code which this command seems to generate. Looking around, there is a bunch Rust crates that I think will help, plus an examples folder, all in this repo:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/capnproto/capnproto-rust" rel="noopener noreferrer"&gt;https://github.com/capnproto/capnproto-rust&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But the example contains an ID in the schema file, so I'm not sure if I need to generate this or it is generated by the tool and... inserted into the schema?&lt;/p&gt;

&lt;p&gt;Some more hunting around and text searching for "generate" brought me to the language page where I found this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcoehjmmc54gxpfup2h7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcoehjmmc54gxpfup2h7.png" alt="Image description" width="800" height="648"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So it looks like I need to generate at least 1 id and put it in my schema.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ capnp &lt;span class="nb"&gt;id&lt;/span&gt;
@0xb068ff5fb1c4f77e&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And let's use the example from the &lt;a href="https://github.com/capnproto/capnproto-rust" rel="noopener noreferrer"&gt;capnproto-rust repo&lt;/a&gt;, but with our ID:&lt;/p&gt;

&lt;p&gt;I will call this file src/schema/point.capnp&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="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0xb068ff5fb1c4f77e&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Float32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Float32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;PointTracker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;addPoint&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;totalPoints&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;UInt64&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;What does this describe? It looks like an RPC call to add a Point (with x &amp;amp; y coords defined as f32's) to something like a list of points, and it returns the totalPoints, which is a u64. As this type is not a collection I will assume it means the total-number-of-points.&lt;/p&gt;

&lt;p&gt;Quick review of the schema basics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Capnp comments use a "#"&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The capnp types are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Void: Void&lt;/li&gt;
&lt;li&gt;Boolean: Bool&lt;/li&gt;
&lt;li&gt;Integers: Int8, Int16, Int32, Int64&lt;/li&gt;
&lt;li&gt;Unsigned integers: UInt8, UInt16, UInt32, UInt64&lt;/li&gt;
&lt;li&gt;Floating-point: Float32, Float64&lt;/li&gt;
&lt;li&gt;Blobs: Text (UTF8 NUL terminated), Data&lt;/li&gt;
&lt;li&gt;Lists: List(T) - the T is a Capnp built-in or defined capnp Schema Struct&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Struct fields are consecutively numbered (like protobuf) - but with an "@"&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;There are Enums but also Unions.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Interfaces wrap methods (the &lt;code&gt;PointTracker&lt;/code&gt; interface above contains &lt;code&gt;addPoint&lt;/code&gt; method)&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;".capnp" files can import other ".capnp" files&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Types for a field are declared with a :colon&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  The plan
&lt;/h2&gt;

&lt;p&gt;As a rough plan, I want to be able to serve this interface and use or save the file in some way as a demo of the capnp capabilities. The challenge will be to make it as simple as possible so it facilitates what is an exploratory reference (for me at least) and hopefully some info/learning for anyone else looking at this protocol or learning/exploring Rust.&lt;/p&gt;

&lt;p&gt;I've now made a &lt;code&gt;cargo new&lt;/code&gt; project folder and added a src/schema folder for the file above. &lt;/p&gt;

&lt;p&gt;In case generating a capnp ID sounds like a pain - the vscode-capnp  extension for vs-code can generate a capnp ID anytime you need it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxyf3cwf94e3fzj59bkei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxyf3cwf94e3fzj59bkei.png" alt="Image description" width="800" height="98"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;(In fact I accidentally found out later that if you forget, the compiler throws an error &lt;em&gt;and&lt;/em&gt; generates the ID for you so you can just copy and paste it in)&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating a Cap'n Proto Schema
&lt;/h2&gt;

&lt;p&gt;Let's see what the cli tool says about compiling now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ capnp &lt;span class="nb"&gt;help &lt;/span&gt;compile
Usage: capnp compile &lt;span class="o"&gt;[&lt;/span&gt;&amp;lt;option&amp;gt;...] &amp;lt;&lt;span class="nb"&gt;source&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;...

Compiles Cap&lt;span class="s1"&gt;'n Proto schema files and generates corresponding source code in one
or more languages.

Options:
    -I&amp;lt;dir&amp;gt;, --import-path=&amp;lt;dir&amp;gt;
        Add &amp;lt;dir&amp;gt; to the list of directories searched for non-relative imports
        (ones that start with a '&lt;/span&gt;/&lt;span class="s1"&gt;').
    --no-standard-import
        Do not add any default import paths; use only those specified by -I.
        Otherwise, typically /usr/include and /usr/local/include are added by
        default.
    -o&amp;lt;lang&amp;gt;[:&amp;lt;dir&amp;gt;], --output=&amp;lt;lang&amp;gt;[:&amp;lt;dir&amp;gt;]
        Generate source code for language &amp;lt;lang&amp;gt; in directory &amp;lt;dir&amp;gt; (default:
        current directory).  &amp;lt;lang&amp;gt; actually specifies a plugin to use.  If
        &amp;lt;lang&amp;gt; is a simple word, the compiler searches for a plugin called
        '&lt;/span&gt;capnpc-&amp;lt;lang&amp;gt;&lt;span class="s1"&gt;' in $PATH.  If &amp;lt;lang&amp;gt; is a file path containing slashes,
        it is interpreted as the exact plugin executable file name, and $PATH is
        not searched.  If &amp;lt;lang&amp;gt; is '&lt;/span&gt;-&lt;span class="s1"&gt;', the compiler dumps the request to
        standard output.
    --src-prefix=&amp;lt;prefix&amp;gt;
        If a file specified for compilation starts with &amp;lt;prefix&amp;gt;, remove the
        prefix for the purpose of deciding the names of output files.  For
        example, the following command:
            capnp compile --src-prefix=foo/bar -oc++:corge foo/bar/baz/qux.capnp
        would generate the files corge/baz/qux.capnp.{h,c++}.
    --verbose
        Log informational messages to stderr; useful for debugging.
    --version
        Print version information and exit.
    --help
        Display this help text and exit.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Aha:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the compiler searches for a plugin called 'capnpc-' in $PATH...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not sure if I have that. Let's see what the autocomplete finds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ capnpc
capnpc        capnpc-c++    capnpc-capnp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nope. Ok let's install capnpc-rust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I couldn't find anything about needing to install this. Maybe it's magical and I can just select Rust as the language:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ capnp compile &lt;span class="nt"&gt;-orust&lt;/span&gt; src/schema/point-schema.capnp
rust: no such plugin &lt;span class="o"&gt;(&lt;/span&gt;executable should be &lt;span class="s1"&gt;'capnpc-rust'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
rust: plugin failed: &lt;span class="nb"&gt;exit &lt;/span&gt;code 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yup, it's not magical.&lt;/p&gt;

&lt;p&gt;Hmm... maybe it's a Cargo crate?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ cargo install capnpnc-rust
    Updating crates.io index
error: could not find `capnpnc-rust` in registry `crates-io` with version `*`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nope.&lt;/p&gt;

&lt;p&gt;Ok maybe I'm going about this the wrong way. I guess I could compile the capnpc-rust to a binary by cloning the repo but that may be unnecessary as what I &lt;em&gt;really&lt;/em&gt; want is to compile it from within my own code. Isn't it? 🤷 - This is just a guess from reading the capnproto-rust repo:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ghsvtv4sfczz76dlwo3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ghsvtv4sfczz76dlwo3.png" alt="Image description" width="800" height="68"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's also strongly hinted at in the &lt;a href="https://docs.rs/capnpc/0.14.7/capnpc/" rel="noopener noreferrer"&gt;capnproto-rust docs&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr58wgv0opuu5m7mdlnex.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr58wgv0opuu5m7mdlnex.png" alt="Image description" width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can try...&lt;/p&gt;

&lt;p&gt;&lt;code&gt;crate::Cargo.toml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[package]&lt;/span&gt;
&lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"capnproto-demo"&lt;/span&gt;
&lt;span class="py"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.1.0"&lt;/span&gt;
&lt;span class="py"&gt;edition&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"2021"&lt;/span&gt;
&lt;span class="py"&gt;build&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"build.rs"&lt;/span&gt;

&lt;span class="nn"&gt;[dependencies]&lt;/span&gt;

&lt;span class="nn"&gt;[build-dependencies]&lt;/span&gt;
&lt;span class="py"&gt;capnpc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.14"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;crate::build.rs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main() {
    capnpc::CompilerCommand::new()
        .src_prefix("src/schema")
        .file("src/schema/point.capnp")
        .run()
        .expect("schema compiler command failed");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it compiles and runs the build &lt;code&gt;cargo build&lt;/code&gt;! But it doesn't do anything. 😞 Or maybe it did and there's a schema somewhere on my drive?&lt;/p&gt;

&lt;p&gt;It's probably this missing Env-var from the examples:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fobfq1xs6cjkwuzb3efjx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fobfq1xs6cjkwuzb3efjx.png" alt="Image description" width="244" height="90"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;...but I think I want to specify the output folder myself:&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="nn"&gt;capnpc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;CompilerCommand&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;.src_prefix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"src/schema"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"src/schema/point.capnp"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.output_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"src/schema"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"schema compiler command failed"&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;Ok! Now we have a generated schema file that is around 500 lines of code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpz9i5l1ee6vzhiei55l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpz9i5l1ee6vzhiei55l.png" alt="Image description" width="800" height="348"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;I'm going to cargo build again to see what happens when the schema already exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ ll src/schema 
total 56
&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;  1 kushaljoshi  staff   159B 30 Apr 15:58 point.capnp
&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;  1 kushaljoshi  staff    20K 30 Apr 17:54 point_capnp.rs

❯ cargo build
    Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;0.04s

❯ ll src/schema
total 56
&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;  1 kushaljoshi  staff   159B 30 Apr 15:58 point.capnp
&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;  1 kushaljoshi  staff    20K 30 Apr 17:54 point_capnp.rs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing (I ran the second &lt;code&gt;cargo build&lt;/code&gt; at 18:00)! this looks good so far. I don't want to be pointlessly regenerating the schema on every build.&lt;/p&gt;

&lt;p&gt;Right, now we have a schema and automatically generated code in our build. That's quite nice. Now how do we use it?&lt;/p&gt;




&lt;h2&gt;
  
  
  Using the generated code
&lt;/h2&gt;

&lt;p&gt;In the generated code there's &lt;code&gt;pub mod point&lt;/code&gt; module wrapper so this seems like a good places to start. Let's &lt;code&gt;use&lt;/code&gt; that module in our project:&lt;/p&gt;

&lt;p&gt;We'll keep it nice and simple. First we can make a server module that will be the capnp server.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cargo.toml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="err"&gt;...&lt;/span&gt;

&lt;span class="nn"&gt;[dependencies]&lt;/span&gt;
&lt;span class="py"&gt;capnp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.14"&lt;/span&gt;

&lt;span class="err"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;main.rs:&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;mod&lt;/span&gt; &lt;span class="n"&gt;server&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;main&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;"Hello, world!"&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;I've left the default new project code for now as a sign-post for very new people to see what is happening and how we are building up the project.&lt;/p&gt;

&lt;p&gt;server.rs:&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;#[path&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"./schema/point_capnp.rs"&lt;/span&gt;&lt;span class="nd"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;point_capnp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;point_capnp&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;point_tracker&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm guessing we need to tell the compiler where the code is. &lt;/p&gt;

&lt;p&gt;There's a small issue when we try to build this. The generated code expects the point_capnp mod to be at the top level and doesn't like it being declared inside &lt;code&gt;server::&lt;/code&gt; :&lt;/p&gt;

&lt;p&gt;That's a little annoying. The generated code is hard coded to &lt;code&gt;crate::point_np&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I had a read of the issues for a few hours and found this has been addressed, albeit in what feels like a hacky way, and was raised/found as an issue in an &lt;a href="https://hoverbear.org/blog/capn-proto-in-rust/" rel="noopener noreferrer"&gt;old blog article from Hoverbear, which helped immensely here&lt;/a&gt; (thanks Ana!).&lt;/p&gt;

&lt;p&gt;The simple answer for us right now (if there is a better/simpler solution, please comment) is to add &lt;a href="https://github.com/capnproto/capnproto-rust/blob/0b274109ce3c2855b6590cd80422465d69e48db1/capnpc/rust.capnp" rel="noopener noreferrer"&gt;this file - rust.capnp&lt;/a&gt; to the schema folder and include it in each schema like this:&lt;/p&gt;

&lt;p&gt;point.capnp:&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="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0xb068ff5fb1c4f77e&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;Rust&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"rust.capnp"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;Rust&lt;/span&gt;&lt;span class="nf"&gt;.parentModule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Float32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Float32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;PointTracker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;addPoint&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;totalPoints&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;UInt64&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;This is an irritant as it's a manual change to every schema file but it works great and compiled fine with tons of "associated function is not used" warnings for the generated code. Adding &lt;code&gt;#![allow(dead_code)]&lt;/code&gt; at the top of the server.rs file fixed this for now. This is a pattern that works for now but probably won't scale - I'll let my server module "own" the capnp generate code for each schema that server is a host for.&lt;/p&gt;

&lt;p&gt;I'm making a first commit to the repo at this point as I have a compiling capnp schema 🎉.&lt;/p&gt;




&lt;h2&gt;
  
  
  Getting to the Point
&lt;/h2&gt;

&lt;p&gt;At this stage we are almost at the end of most of the available documentation regarding Rust but the capnproto-rust repo contains both serialisation and RPC examples. Deconstructing those, I'm hoping to make the simplest implementation I can here.&lt;/p&gt;

&lt;p&gt;Let's make a point from our Point. The docs say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In Rust, the generated code for the example above includes a point::Reader&amp;lt;'a&amp;gt; struct with get_x() and get_y() methods, and a point::Builder&amp;lt;'a&amp;gt; struct with set_x() and set_y() methods.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To understand how to use these, we have to jump back to the beginning of the documentation to understand how capnp works:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cap’n Proto generates classes with accessor methods that you use to traverse the message.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok so we need to make a &lt;em&gt;message&lt;/em&gt; that will &lt;em&gt;contain&lt;/em&gt; our Point. I think.&lt;/p&gt;

&lt;p&gt;In the address book example &lt;code&gt;capnp::serialized_packed&lt;/code&gt; is used to read and write this message to a stream. Docs for this are here.&lt;/p&gt;

&lt;p&gt;We can copy this address book code structure to make our Point.&lt;/p&gt;

&lt;p&gt;server.rs:&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;#![allow(dead_code)]&lt;/span&gt;

&lt;span class="nd"&gt;#[path&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"./schema/point_capnp.rs"&lt;/span&gt;&lt;span class="nd"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;point_capnp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;point_demo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;server&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;point_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;serialize_packed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;write_to_stream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;message&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new_default&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;demo_point&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="py"&gt;.init_root&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nn"&gt;point&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.set_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5_f32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.set_y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;10_f32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nn"&gt;serialize_packed&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;write_message&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;mut&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdout&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;message&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;main.rs:&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;mod&lt;/span&gt; &lt;span class="n"&gt;server&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;main&lt;/span&gt;&lt;span class="p"&gt;()&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;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;server&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;point_demo&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;write_to_stream&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ cargo run
   Compiling capnproto-demo v0.1.0 &lt;span class="o"&gt;(&lt;/span&gt;/Users/kushaljoshi/code/rust/capnproto/capnproto-demo&lt;span class="o"&gt;)&lt;/span&gt;
    Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;1.13s
     Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/capnproto-demo&lt;span class="sb"&gt;`&lt;/span&gt;
 ̠@ A%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fantastic! We "serialized" our Point and packed it into a capnp message. The message is not readable (it's the underscore, at-symbol, space, capital-A, percent-symbol) because it is capnp's binary type that does not need further serialization/deserialization over a stream to be used with an application. Can we check it?&lt;/p&gt;

&lt;p&gt;Yes! The &lt;code&gt;capnp&lt;/code&gt; tool provides a decode feature that needs the schema and the data structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ cargo run | capnp decode ./src/schema/point.capnp Point
    Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;0.04s
     Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/capnproto-demo&lt;span class="sb"&gt;`&lt;/span&gt;
capnp decode: The input is not &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"binary"&lt;/span&gt; format. It looks like it is &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"packed"&lt;/span&gt; format. Try that instead.
Try &lt;span class="s1"&gt;'capnp decode --help'&lt;/span&gt; &lt;span class="k"&gt;for &lt;/span&gt;more information.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok so this didn't work because we need to either tell capnp that it's a packed (compressed) message, or we need to print the raw message to STDOUT. Let's do both to increase our intuition of what is happening here. First we just need to add --packed to the CLI command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ cargo run | capnp decode ./src/schema/point.capnp Point &lt;span class="nt"&gt;--packed&lt;/span&gt;
    Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;0.04s
     Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/capnproto-demo&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 5, y &lt;span class="o"&gt;=&lt;/span&gt; 10&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can see that capnp can unpack (decompress) the message and print out the Point coords that we set. But we may not always have packed data so let's send the Point in its raw message format and make sure we can decode it as we would expect. We need to make a change to server for that:&lt;/p&gt;

&lt;p&gt;server.rs&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="o"&gt;...&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;point_demo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;server&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;point_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;write_to_stream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;message&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new_default&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;demo_point&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="py"&gt;.init_root&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nn"&gt;point&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.set_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5_f32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.set_y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;10_f32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nn"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;write_message&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;mut&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdout&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;message&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ cargo run | capnp decode ./src/schema/point.capnp Point &lt;span class="nt"&gt;--packed&lt;/span&gt;
   Compiling capnproto-demo v0.1.0 &lt;span class="o"&gt;(&lt;/span&gt;/Users/kushaljoshi/code/rust/capnproto/capnproto-demo&lt;span class="o"&gt;)&lt;/span&gt;
    Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;0.65s
     Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/capnproto-demo&lt;span class="sb"&gt;`&lt;/span&gt;
capnp decode: The input is not &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"packed"&lt;/span&gt; format. It looks like it is &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"binary"&lt;/span&gt; format. Try that instead.
Try &lt;span class="s1"&gt;'capnp decode --help'&lt;/span&gt; &lt;span class="k"&gt;for &lt;/span&gt;more information.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A very helpful message that confirms what we know we did. We can remove the &lt;code&gt;--packed&lt;/code&gt; flag now.&lt;/p&gt;

&lt;p&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;❯ cargo run | capnp decode ./src/schema/point.capnp Point
    Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;0.05s
     Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/capnproto-demo&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 5, y &lt;span class="o"&gt;=&lt;/span&gt; 10&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fabulous. &lt;/p&gt;

&lt;p&gt;If you have followed along and got this working, you may want to see the benefit more clearly so for that we can save the data and load it back in without any further serialization/deserialization.&lt;/p&gt;

&lt;p&gt;server.rs:&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;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;point_demo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;server&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;point_capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;write_to_stream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;message&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new_default&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;demo_point&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="py"&gt;.init_root&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nn"&gt;point&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.set_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5_f32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.set_y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;10_f32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// This Result should be consumed properly in an actual app&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;write_message&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;mut&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdout&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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Save the point&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;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"point.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&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;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;write_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Read the point from file&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;point_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"point.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="c1"&gt;// We want this to panic in our demo incase there is an issue&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;point_reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                &lt;span class="nn"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;read_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;point_file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;capnp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;message&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;ReaderOptions&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;.unwrap&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;demo_point&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;point&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;point_reader&lt;/span&gt;&lt;span class="nf"&gt;.get_root&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&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;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;(x = {}, y = {})"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.get_x&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;demo_point&lt;/span&gt;&lt;span class="nf"&gt;.get_y&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nf"&gt;Ok&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ cargo run
   Compiling capnproto-demo v0.1.0 &lt;span class="o"&gt;(&lt;/span&gt;/Users/kushaljoshi/code/rust/capnproto/capnproto-demo&lt;span class="o"&gt;)&lt;/span&gt;
    Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;0.63s
     Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/capnproto-demo&lt;span class="sb"&gt;`&lt;/span&gt;
@ A
&lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 5, y &lt;span class="o"&gt;=&lt;/span&gt; 10&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So... it doesn't look like much happened there and the output, by design, looks the same.&lt;/p&gt;

&lt;p&gt;However, you may have missed what just happened and how awesome this 😄 !! &lt;/p&gt;

&lt;p&gt;Let's go through it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We created a serialized Point from our Point Schema&lt;/li&gt;
&lt;li&gt;We set data &lt;em&gt;inside&lt;/em&gt; the serialized Point (no need to deserialize Point or serialize x &amp;amp; y float 32 values)&lt;/li&gt;
&lt;li&gt;We saved the serialized data to disk using the standard file tools&lt;/li&gt;
&lt;li&gt;We read in the serialized data using the standard file tooling (endianness is considered in the filetype)&lt;/li&gt;
&lt;li&gt;We used &lt;em&gt;accessor methods&lt;/em&gt; on the serialized data and printed the value &lt;em&gt;without&lt;/em&gt; deserializing the data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's ok - if you are thinking "so what?", then your project use cases may not have been performance critical so far. If they have though, then this should be a wondrous thing to behold!&lt;/p&gt;

&lt;p&gt;For the uber skeptical: The &lt;code&gt;Reader&lt;/code&gt; above is not a deserializer. It is literally a Reader. It needs a schema and some data and it know how to set the pointers in the data (which is made up of ordered segments) to make the accessor methods point at the correct parts of the data. For more information have a read of the &lt;a href="https://capnproto.org/encoding.html" rel="noopener noreferrer"&gt;capnp encoding page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can decode the file data the same way as the STDOUT output above:&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;cat &lt;/span&gt;point.txt | capnp decode ./src/schema/point.capnp Point
&lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 5, y &lt;span class="o"&gt;=&lt;/span&gt; 10&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this really quite interesting; if it can be saved, it can be thrown over a network and used by any client that has the appropriate map (schema) to read the received data, without any interim deserialization steps. &lt;/p&gt;

&lt;p&gt;That's what we will try next in Part 2.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>data</category>
      <category>network</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>18 factors powering the Rust revolution, Part 3 of 3</title>
      <dc:creator>Kushal Joshi</dc:creator>
      <pubDate>Mon, 11 Apr 2022 21:59:06 +0000</pubDate>
      <link>https://forem.com/kushalj/18-factors-powering-the-rust-revolution-part-3-of-3-2p6b</link>
      <guid>https://forem.com/kushalj/18-factors-powering-the-rust-revolution-part-3-of-3-2p6b</guid>
      <description>&lt;p&gt;Cover Photo by &lt;a href="https://unsplash.com/@markusspiske?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Markus Spiske&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/rust?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: I work at &lt;a href="https://keyrock.eu/"&gt;Keyrock&lt;/a&gt; but views expressed here are my own. However, if you like the views, do go checkout the company as it is full of awesome people and I have learned much whilst working there, including Rust.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you haven't read Parts 1 &amp;amp; 2, I highly recommend you start with those:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/kushalj/18-factors-powering-the-rust-revolution-part-1-of-3-3imc"&gt;Part 1&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Syntax&lt;/li&gt;
&lt;li&gt;Separation of Data and Code&lt;/li&gt;
&lt;li&gt;Type safety&lt;/li&gt;
&lt;li&gt;Declarative Programming and Functional Composition features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/kushalj/18-factors-powering-the-rust-revolution-part-2-of-3-h73"&gt;Part 2&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Evolved package management&lt;/li&gt;
&lt;li&gt;Concurrency ready std lib and great async support&lt;/li&gt;
&lt;li&gt;Clear, helpful compiler errors&lt;/li&gt;
&lt;li&gt;Zero cost abstractions (Optimisation)&lt;/li&gt;
&lt;li&gt;Energy efficiency&lt;/li&gt;
&lt;li&gt;WASM&lt;/li&gt;
&lt;li&gt;Tauri - High performance, secure, cross-platform desktop computing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Part 3&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Macros!&lt;/li&gt;
&lt;li&gt;Corporate Engagement&lt;/li&gt;
&lt;li&gt;Cryptocurrency and Blockchains&lt;/li&gt;
&lt;li&gt;Everybody else's tooling&lt;/li&gt;
&lt;li&gt;Speed&lt;/li&gt;
&lt;li&gt;No garbage collection&lt;/li&gt;
&lt;li&gt;Memory safety features&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Macros!
&lt;/h2&gt;

&lt;p&gt;Some people may be scarred and jaded from out of control macro usage in certain languages (especially low-level languages). In the world of Ruby people would decry "Rails" developers and later in the Elixir universe, people were encouraged to start &lt;em&gt;without&lt;/em&gt; Phoenix, that giant macro-driven-code-generating beauty, for fear that they would never really explore the language properly and become "Phoenix" developers; I understand why - but in these examples - like with Elixir metaprogramming (using macros to generate and change code) - Macros were generally used for good, and supercharged the language the right way. Usually.&lt;/p&gt;

&lt;p&gt;Macros in Rust are generally very sensible, and easy to identify - either with their exclamation mark identifiers for declarative style macros (like &lt;code&gt;println!()&lt;/code&gt; &lt;a href="https://doc.rust-lang.org/std/macro.println.html"&gt;in the std lib&lt;/a&gt;), or used with &lt;code&gt;derive&lt;/code&gt; or as custom function attributes, which usually generate code from your code.&lt;/p&gt;

&lt;p&gt;Generally the macro system is a platform for extended functionality and simplifying the interface to libraries. This hints at the potential growth still to come.&lt;/p&gt;

&lt;p&gt;So how is this a major factor? I believe Macros are very similar to a custom type system. If the type is heavily documented and openly shared, it becomes part of the language and general knowledge of the system, known by all that claim to be its adherents. Where macros are not maintained, not tested well, not documented well, and are part of a closed system, they can become a knowledge-barrier to those attempting to change/fix/test that system's code. - hence the resistance many people have.&lt;/p&gt;

&lt;p&gt;The Rust community has, through common practice, set some pretty heavy expectations for people producing macros that become part of the general knowledge of the language: As an example, the &lt;a href="https://serde.rs/"&gt;Serde&lt;/a&gt; library, known to almost anyone that will use Rust for web service development, is a heavily documented set of macros that allow for very granular transformation when serialising and derserialising data. There is even a Serde "book". Approached in this disciplined way, Rust Macros are going to provide an explosion of metaprogramming in the years to come, that will amplify the existing Rust growth.&lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://serde.rs/"&gt;https://serde.rs/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://happens.lol/posts/a-love-letter-to-rust-macros/"&gt;https://happens.lol/posts/a-love-letter-to-rust-macros/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jstrong.dev/posts/2020/productive-rust-implementing-traits-with-macros/"&gt;https://jstrong.dev/posts/2020/productive-rust-implementing-traits-with-macros/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Corporate Engagement
&lt;/h2&gt;

&lt;p&gt;We're way past the initial gradual incline slope of Rust growth. At this point, we're approaching hypergrowth. Try and find a company interested in performance systems programming that's &lt;em&gt;not&lt;/em&gt; looking at Rust or starting to teach it themselves. With Microsoft and AWS sponsoring major rust packages, it feels very much like all the ducks are already neatly lined up and ready for take-off.&lt;/p&gt;

&lt;p&gt;Let's take a tiny peek at how this is happening and what this corporate engagement looks like:&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“We’re not just hiring a couple of folks for Tokio and the Rust compiler,” Shane Miller says. “Rust is a critical component of our long-term strategy, and we’re investing to deliver Rust engineering at Amazon scale. That includes developer tools, infrastructure components, interoperability, and verification.”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— &lt;a href="https://aws.amazon.com/blogs/opensource/why-aws-loves-rust-and-how-wed-like-to-help/"&gt;AWS Open Source Blog, November 2020&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;News headline:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"AWS's Shane Miller to head the newly created Rust Foundation"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— &lt;a href="https://www.zdnet.com/article/awss-shane-miller-to-head-the-newly-created-rust-foundation/"&gt;ZDNet, April 2021&lt;/a&gt;&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The Rust Foundation Community Grants Program is made possible by the generous support of corporate partners, with Amazon Web Services (AWS), Google, and Huawei amongst the first contributors...&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“A large part of the current success of the Rust Project is due to the vibrant community and volunteers who contribute not only to engineering tasks, but to all aspects of making the Project successful,” said Lars Bergstrom, Google Director of Engineering. “We believe that one way that the Rust Foundation can best support the Rust Project is through providing Community Grants to those critical volunteers.”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— &lt;a href="https://www.businesswire.com/news/home/20211209005763/en/Rust-Foundation-to-Launch-Community-Grants-Program"&gt;Businesswire, December 2021&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;News headline:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Google's Android Team is backing an effort to introduce Rust as a second programming language in the Linux kernel.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— &lt;a href="https://www.zdnet.com/article/google-backs-effort-to-bring-rust-to-the-linux-kernel/"&gt;ZDNet, April 2021&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;News headline:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In Rust we trust: Shoring up Apache, ISRG ditches C, turns to wunderkind lang for new TLS crypto module &lt;br&gt;
Subheading: Elder server httpd to be revitalized with Google-funded memory-safe add-on&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— &lt;a href="https://www.theregister.com/2021/02/02/patching_apache_rust/"&gt;The Register, Feb 2021&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;News headline:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Fuchsia OS uses Rust to write code extensively&lt;br&gt;
"According to the overall code analysis of Fuchsia, Rust has the most lines of code."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— &lt;a href="https://meterpreter.org/fuchsia-os-uses-rust-to-write-code-extensively/"&gt;Infotech News, meterpreter, May 2021&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;News headline:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Announcing three new Rust Foundation Board Members"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Article detail:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The Rust Foundation is delighted to announce that three new individuals have joined the Board of Directors.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;The members:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;– Ryan Levick, Developer Advocate, Microsoft&lt;br&gt;
– Andrew Wafaa, Senior Director of Software Communities, Arm&lt;br&gt;
– Eric Garcia, Software Engineering Manager, Meta&lt;/p&gt;

&lt;p&gt;— &lt;a href="https://foundation.rust-lang.org/news/2022-03-16-news-announcing-three-new-board-members-copy/"&gt;Rust Foundation news blog, March 2022&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;This is really a tiny proportion of the Rust news out there. Additionally all of the tech giants are launching their own Rust onboarding and training material. As an idea, introducing Rust at a giant enterprise might sound experimental but when &lt;em&gt;everyone's&lt;/em&gt; doing it, well, then it's at worst, a fashionable trend; at best, corporates jumping onboard to position themselves around a technology that constitutes part of the core competitive advantage of tomorrows solutions.&lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://docs.microsoft.com/en-us/learn/paths/rust-first-steps/"&gt;https://docs.microsoft.com/en-us/learn/paths/rust-first-steps/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.ibm.com/articles/os-using-rust/"&gt;https://developer.ibm.com/articles/os-using-rust/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Cryptocurrency and Blockchains
&lt;/h2&gt;

&lt;p&gt;Assuming you were not trapped in a CIA black-site for the last 5 years, you've probably seen another unstoppable tidal wave, besides Rust; call it what you want: Web 3.0, The blockchain revolution, The rise of DEFI, NewFI, GameFI, The New Fiat... Cryptocurrency has arrived and it's here to stay (note: I might be slightly biased - see my history in &lt;a href="https://dev.to/kushalj/18-factors-powering-the-rust-revolution-part-1-of-3-3imc"&gt;Part 1&lt;/a&gt;). And what else has come along with this new industry? A demand for performance: Performance transaction/block processing, performance historical data processing, and performance contract execution - all on various distributed platforms, each claiming superiority over the others, all burning millions in energy (see 'Energy efficiency' in &lt;a href="https://dev.to/kushalj/18-factors-powering-the-rust-revolution-part-2-of-3-h73"&gt;Part 2&lt;/a&gt;), and each not wanting live code to fail (see 'Safety features', below) - all so some other excited people can buy &lt;a href="https://www.nftsstreet.com/top-10-most-expensive-bored-ape-yacht-club-nfts/"&gt;$2m (two million dollar) cartoon apes&lt;/a&gt;, or a piece of virtual land etc etc. If you were starting one of these projects today, what technology might you decide to bet on?&lt;/p&gt;

&lt;p&gt;Here's hint - checkout &lt;a href="https://docs.substrate.io/"&gt;Substrate&lt;/a&gt;, a blockchain framework, written in Rust.&lt;/p&gt;

&lt;p&gt;Some Blockchain projects using Rust:&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Article title:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Solana Uses Rust to Pull in Developers and Avoid Copypasta"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://thenewstack.io/solana-rust-developers/"&gt;The New Stack, Jan 2022&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;News Headline:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Concordium Launches DevX Initiative to Support Rust Devs"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://coinquora.com/concordium-launches-devx-initiative-to-support-rust-devs/"&gt;CoinQuora, April 2021&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;And when the many-named, many-faced hydra of &lt;em&gt;social data&lt;/em&gt; wanted to make a cryptocurrency (&lt;a href="https://en.wikipedia.org/wiki/Diem_(digital_currency)"&gt;Libra/Diem&lt;/a&gt;), they also went full Rust, back in 2019.&lt;/p&gt;

&lt;h2&gt;
  
  
  Everybody else's tooling
&lt;/h2&gt;

&lt;p&gt;There is a naive argument that can be made at some point: &lt;em&gt;"So why doesn't everyone just use Rust?"&lt;/em&gt; - well, there are lots of reasons: Legacy code (Cobol, C++ that no-one wants to touch but they want to extend - see all Banks), availability of developers, capability of engineering managers (😇), context and requirements (do we need quick code, or need to code quick?), suitability of application to the context (Some front-end frameworks are very mature - why fix what is not borked?), availability of specialist libraries (e.g. Python/Julia), architecture priority (e.g. we want 100% functional, with Actors), addiction to pain (Haskell, Java, C++), etc, etc. &lt;/p&gt;

&lt;p&gt;However, The &lt;em&gt;tooling&lt;/em&gt; around other languages does not need to be limited by the limits of those languages. An outcome of energy efficiency requirements, speed, and concurrency features, for example, might lead to someone writing the &lt;a href="https://deno.land/"&gt;compiler/interpreter for another language in Rust&lt;/a&gt; 🙂 .  More interestingly, once it is possible to transpile a language into Rust, it can be recompiled into WASM and run in the browser!&lt;/p&gt;

&lt;p&gt;This means the Rust will likely form the core of other languages' tooling as we enter the next decade.&lt;/p&gt;

&lt;p&gt;Examples:&lt;br&gt;
A Node.js interpreter written in Rust: &lt;a href="https://deno.land/"&gt;https://deno.land/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A web tool pipeline written in Rust: &lt;a href="https://swc.rs/"&gt;https://swc.rs/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The fastest dataframe library with Python links, written in Rust: &lt;a href="https://pola-rs.github.io/polars-book/user-guide/index.html"&gt;https://pola-rs.github.io/polars-book/user-guide/index.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A Clojure interpreter written in Rust: &lt;a href="https://github.com/clojure-rs/ClojureRS"&gt;https://github.com/clojure-rs/ClojureRS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A Python interpreter written in Rust: &lt;a href="https://rustpython.github.io/"&gt;https://rustpython.github.io/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Erlang BEAM (VM) written in Rust: &lt;a href="https://getlumen.org/"&gt;https://getlumen.org/&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Ok, now we get to the "big three". Often when I am out and about and professionally participating in the local requirement to support the testing of grain, potato, and fruit fermentation operations, I'm often asked "Why Rust?". As the previous 15 factors in these articles would involve a rather extended evening of technical discussions, which can be challenging considering my previously mentioned professional activities, and not to mention immensely boring for non-developers, I usually default to the following three factors.&lt;/p&gt;




&lt;h2&gt;
  
  
  Speed
&lt;/h2&gt;

&lt;p&gt;It may be obvious that low level compiled languages are faster; but it may not be obvious that they are &lt;em&gt;faster&lt;/em&gt; - I mean hundreds and sometimes thousands of times faster than interpreted languages (I didn't realise that this was not common knowledge until speaking to a JS dev recently who said "Thousands? Are you sure?"). Rust speed is generally on par with C++ and in some cases faster - usually when C++ is not "cheating" with what Rust would call &lt;em&gt;unsafe&lt;/em&gt; code. There are rules in the world of Rust this is going to add a few nanoseconds of overhead. For most application contexts, the difference will be a moot point.&lt;/p&gt;

&lt;p&gt;More interesting is the meeting point of high-level abstractions (see &lt;a href="https://dev.to/kushalj/18-factors-powering-the-rust-revolution-part-1-of-3-3imc"&gt;Part 1&lt;/a&gt;) and speed. This allows for very high speed code that is easy to maintain, fix, test, and learn (knowledge transition amongst a team).&lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.techempower.com/benchmarks/"&gt;https://www.techempower.com/benchmarks/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.logdna.com/blog/coding-for-performance-why-we-chose-rust"&gt;https://www.logdna.com/blog/coding-for-performance-why-we-chose-rust&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://programming-language-benchmarks.vercel.app/c-vs-rust"&gt;https://programming-language-benchmarks.vercel.app/c-vs-rust&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://programming-language-benchmarks.vercel.app/cpp-vs-rust"&gt;https://programming-language-benchmarks.vercel.app/cpp-vs-rust&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/deepu105/concurrency-in-modern-programming-languages-rust-vs-go-vs-java-vs-nodejs-vs-deno-36gg"&gt;https://dev.to/deepu105/concurrency-in-modern-programming-languages-rust-vs-go-vs-java-vs-nodejs-vs-deno-36gg&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  No garbage collection
&lt;/h2&gt;

&lt;p&gt;Interpreted or compiled, high level languages are usually very memory safe, due to the common language feature of a garbage collector. Memory is tracked and when variables are no longer in scope, the memory is tidied up and we never see a null pointer or experience segmentation fault/memory access violation. In fact most people with a high level language background that I speak with about this subject say the same thing every time: &lt;em&gt;I've never really had to think about memory, memory safety or how I use memory.&lt;/em&gt; You can thank your friendly neighbourhood garbage collector.&lt;/p&gt;

&lt;p&gt;So what's the issue? Well, the JavaScript garbage collector (GC) can take around 150ms in some cases to finish its "sweep" of memory. That is a long time in a realtime system. Go-lang claims to have the fastest garbage collection and they have managed to eliminate large memory scanning pauses; they claim to have been able to get a GC pause down to &lt;a href="https://groups.google.com/g/golang-dev/c/Ab1sFeoZg_8/m/_DaL0E8fAwAJ?pli=1"&gt;100 microseconds&lt;/a&gt;. This is impressive and likely makes GC pauses insignificant for most contexts.&lt;/p&gt;

&lt;p&gt;However, what if we are creating an autopilot for an airplane, or a self-driving car? Do we &lt;em&gt;ever&lt;/em&gt; want GC pauses? Unless we like to make extremely dangerous (and borderline psychotic) gambles, probably not  [There are exceptions, in the case of a well designed system where GC is distributed and controlled, like &lt;a href="https://www.ponylang.io/reference/pony-performance-cheatsheet/"&gt;Pony&lt;/a&gt; - but they start with the words "If you know what you are doing..."]. Similarly most High Frequency Trading systems in the traditional financial trading world are written in C++ for the same reason. Even a 100 microsecond pause could lead to a significant loss from that period of "blindness".&lt;/p&gt;

&lt;p&gt;This sounds great and you may be thinking "Sure, but I'm making a web-server so I don't think I need to worry about this GC stuff." You are probably right - except maybe from one other perspective; I'm going to hit someone's buzzword bingo here - machine empathy. The lack of a garbage collector makes the code more efficient, as seen by Rust's energy profile, because memory is not being intermittently scanned/deallocated/reallocated etc. This can be understood intuitively: a GC pause is a performance hit that takes place because (usually) all threads must be suspended (stopping of the world) to ensure no memory access takes place during the sweep [Except in the Erlang/Elixir and Pony universes of course]. Multi-core systems can even experience a detrimental impact on throughput with an &lt;a href="https://www.oracle.com/java/technologies/javase/gc-tuning-6.html"&gt;increase in number of threads&lt;/a&gt; (as time spent collecting garbage increases).&lt;/p&gt;

&lt;p&gt;Some popular options to avoid GC are then C/C++/Rust/Objective-C/Swift/some other ancient languages like Cobol/Pascal/Fortran. Objective-C &amp;amp; Swift are generally thought of as an Apple device language with associated/relevant tools (although &lt;a href="https://developer.ibm.com/languages/swift/"&gt;Server-side Swift&lt;/a&gt; is apparently a thing now) but not as cross platform general use languages (yet) - &lt;em&gt;and&lt;/em&gt; Swift reference counting is thought of as a type of GC (the compiler inserts release/retain code at compile time) so we can remove those. So then, eliminating the ancient languages leaves C/C++... and Rust. &lt;/p&gt;

&lt;p&gt;Taking these other factors into consideration, which would you choose?&lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://groups.google.com/g/golang-dev/c/Ab1sFeoZg_8/m/_DaL0E8fAwAJ?pli=1"&gt;https://groups.google.com/g/golang-dev/c/Ab1sFeoZg_8/m/_DaL0E8fAwAJ?pli=1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.oracle.com/java/technologies/javase/gc-tuning-6.html"&gt;https://www.oracle.com/java/technologies/javase/gc-tuning-6.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://wiki.c2.com/?LanguagesWithoutGarbageCollection"&gt;https://wiki.c2.com/?LanguagesWithoutGarbageCollection&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html"&gt;https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Safety features
&lt;/h2&gt;

&lt;p&gt;Rust provides an incredible amount of code safety for a low-level language (more than just the glossed-over object-lifetime tracking that is often mentioned as the main feature). The first line of defence comes from the strong static typing that is use to check all type usage at compile time. Similarly, the Trait system creates a structured way to check for characteristics of generic types in "dynamic" code, meaning a function can accept a generic as long as the generic type expresses (or implements in the Rust terminology) specific Traits. A simple example is that the &lt;code&gt;println!()&lt;/code&gt; function (which is actually a &lt;a href="https://doc.rust-lang.org/std/macro.println.html"&gt;std-lib macro&lt;/a&gt;) can accept any type but to print it to stdout, the type must implement either the Display trait or the Debug trait (which has different usage but with this same macro). This means the code will never crash because an undisplayable type was sent to this macro - because that code would not compile in the first place!&lt;/p&gt;

&lt;p&gt;Memory safety comes from a feature called &lt;em&gt;Ownership&lt;/em&gt;. The theory behind ownership is two-fold; no two pieces of code should be able to write to the same piece of memory at once time, and no code should be able to access a memory location that is no longer in scope in its parent/calling function - or rather is "alive" long enough for the usage (the concept of reference &lt;em&gt;Lifetimes&lt;/em&gt;). This is primarily expressed by "passing" ownership of variables from one part of the code to another, and by tracking the lifetime of a reference at compile time. References can be &lt;em&gt;borrowed&lt;/em&gt; as well, so they can be read but they cannot be written to unless borrowed as mutable, which is an alternative to ownership. As you can imagine, this brand new paradigm gets complicated really fast when it is first encountered and is often the source of frustration for those new to rust.&lt;/p&gt;

&lt;p&gt;I think there is something interesting in encountering a new paradigm like this. We assume we know how code works. We come across a new language and look for "hooks" that are familiar paradigms and philosophies, or architectures, that we can use as the foundation of our learning. Along comes a new, unique, and unfamiliar paradigm. Now, unless we take step back and accept &lt;em&gt;we do not know how this feature works and have almost no existing basis for understanding it&lt;/em&gt;, we will start to stumble around and make mistakes, or worse, avoid that area of the language entirely because it is difficult to grok.&lt;/p&gt;

&lt;p&gt;So is it worth learning all this stuff? Is Rust &lt;em&gt;actually&lt;/em&gt; safer? A couple of examples and a proof:&lt;/p&gt;

&lt;h3&gt;
  
  
  i.
&lt;/h3&gt;

&lt;p&gt;Nick Mathewson, co-founder of the Tor project started learning Rust by rebuilding Tor with Rust. He soon found that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Roughly half of Tor’s security issues since 2016 would have been impossible in Rust, and many of the other issues would have been much less likely, based on our informal audit."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There now exists a Rust based Tor project (&lt;a href="https://gitlab.torproject.org/tpo/core/arti"&gt;ARTI&lt;/a&gt;) that's &lt;a href="https://sg.finance.yahoo.com/news/tor-project-receives-670k-zcash-123137966.html?guccounter=1&amp;amp;guce_referrer=aHR0cHM6Ly95cm8uc2xhc2hkb3Qub3JnLw&amp;amp;guce_referrer_sig=AQAAAFDLHpzCuAVVN-9FJmTXxtOE34l88CIuSkVannDGS3ddx4yEvsusyVSQ16JE6hrE70c6D3__ojIAXOW3qMcDycLB06dRr49PMIev9o1wprtbWJPrJygLSDfrY9hPJd3pUrLq64rE9zARx-7FHnnNlY9XattSge8T9wnjtViYGyat"&gt;in development&lt;/a&gt; as we speak.&lt;/p&gt;

&lt;h3&gt;
  
  
  ii.
&lt;/h3&gt;

&lt;p&gt;Microsoft has produce some research that has become almost iconic in the rust community. They examined bugs and vulnerabilities in their software &lt;a href="https://msrc-blog.microsoft.com/2019/07/18/we-need-a-safer-systems-programming-language/"&gt;going back to 2006&lt;/a&gt; and &lt;a href="https://msrc-blog.microsoft.com/2019/07/22/why-rust-for-safe-systems-programming/"&gt;stated that&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"roughly 70% of the security issues that the MSRC assigns a CVE to are memory safety issues. This means that if that software had been written in Rust, 70% of these security issues would most likely have been eliminated."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Note: CVE = "Common Vulnerabilities and Exposures"&lt;/p&gt;

&lt;h3&gt;
  
  
  The proof:
&lt;/h3&gt;

&lt;p&gt;Beyond simple reasoning around the description of these features, how do we know they contribute towards "safety" - specifically memory safety in the language. Well, you could write lots of tests and set up many different scenarios to demonstrate this but you can further (if you really want to): In 2018, Ralf Jung, Jacques-Henri Jourdan, Robbert Krebbers, and Derek Dreyer went a step further and documented a &lt;a href="https://people.mpi-sws.org/~dreyer/papers/rustbelt/paper.pdf"&gt;formal safety proof&lt;/a&gt; for the language, which included verification conditions for libraries which, even if they contain unsafe code features, can be used to demonstrate their safety and inclusion as an extension to the language.&lt;/p&gt;

&lt;p&gt;This is important because without these verification conditions, libraries that contain C++ could be seen as inherently unsafe. If there there is an "external" set of conditions that the library needs to meet, it's inclusion into the ecosystem becomes a less contentious process - and could provide for much more crossover with other low level systems programming languages that have decades of history solving problems that may not have been solved by Rust as yet.&lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://doc.rust-lang.org/nomicon/ownership.html"&gt;https://doc.rust-lang.org/nomicon/ownership.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://devopedia.org/rust-language"&gt;https://devopedia.org/rust-language&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://msrc-blog.microsoft.com/2019/07/22/why-rust-for-safe-systems-programming/"&gt;https://msrc-blog.microsoft.com/2019/07/22/why-rust-for-safe-systems-programming/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://msrc-blog.microsoft.com/2019/07/18/we-need-a-safer-systems-programming-language/"&gt;https://msrc-blog.microsoft.com/2019/07/18/we-need-a-safer-systems-programming-language/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cacm.acm.org/magazines/2021/4/251364-safe-systems-programming-in-rust/fulltext"&gt;https://cacm.acm.org/magazines/2021/4/251364-safe-systems-programming-in-rust/fulltext&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://people.mpi-sws.org/%7Edreyer/papers/rustbelt/paper.pdf"&gt;https://people.mpi-sws.org/~dreyer/papers/rustbelt/paper.pdf&lt;/a&gt;&lt;br&gt;
&lt;a href="https://plv.mpi-sws.org/rustbelt/"&gt;https://plv.mpi-sws.org/rustbelt/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://etaps.org/user-profile/archive/47-etaps-2018/etaps-2018/373-speaker-6"&gt;https://etaps.org/user-profile/archive/47-etaps-2018/etaps-2018/373-speaker-6&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/0733/"&gt;https://pvs-studio.com/en/blog/posts/0733/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  In summary
&lt;/h2&gt;

&lt;p&gt;Is Rust my &lt;em&gt;favourite&lt;/em&gt; language? No. There are plenty of areas for improvement: Excessively implicit patterns (From/Into, implicit return values that are easy to miss, implicit traits like Display), some more complex internal architecture that requires "putting in the work" (e.g. Futures &amp;amp; "Pinning" memory when using async self-referencing futures), considerable boiler plate around custom Errors (some libraries help, but also have their own odd quirks) - I can continue - but none of them are dealbreakers. These things are being improved at an acceptable rate by a dedicated core team and a giant, motivated global community of devs (&lt;a href="https://www.rustacean.net/"&gt;rustaceans&lt;/a&gt; 🦀).&lt;/p&gt;

&lt;p&gt;The Rust revolution is not only about Rust being a very enjoyable language learn and code with - actually it's sometimes the opposite when getting started - It's about &lt;em&gt;inevitability&lt;/em&gt;. Rust is a solution that meets several (around 18) needs, bridges programming paradigms, and provides enough added value benefits and tooling that it's difficult not to consider it for everything from WASM, Data Science, performance distributed computing, and embedded systems.&lt;/p&gt;

&lt;p&gt;So where are going? I had a fellow JavaScript dev challenge my passion and conviction for Rust about three years ago, when I was fanboying the technology with a less explicit understanding of it. He said &lt;em&gt;"So you think everyone will be using Rust in the next 5 years?"&lt;/em&gt; I said &lt;em&gt;"More or less, yes, I think that's possible."&lt;/em&gt; He said &lt;em&gt;"Wanna make a bet?"&lt;/em&gt; Today I really wish I took that bet. The last language that had so much breadth, and that was also similarly championed by its community... was JavaScript. And that is where I think we're going with Rust: &lt;strong&gt;Ubiquity&lt;/strong&gt;; Rust will be soon growing &lt;em&gt;everywhere&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>performance</category>
      <category>systems</category>
    </item>
    <item>
      <title>18 factors powering the Rust revolution, Part 2 of 3</title>
      <dc:creator>Kushal Joshi</dc:creator>
      <pubDate>Sun, 10 Apr 2022 21:35:18 +0000</pubDate>
      <link>https://forem.com/kushalj/18-factors-powering-the-rust-revolution-part-2-of-3-h73</link>
      <guid>https://forem.com/kushalj/18-factors-powering-the-rust-revolution-part-2-of-3-h73</guid>
      <description>&lt;p&gt;Cover Photo by &lt;a href="https://unsplash.com/@who_is_trent?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Trent Pickering&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/rust-growth?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: I work at &lt;a href="http://Keyrock.eu"&gt;Keyrock&lt;/a&gt; but views expressed here are my own. However, if you like the views, do go checkout the company as it is full of awesome people and I have learned much whilst working there, including Rust.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you have haven't read &lt;a href="https://dev.to/kushalj/18-factors-powering-the-rust-revolution-part-1-of-3-3imc"&gt;Part 1&lt;/a&gt;, I strongly encourage you to do that first. It covers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Syntax&lt;/li&gt;
&lt;li&gt;Separation of Data and Code&lt;/li&gt;
&lt;li&gt;Type safety&lt;/li&gt;
&lt;li&gt;Declarative Programming and Functional Composition features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this part we are going to cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Evolved package management&lt;/li&gt;
&lt;li&gt;Concurrency ready std lib and great async support&lt;/li&gt;
&lt;li&gt;Clear, helpful compiler errors&lt;/li&gt;
&lt;li&gt;Zero cost abstractions (Optimisation)&lt;/li&gt;
&lt;li&gt;Energy efficiency&lt;/li&gt;
&lt;li&gt;WASM&lt;/li&gt;
&lt;li&gt;Tauri - High performance, secure, cross-platform desktop computing&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Evolved package management
&lt;/h2&gt;

&lt;p&gt;Here's something I don't see many articles noting about the "why Rust" conversation: Package management. I remember when I was first learning C++ over 20 years ago and you had to create folders, add header files, reference the folders in compiler params/settings, go through manual linking and a bunch of initially confusing activities like non-standard build tools that are dependent on the library creators choices. Later it felt like cumbersome &lt;a href="http://projects.csail.mit.edu/gsb/old-archive/gsb-archive/gsb2000-02-11.html"&gt;yak shaving&lt;/a&gt; that was just waiting for some automation.&lt;/p&gt;

&lt;p&gt;Today C++ developers can use some nascent package management tools - however from the looks of it (I haven't used them - happy to hear more from today's C++ devs) it's still a painful area of the language. There's an interesting conversation &lt;a href="https://www.reddit.com/r/cpp/comments/ix9n1u/why_is_it_such_an_abysmal_pain_to_use_libraries/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What about other languages? Well I used Pip (Python) for many years and battled circular/changing references and the (still) extra step of managing virtual environments. &lt;del&gt;It was ok but left a lot to be desired&lt;/del&gt; [edit: I changed my mind - I recently completed the Udacity ML for Trading course and I spent more time fixing dependency issues than learning. Working with Pip is still a load of arse]. JavaScript was a small step forward but the solution there seemed to be "pile all the dependent code into a folder and compile everything". It evolved over time (tree-shaking!) but today the "10k files in node_modules" is a well known &lt;del&gt;joke&lt;/del&gt; pain point.&lt;/p&gt;

&lt;p&gt;For me, Go-lang was the first language with well thought out package management and compilation tooling, with some foresight around name-spacing the dependencies and linking to existing tools like github (easily). It was flexible, simple, and featured the ability to very easily compile to some target hardware from some other hardware (a feature spreading across other tool-chains now). I have a friend that swear by Go &lt;em&gt;because&lt;/em&gt; of the benefits of it's simple effective tooling (and now there are package management options as the language evolves). New languages like Elixir now set the the standard with package managers like Hex that have batteries included and support building and publishing of packages with ease.&lt;/p&gt;

&lt;p&gt;The Rust language developers have made some excellent choices around tooling and fortunately &lt;em&gt;learned&lt;/em&gt; from the history of package management pain in other languages. For downloading, compiling, pushing open-source, and building CI tool chains, the tooling is mostly a help rather than a hindrance.&lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://doc.rust-lang.org/book/ch01-03-hello-cargo.html"&gt;https://doc.rust-lang.org/book/ch01-03-hello-cargo.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/blackode/how-to-write-elixir-packages-and-publish-to-hex-pm-8723038ebe76"&gt;https://medium.com/blackode/how-to-write-elixir-packages-and-publish-to-hex-pm-8723038ebe76&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://news.ycombinator.com/item?id=19989188"&gt;https://news.ycombinator.com/item?id=19989188&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Concurrency ready std lib and great async support
&lt;/h2&gt;

&lt;p&gt;The rust-lang blog has a great article that lays out the rust approach to concurrency - called &lt;a href="https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html"&gt;Fearless Concurrency&lt;/a&gt;. It lays out architectural and language principles and it's a good place to start towards understand the features of Rust that support concurrent programming.&lt;/p&gt;

&lt;p&gt;To try and summarise the fabulous concurrency toolkit is quite a challenge but here goes:&lt;/p&gt;

&lt;p&gt;The std lib contains "concurrency primitives" made up of both tooling and types that are built from the ground up to be supportive of the principles of fearless concurrency and to be thread safe for those wanting to experiment and try alternative methods/patterns of concurrent (or even parallel) programming.&lt;/p&gt;

&lt;p&gt;Two major projects (non std lib but extremely commonly used) stand out in the area of async programming: &lt;a href="https://async.rs/"&gt;Async std&lt;/a&gt; and &lt;a href="https://tokio.rs/"&gt;Tokio&lt;/a&gt; - no doubt familiar to anyone that has turned an eye towards Rust for a second too long. Async architecture in general is likely very familiar to JavaScript programmers but in Rust there are some extra considerations (like &lt;em&gt;ownership&lt;/em&gt; of the data that is thrown into an async function). Tokio is fast becoming a heavily supported and road tested async framework, with a thread scheduling runtime "baked in" that has learned from the history of &lt;a href="https://tokio.rs/blog/2019-10-scheduler"&gt;Go, Erlang, and Java thread schedulers&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Tokio is a "take what you need" framework, whilst Async-std started as an "everything the box" solution. Today both have a lot of crossover with micro async runtimes like smol becoming the foundation one of framework and optionally usable in the other. The ability to rip out a small dependent sub-crate (dependent package) like &lt;a href="https://github.com/smol-rs/smol"&gt;smol&lt;/a&gt; and use it independently with ease never get's boring, by the way. It's great way to include a test runtime in an async library without forcing the inclusion of a giant async framework.&lt;/p&gt;

&lt;p&gt;Both have rich documentation and both should be explored as a learning point for those new to Rust.&lt;/p&gt;

&lt;p&gt;However, this isn't even 50% of what's out there: Need raw parallel power (and maybe don't need an async runtime)? Checkout &lt;a href="https://crates.io/crates/rayon"&gt;Rayon&lt;/a&gt;. Need simple Actors for concurrent processing? Checkout &lt;a href="https://crates.io/crates/actix"&gt;Actix&lt;/a&gt;. Need a larger Actor &lt;em&gt;system&lt;/em&gt; for fault tolerance/CQRS messaging? Checkout &lt;a href="https://riker.rs/"&gt;Riker&lt;/a&gt;. Damn, I sound like a youtube advert 🤦 - For real though, this is the tip of the concurrency iceberg. There is so much more - and it's growing.&lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html"&gt;https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tokio.rs/blog/2019-10-scheduler"&gt;https://tokio.rs/blog/2019-10-scheduler&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Clear, helpful compiler errors
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jSThig5Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53c8j0l8365a6ryyqjum.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jSThig5Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53c8j0l8365a6ryyqjum.png" alt="C unreadable error" width="879" height="695"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;from: &lt;a href="https://www.reddit.com/r/ProgrammerHumor/comments/8nwyma/c_is_a_beautiful_language/"&gt;https://www.reddit.com/r/ProgrammerHumor/comments/8nwyma/c_is_a_beautiful_language/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I remember wondering what "Syntax Error" meant when I was about 11, as it was the response to pretty much anything I typed into my Commodore 64. I didn't know what a "syntax" was but it was fun to see that this beige box responding to anything I typed, as if I was meeting a digital creature speaking an alien language and was at least having some kind of dialogue, with the hope it will one day understand me.&lt;/p&gt;

&lt;p&gt;Later, as I learned various programming languages I learned the art of reading errors and stack traces and how to decipher the junk that compilers spewed out when everything went wrong. It was a strange "normal" and a part of learning to code in many languages, especially low level languages.&lt;/p&gt;

&lt;p&gt;When I first came across Evan Czaplicki's &lt;a href="https://elm-lang.org/news/compiler-errors-for-humans"&gt;"Compiler Errors for Humans"&lt;/a&gt; article and approach (expressed his landmark FP front-end language, &lt;a href="https://elm-lang.org/"&gt;Elm&lt;/a&gt;) I was gob-smacked. I had never realised how, or why, I had grown to accept the ridiculous, complicated, and often unintelligible, junk that passed for compiler "messaging", as being the norm.&lt;/p&gt;

&lt;p&gt;It feels like the common sense of user-experience-centric design principles applied to compiler (or interpreter) error messaging became a tidal wave in new languages. I remember I was learning Elixir and the community forums were discussing the desire for Elm-like errors (today Elixir's error messages are wonderful too, by the way).&lt;/p&gt;

&lt;p&gt;Rust has very much adopted this approach from the ground up and error information is extremely helpful and constantly improving. The complexities of the language, that generally stem from the vast menu of features, need this type of support, and while the initial experience can be frustrating, like a nagging, fusty, computer science pedant, lecturing you on every save, eventually you will become friends with it and it will be... emotional (I promise).&lt;/p&gt;

&lt;p&gt;This is however, an inadequate summary of the errors, notes, critique, and alternative solutions the compiler suggests. One of the best reactions was from a developer to whom I gave an initial exercise towards learn rust. He had a background in Go-lang, PHP, JavaScript and more. I had a catchup early on and he said "It's only been 3 days and I'm love with the compiler." That's a more adequate summary.&lt;/p&gt;

&lt;p&gt;What's creating this reaction is that, in Rust, a lack of compiler errors carries the implicit guarantee that (as long as there is no badly managed unsafe code) the compiler says your code is type-safe and memory-safe (also meaning thread-safe when data is being passed around). This increases confidence in the dev and makes refactoring far less dangerous. I even have straight-out-of-uni interns work on critical production code and I know that the compiler has got our backs.&lt;/p&gt;

&lt;p&gt;More info:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ferrous-systems.com/blog/the-compiler-is-your-friend/"&gt;https://ferrous-systems.com/blog/the-compiler-is-your-friend/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.reddit.com/r/rust/comments/8lse7e/rust_compiler_errors_are_appreciated_apparently_d/"&gt;https://www.reddit.com/r/rust/comments/8lse7e/rust_compiler_errors_are_appreciated_apparently_d/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example advanced response:&lt;br&gt;
&lt;a href="https://twitter.com/b0rk/status/954366146505052160?s=20&amp;amp;t=Ot4RtYCeRDbotl-TpoaPog"&gt;https://twitter.com/b0rk/status/954366146505052160?s=20&amp;amp;t=Ot4RtYCeRDbotl-TpoaPog&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Zero cost abstractions
&lt;/h2&gt;

&lt;p&gt;I was very sad to hear of the death of Joe Armstrong in 2019. He was without doubt a pioneer in our wider industry of software engineering. Joe had some classic quotes he is known for and I love them all. However, I have a bone to pick with this one:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Make it work, then make it beautiful, then if you really, really have to, make it fast. 90 percent of the time, if you make it beautiful, it will already be fast. So really, just make it beautiful!"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— Joe Armstrong, &lt;a href="https://www.google.co.uk/books/edition/Erlang_and_OTP_in_Action/1zgzEAAAQBAJ?hl=en&amp;amp;gbpv=1&amp;amp;dq=joe+armstrong+%22make+it+beautiful%22+erlang+and+OTP+in+action&amp;amp;pg=PT533&amp;amp;printsec=frontcover"&gt;quoted by Martin Logan in Erlang and OTP in Action, 2010, Manning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm going to say this quote works in Erlang or Elixir and maybe some other contexts but it's not a generalisation we can apply to all languages, especially imperative languages (see 'Declarative Programming and Functional Composition features' in &lt;a href="https://dev.to/kushalj/18-factors-powering-the-rust-revolution-part-1-of-3-3imc"&gt;Part 1&lt;/a&gt;). Try making "beautiful" optimised code in C++ or Go-lang that still maintains the implicit &lt;em&gt;knowledge&lt;/em&gt; in the code required for long term maintenance by a &lt;em&gt;team&lt;/em&gt; of devs. Usually there will be some "cheats" or alternative methods of looping, buffering data, sometimes referencing data in an unsafe way, or forced, dangerous tail-recursion, where the language doesn't support this feature in an efficient way. Even watching experts writing manually optimised code in some languages, the outcome is not intuitive and its ugliness is often hidden behind a veil of "that's the pattern to use there". The worst is often branchless-code fanatics trying to wedge a transformation into a 0 or 1 outcome to avoid a "costly" &lt;code&gt;if&lt;/code&gt; branch ( 🤦 ). &lt;/p&gt;

&lt;p&gt;Added to this general lack of aesthetic focus in low-level code is the overwhelming cognitive load added to the codebases where this optimisation has taken place. I can hear the responses already - in fact I heard this in an interview once - &lt;em&gt;"but there are tools to alleviate this and&lt;/em&gt; &lt;strong&gt;&lt;em&gt;the way I write code&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;means I don't have that problem."&lt;/em&gt; I'm not going to respond much there except to say that as soon as a person is thinking about the code &lt;em&gt;they&lt;/em&gt; write rather than the code than &lt;em&gt;can&lt;/em&gt; be written and understood by a &lt;em&gt;team&lt;/em&gt; of people with different levels of experience, we're already on different pages regarding teamwork and scalable productivity.&lt;/p&gt;

&lt;p&gt;Rust helps to alleviate the dangers of this minefield of optimisation and yak-shaving boiler-plate coding by providing an ever evolving set of high-level "primitive" functions (especially around functional declarative features) that are said to be "zero cost abstractions" (a concept that will be familiar to C++ devs, in who's world the term originated).&lt;/p&gt;

&lt;p&gt;What does this mean? Well, in general, it means, in around 99% of cases, you will not be able to code something more efficient if you tried to code it yourself. There are some exceptions and they will be  addressed over time but in general it works this way: The compiler will look at the code that uses some looping or high level function and will "unfold" that code to be as fast as it can be. This means (for example) a loop that runs twice may be unfolded into the exact code running twice instead of a loop, or a generic function will be statically generated more than once in the compile (for each type) rather than dynamically derived at runtime  (if that is deemed as more efficient - this can also be a dev's choice in the code) or, as the language evolves, the methods to collate, parse, or (default) sort/search data will improve over time, and we get that optimisation for free (zero cost).&lt;/p&gt;

&lt;p&gt;What's the outcome? We get to maintain lovely high-level declarative code without having to muddy the waters with imperative optimisation cheats (that will need maintaining and reviewing in the future) - and which often impedes the knowledge transition of the intent of the author to the reader of the code - it increases the ability to &lt;a href="https://medium.com/@kushaljoshi/code-like-a-systems-thinker-abe96bc7722a"&gt;code like a systems thinker&lt;/a&gt; (yes, that's a self-plug link but I think it's fairly relevant and interesting here, as I wrote that article before I learned Rust).&lt;/p&gt;

&lt;p&gt;I see imperative developers often diving into the abstractions, finding issues with efficiency or making changes and pasting the code into code of their projects, as if they have achieved something great with this approach. That thinking is a little obtuse at best and a behavioural &lt;a href="https://en.wikipedia.org/wiki/Not_invented_here"&gt;Not Invented Here&lt;/a&gt; mindset problem at worst. There are exceptions to every rule but in general, code is written for humans; to communicate an intent towards solving a data processing problem. This implies that code is written for knowledge transmission and therefore we should embrace high-level abstractions and cleaner code wherever possible. Rust enthusiastically embraces this philosophy.&lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://boats.gitlab.io/blog/post/zero-cost-abstractions/"&gt;https://boats.gitlab.io/blog/post/zero-cost-abstractions/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.rust-lang.org/2015/05/11/traits.html"&gt;https://blog.rust-lang.org/2015/05/11/traits.html&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Energy efficiency
&lt;/h2&gt;

&lt;p&gt;With eye-catching headlines figures like "98% more efficient than Python" and using "23% less energy than C++" Rust is paving the way towards a greener future with the ability to reduce energy usage in server farms on a gigantic scale. A 30% reduction in electricity bills for your raspberry pi might seem borderline pointless - but imagine a 30% saving for a web services business running on tight margins. That's considerable, especially with the oncoming energy crises hitting some Western European countries.&lt;/p&gt;

&lt;p&gt;But is it all about money? For business maybe, but consider this: It was recently claimed (and much debated) that (based on today's technology requirements, code, and architecture, and sources of energy etc, etc) watching a 30 minute show on Netflix could consume a small but significant amount of energy - I'm avoiding stating the amount because of the intense debates it creates; &lt;a href="https://www.carbonbrief.org/factcheck-what-is-the-carbon-footprint-of-streaming-video-on-netflix?"&gt;go read about it yourself&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Regardless of the amount of energy, the implication is thought provoking: When I use a digital service, I am increasing my carbon footprint by amounts that &lt;a href="https://haslab.github.io/SAFER/scp21.pdf"&gt;differ exponentially&lt;/a&gt;, depending on the language and architectural &lt;em&gt;choices&lt;/em&gt; of the digital service provider!&lt;/p&gt;

&lt;p&gt;Note: I wrote the section above as the Russian invasion of Ukraine was just getting started. Today we see spiking energy prices and a looming energy crisis in the UK, where I live most of the time. The ability of a new technology to drastically lower energy consumption in today's world is not merely an added extra; it's a competitive factor that will drive a switch to Rust in many industries, and for tooling across the board (see 'Everybody else's tooling' in Part 3).&lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://greenlab.di.uminho.pt/wp-content/uploads/2017/10/sleFinal.pdf"&gt;https://greenlab.di.uminho.pt/wp-content/uploads/2017/10/sleFinal.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://haslab.github.io/SAFER/scp21.pdf"&gt;https://haslab.github.io/SAFER/scp21.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.theregister.com/2021/11/30/aws_reinvent_rust/"&gt;https://www.theregister.com/2021/11/30/aws_reinvent_rust/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://energyinnovation.org/2020/03/17/how-much-energy-do-data-centers-really-use/"&gt;https://energyinnovation.org/2020/03/17/how-much-energy-do-data-centers-really-use/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  WASM
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In JavaScript, there is a beautiful, elegant, highly expressive language that is buried under a steaming pile of good intentions and blunders.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— Douglas Crockford, JavaScript: The Good Parts (ed. 2008)&lt;/p&gt;

&lt;p&gt;JavaScript, or ECMAScript (which is its true name that around 0% of people use), is my guilty pleasure. It really is an expressive tool that allows for free flowing thought to bleed into the editor, with functional composition features, easy to use async, simple baked in handling of JSON, what's not to like? Well... A lot, actually. There are many ways to discuss this but the simplest is the one that is the most frightening: I once read somewhere that anything that is possible to be structured and written in a programming language will eventually appear in the code (can't remember the reference - I'll add the reference when I find it one day). This means all the crazy stuff that's possible with JavaScript  (e.g. confusing usage of syntax, accidental mathematical errors, parsing quirks, implicit type conversation to the wrong type, overly flexible architecture, silent errors, missing semi-colon issues etc) will eventually be a bugs in JS code everywhere.&lt;/p&gt;

&lt;p&gt;Anyone that has spent time working with JS knows there are ways to work around these issues and that comes with experience, but in general this sounds like 😱 and I feel goes a step towards explaining why front-end development is 10x more complicated than any back-end-only dev already thinks it might be!&lt;/p&gt;

&lt;p&gt;However, baked into &lt;a href="https://webassembly.org/roadmap/"&gt;all the frontend JavaScript engines in nearly all major browsers&lt;/a&gt; is an intermediate language known as &lt;a href="https://webassembly.org/"&gt;Web Assembly&lt;/a&gt; (WASM), that JavaScript is transformed into internally. Today, other languages can generate Web Assembly and ship it to the browser and it already works great everywhere. I mean really great:&lt;/p&gt;

&lt;p&gt;For example, this is a Python interpreter, written completely in Rust, then cross-compiled to WASM, and loaded into the browser as a component: &lt;a href="https://rustpython.github.io/demo/"&gt;https://rustpython.github.io/demo/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uMFgJJKk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/712aia6jb0v3vfb7nlut.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uMFgJJKk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/712aia6jb0v3vfb7nlut.png" alt="Rusty Python running in the browser" width="880" height="676"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the question is this: If all the benefits of Rust can also be applied to frontend code, as the frameworks in this area develop, why would would people continue to choose JavaScript frameworks for web apps?&lt;/p&gt;

&lt;p&gt;What am I saying? That Rust will end the era of JS in the browser? I don't know - probably not. JS can be really fun too. But there already is an &lt;a href="https://angular-rust.github.io/"&gt;Angular Rust&lt;/a&gt; project, WASM integration into some desktop application frameworks, and &lt;a href="https://madewithwebassembly.com/"&gt;several rapidly growing WASM projects&lt;/a&gt;, so things are moving in an interesting direction. At my current work we were able to compile an entire backend rust service into a WASM module that gets loaded into a Vue based UI. We then stream the context that the backend service is receiving to the WASM module and we can "see" the output the service will be producing, in (throttled) realtime - but it's available in the browser with zero load on the backend service itself needing to deliver that data. It's a very interesting application of Rust and provokes thoughts of new alternative architectures.&lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.decipherzone.com/blog-detail/front-end-developer-roadmap-2021"&gt;https://www.decipherzone.com/blog-detail/front-end-developer-roadmap-2021&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://webassembly.org/roadmap/"&gt;https://webassembly.org/roadmap/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://rustwasm.github.io/"&gt;https://rustwasm.github.io/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://yew.rs/"&gt;https://yew.rs/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/MoonZoon/MoonZoon"&gt;https://github.com/MoonZoon/MoonZoon&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tauri - High performance, secure, cross-platform desktop computing
&lt;/h2&gt;

&lt;p&gt;Enabled by the emergence of the &lt;a href="https://github.com/webview/webview"&gt;webview library&lt;/a&gt;, Tauri opens a door that, until now, was half closed unless we were willing to sacrifice some security, performance, choice of tooling, performance, memory management and often performance... Cross platform desktop applications have been a veritable holy grail, just out of reach for those not prepared to sacrifice language-choice, speed, concurrency features, security, and memory usage. Yes, I'm talking about that incredible laptop-fan-stimulating heap of JS junk: &lt;a href="https://www.electronjs.org/"&gt;Electron.js&lt;/a&gt; (Hey, it's just an opinion. Leave me alone). Electron was a great idea and still facilitates a good option for Desktop applications where security or performance is not a major concern, like the WhatsApp desktop client (:halo: ). In fact it's used for a considerable number of apps that you are probably using right now, including one that is used by many Rust developers, including me, because it's pretty awesome: Visual Studio Code 😞.&lt;/p&gt;

&lt;p&gt;Today however, we have Tauri. I'm not going to fly the Tauri flag too much here, except to say that it's an extremely interesting project. Grab you beverage of choice and join Denjell from the Tauri Core Team at their &lt;a href="https://www.youtube.com/watch?v=UxTJeEbZX-0"&gt;fireside video&lt;/a&gt; where he presents what they are working on. Seriously - it's a another Rust revolution driver - it's currently the &lt;a href="https://github.com/EvanLi/Github-Ranking/blob/master/Top100/Rust.md"&gt;fourth highest ranked Rust project&lt;/a&gt;, even &lt;em&gt;above&lt;/em&gt; projects you may know well such as rust-analyzer, Actix, and Tokio.&lt;/p&gt;

&lt;p&gt;I'll say it explicitly for those skim-reading and missing the point. Rapid localised, desktop, rust applications, with a high-speed UI that leveraged the skill of &lt;em&gt;almost any front-end framework developer&lt;/em&gt;, are about to become a reality. It won't be surprisingly to see every front-end dev adding Rust to their already ridiculously over-weight tool-belts.&lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://tauri.studio/"&gt;https://tauri.studio/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://evanli.github.io/Github-Ranking/Top100/Rust.html"&gt;https://evanli.github.io/Github-Ranking/Top100/Rust.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.smashingmagazine.com/2020/07/tiny-desktop-apps-tauri-vuejs/"&gt;Creating Tiny Desktop Apps With Tauri and Vue.js, Smashing Magazine, Kelvin Omereshone, July 2020&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Thanks for reading up to this point. If this article is interesting, or even infuriating, I can promise more of the same in Part 3 where I will cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Macros!&lt;/li&gt;
&lt;li&gt;Cryptocurrency and Blockchains&lt;/li&gt;
&lt;li&gt;Everybody else's tooling&lt;/li&gt;
&lt;li&gt;Speed&lt;/li&gt;
&lt;li&gt;No garbage collection&lt;/li&gt;
&lt;li&gt;Memory safety features&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rust</category>
      <category>performance</category>
      <category>systems</category>
    </item>
    <item>
      <title>18 factors powering the Rust revolution, Part 1 of 3</title>
      <dc:creator>Kushal Joshi</dc:creator>
      <pubDate>Sun, 10 Apr 2022 20:52:33 +0000</pubDate>
      <link>https://forem.com/kushalj/18-factors-powering-the-rust-revolution-part-1-of-3-3imc</link>
      <guid>https://forem.com/kushalj/18-factors-powering-the-rust-revolution-part-1-of-3-3imc</guid>
      <description>&lt;p&gt;Cover Photo by &lt;a href="https://unsplash.com/@marie_enk?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Marie P&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/rust?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: I work at &lt;a href="https://keyrock.eu/" rel="noopener noreferrer"&gt;Keyrock&lt;/a&gt; but views expressed here are my own. However, if you like the views, do go checkout the company as it is full of awesome people and I have learned much whilst working there, including Rust.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where we were
&lt;/h2&gt;

&lt;p&gt;In 2019, whilst working at Keyrock, the dev team (around 4-6 of us during that period) had reached the limits of what our JavaScript prototype trading services could do when it came to handling the complex world of high frequency trading (HFT). We had to process multiple feeds of information concurrently and have data available, within certain time constraints, inside a single service, with reliability. Of course, I am aware JS was probably not the best choice but we had a team-wide capability and have you ever tried to prototype in JS? It's so fast... Anyway,  we had rapidly prototyped our first generation of services in JavaScript and whilst JS is suited to many use cases, (first strong opinion of the article) HFT is not one of them. Not that this stopped us trying 😇.&lt;/p&gt;

&lt;p&gt;In choosing a new technology for the second generation of services we went through a research phase, trying out Go-lang, Kotlin, Elixir, and also another language I was tinkering with... Rust. We knew we didn't want to go the C++ route for reasons I will expand on in a &lt;em&gt;different&lt;/em&gt; article. I'd done some Rust tests with basic sorting algorithms and couldn't quite believe the speed and kept going on about it at our office in Brussels. Another senior dev challenged the technology (vs what we had in production) and said he didn't want to keep hearing me go on about it unless it was significantly faster than what we had; he did some deeper experiments himself and soon went on to become our most ardent Rust emissary. Rust had the speed we needed, approachable concurrency primitives, acceptable syntax, and great tooling. We dove into this new world and today most of our production code is Rust.&lt;/p&gt;

&lt;p&gt;However, there was something else about this language, even at the early stages, that I couldn't quite put my finger on. While sometimes complex to deal with, at the time I knew our decision was &lt;em&gt;right&lt;/em&gt; even though I couldn't expand much on why, beyond the speed, lack of garbage collector, and concurrency support. Was that risky? I didn't think so - we did (just) enough research and I personally had Apple's philosophy ringing in my head - that we should use the opportunity to both eliminate a technology we had maxed out, and replace it with a new technology entering it's dawn.&lt;/p&gt;

&lt;p&gt;Three years later I can express explicitly what I think I sensed intuitively - And I feel compelled to write about it! If you are new to Rust, I know where you are going... it is a world of initial confusion and frustration, followed eventually by a feeling that this just &lt;em&gt;makes sense&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Today we see rust everywhere. Blockchains, web services, libraries supporting Python and JavaScript modules, even inside the holy of holies - the Linux kernel. This three-part article is about &lt;em&gt;why&lt;/em&gt; that is the case.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why is this happening?
&lt;/h2&gt;

&lt;p&gt;Is it a cult? Is it mass hysteria? Or it just the latest fashion in hipster coding languages that's running out of control? Well, maybe there's an &lt;a href="https://insights.stackoverflow.com/survey/2021#technology-worked-with-vs-want-to-work-with" rel="noopener noreferrer"&gt;element of this&lt;/a&gt; - but there are also many concrete reasons this revolution is happening. And make no mistake - it is a revolution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67qkhcxvrc5d8o0r5h1t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67qkhcxvrc5d8o0r5h1t.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://insights.stackoverflow.com/survey/2021#technology-worked-with-vs-want-to-work-with" rel="noopener noreferrer"&gt;From the 2021 Stack Overflow Developer Survey&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can begin with the factors that are easy to understand and accept, and then I'll get into the less obvious but immensely powerful features of the language.&lt;/p&gt;

&lt;p&gt;Let's start with the entire list and then break them down:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 1&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Syntax&lt;/li&gt;
&lt;li&gt;Separation of Data and Code&lt;/li&gt;
&lt;li&gt;Type safety&lt;/li&gt;
&lt;li&gt;Declarative Programming and Functional Composition features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/kushalj/18-factors-powering-the-rust-revolution-part-2-of-3-h73"&gt;Part 2&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Evolved package management&lt;/li&gt;
&lt;li&gt;Concurrency ready std lib and great async support&lt;/li&gt;
&lt;li&gt;Clear, helpful compiler errors&lt;/li&gt;
&lt;li&gt;Zero cost abstractions (Optimisation)&lt;/li&gt;
&lt;li&gt;Energy efficiency&lt;/li&gt;
&lt;li&gt;WASM&lt;/li&gt;
&lt;li&gt;Tauri - High performance, secure, cross-platform desktop computing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/kushalj/18-factors-powering-the-rust-revolution-part-3-of-3-2p6b"&gt;Part 3&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Macros!&lt;/li&gt;
&lt;li&gt;Cryptocurrency and Blockchains&lt;/li&gt;
&lt;li&gt;Everybody else's tooling&lt;/li&gt;
&lt;li&gt;Speed&lt;/li&gt;
&lt;li&gt;No garbage collection&lt;/li&gt;
&lt;li&gt;Memory safety features&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;At university I had some experience with Pascal and Smalltalk (it was a long time ago), before that I was coding Motorola 68000 series assembler on a Commodore Amiga and before that (my first exposure to low-level coding) I was playing with machine code on the &lt;a href="https://www.c64-wiki.com/wiki/MOS_Technology_6510" rel="noopener noreferrer"&gt;MOS Technology 6510 in the Commodore 64&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Why am I sharing this? Because the coding syntax in that world was not "C" style. During the following years I also used C, C++, Java, JavaScript, Perl, C#, and later R, which are known as C-type  or  &lt;a href="https://en.wikipedia.org/wiki/List_of_C-family_programming_languages" rel="noopener noreferrer"&gt;C-family of languages&lt;/a&gt;. (Python is a little different but it's not far off). The other languages above are not and this often leads to frustration in learning the language and difficulty in finding people that can "convert" to those languages.&lt;/p&gt;

&lt;p&gt;The C-type languages above are actually a very small selection of the number of languages using C-family syntax (around 73). Why so many? Well, after the success of C &amp;amp; C++, it was theorised that it would be much easier to dive into a familiar syntax if learning a new language. For example Sun Microsystems worked on a project to clean up and and make C++ better by being able to run it everywhere - called Oak. Later they needed to change the name and they called it Java.&lt;/p&gt;

&lt;p&gt;The fact that Rust retains this C-type "DNA" is by design. Systems programming is primarily the domain of C &amp;amp; C++ developers, with a smaller group of Objective-C (a superset of C) and Swift developers in the Apple universe. Rust honours this legacy - and targets those developers - by fusing a very familiar syntax together with symbols and features that facilitate new paradigms in a way that is only mildly distracting at the beginning of the Rust journey, and makes a lot of sense when you get used to it.&lt;/p&gt;

&lt;p&gt;I've had a tech-challenge submissions from C++ devs with only 1-2 months of Rust experience and they "get it", often much more quickly than devs with other backgrounds. These submissions are not idiomatic Rust at all but they show the power of a familiar syntax.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separation of Code and Data
&lt;/h2&gt;

&lt;p&gt;I'm a big fan of Functional Programming (&lt;a href="https://en.wikipedia.org/wiki/FP_(programming_language)" rel="noopener noreferrer"&gt;FP&lt;/a&gt;) - and Elixir and Erlang in particular. The original sub-heading here was: &lt;strong&gt;&lt;em&gt;A reasonable step away from the era of misunderstood Object Oriented philosophy and architecture, towards separation of code and data, using Traits and Functions.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's a long section title. I rewrote it several times. It was really difficult to find a title that represents this factor that wouldn't trigger the masses of converts of the confusing, and usually misunderstood, interpretation of Object Oriented Programming (OO, or OOP), who have spent years teaching and developing methods and patterns for structuring code, based on an approach that has virtually zero evidence but instead was the result of millions spent in marketing, over around 3-4 decades, plus the directed efforts of &lt;a href="https://www.sigcis.org/files/Hsu%20--%20Software%20Crisis%20and%20OOP.pdf" rel="noopener noreferrer"&gt;non-technical managers&lt;/a&gt; to split coding into "units of work".&lt;/p&gt;

&lt;p&gt;There are various histories that document this (my favourite is by the eternally affable &lt;a href="https://www.youtube.com/watch?v=QyJZzq0v7Z4" rel="noopener noreferrer"&gt;Richard Feldman&lt;/a&gt;, author of the &lt;a href="https://elm-lang.org/" rel="noopener noreferrer"&gt;Elm&lt;/a&gt; programming language). A very short summary:&lt;/p&gt;

&lt;p&gt;A long time ago programs were made of data and procedures and were often a highly coupled mess of data sharing. Then a guy call Alan (&lt;a href="https://en.wikipedia.org/wiki/Smalltalk" rel="noopener noreferrer"&gt;Kay, 1966&lt;/a&gt;) said programs should pass messages instead of share data (&lt;a href="https://en.wikipedia.org/wiki/Simula" rel="noopener noreferrer"&gt;an idea from Simula, 1962&lt;/a&gt;). He and others made a language of objects with associated functions that passed messages between objects (Smalltalk '72). But it seems (to keep the story short) he was somewhat mis-heard, and later languages implemented these ideas with their own odd assumptions.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“C++ is a significant barrier to learning and correctly applying object-oriented methodologies and technologies.”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— Bruce Webster, Pitfalls of object-oriented development&lt;br&gt;
(New York: M&amp;amp;T Books, 1995), 139 (quoted in Hsu, Mahoney - see below)&lt;/p&gt;




&lt;p&gt;Alan also tried to explain some of the confusion many years later:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“I made up the term ‘object-oriented’, and I can tell you I didn’t have C++ in mind.”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— Alan Kay, OOPSLA ‘97&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things.”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— Alan Kay, 1998, &lt;a href="https://wiki.c2.com/?AlanKayOnMessaging" rel="noopener noreferrer"&gt;https://wiki.c2.com/?AlanKayOnMessaging&lt;/a&gt;&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"I'm sorry that I long ago coined the term "objects" for this topic because it gets many people to focus on the lesser idea."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— Alan Kay, 2014, &lt;a href="http://lists.squeakfoundation.org/pipermail/squeak-dev/1998-October/017019.html" rel="noopener noreferrer"&gt;http://lists.squeakfoundation.org/pipermail/squeak-dev/1998-October/017019.html&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Unfortunately though, by that time the world had mostly put Functional Programming, and landmark computer science theories such as Carl Hewitt's &lt;a href="https://arxiv.org/vc/arxiv/papers/1008/1008.1459v8.pdf" rel="noopener noreferrer"&gt;Actor model theory&lt;/a&gt; (which were partly inspired by Smalltalk '72) aside, and progressed the "OO super-soldier" ideas of Objective-C and C++ and mutated them into the uncontrollable "Hulk" of Java. (a language which once had so much kudos that another mostly unrelated language was named after it, and mutated to look like it).&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Object-oriented programming, for instance, was seen by one adherent as a way to impose the traditional workplace relationships of manufacturing. Taylorism was implicit in all the proposed solutions to the crisis."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— &lt;a href="https://www.academia.edu/10418616/Connections_between_the_Software_Crisis_and_Object_Oriented_Programming" rel="noopener noreferrer"&gt;Hsu, Cornell University, 2020&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;By the time the corporate sponsors and their agents had finished with computer magazine articles and adverts, university course sponsorships, and mass marketing of their consultancy services behind the languages, the idea that OO was the de facto way to code had become ubiquitous. That word will back later in this article.&lt;/p&gt;

&lt;p&gt;There are exceptions: Managing a handler for a side-effect should (for me) pretty much always be architecture similar to an "object + methods" because it just works wonderfully (e.g. DB driver code, wrapped in a Class or Struct + Methods in Rust) because there is &lt;em&gt;an exclusive association between the code and datatype&lt;/em&gt;, in that case. But for flexible, sane, maintainable architecture for concurrent, distributed systems, (meaning mass interconnectivity of functionality) there are less troublesome, more flexible, elegant, and testable alternatives (for examples, please go read &lt;a href="https://archivesspace.mit.edu/repositories/2/resources/871" rel="noopener noreferrer"&gt;Carl Hewitt&lt;/a&gt; and argue with him and others like the creators of Erlang, and not me, please. Or better, first watch the excellent Brian Will and Richard Feldman videos linked below).&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"I think the lack of reusability comes in object-oriented  languages, not functional languages. Because the problem with  object-oriented languages is they’ve got all this implicit environment  that they carry around with them.&lt;/em&gt; &lt;strong&gt;You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you have referentially transparent code, if you have pure  functions — all the data comes in its input arguments and everything goes out and leave no state behind — it’s incredibly reusable."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— &lt;a href="https://codersatwork.com/joe-armstrong.html" rel="noopener noreferrer"&gt;Joe Armstrong on software reusability&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Thankfully, the &lt;em&gt;pain&lt;/em&gt; of the "taught" way of OO coding is finally leading the world to &lt;em&gt;rediscovery&lt;/em&gt; of FP benefits (even Java has an FP/OO hybrid successor - &lt;a href="https://www.infoworld.com/article/3224868/what-is-kotlin-the-java-alternative-explained.html" rel="noopener noreferrer"&gt;Kotlin&lt;/a&gt;) and alternative practices and architectural approaches. Mozilla and the Rust core-team thankfully chose an alternative architectural approach when making Rust: &lt;strong&gt;Traits&lt;/strong&gt;. This was a stroke of genius in my opinion as it has resulted in (very amusingly) people migrating towards FP architectural practices without even knowing they are doing this, often whilst complaining about FP.&lt;/p&gt;

&lt;p&gt;As a simple intro, Rust devs can create data structures called Structs and associated functions and/or methods (with an explicit self reference) with those Structs. This however has limitations when creating abstractions and eventually leads to the discovery of Rust Traits. Traits are akin to &lt;em&gt;interfaces&lt;/em&gt; in some OO languages. A trait usually consists of function signatures that need to be expressed by a struct claiming to express that trait. In this way they are like a set of interrelated behaviours for some unknown type. Not quite a pure function but a constrained function, with expectations of the type that will be transformed by the function.&lt;/p&gt;

&lt;p&gt;So what? Well... it means we can:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a set of pure functions that process data, or we can...&lt;/li&gt;
&lt;li&gt;define a set of methods as behaviours for some generic type with some expected traits, or...&lt;/li&gt;
&lt;li&gt;we can create a known Struct with a set of traits and expressed functions for those traits, specifically designed for that Struct - a little like an Object + methods - but with the trait we retain the principle of the struct - or "object" - being &lt;a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod" rel="noopener noreferrer"&gt;open for extension&lt;/a&gt; - one of the first five principles of object oriented design, all of which can still be expressed in Rust without permanently coupling data to code.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This lack of opinion in the features of the language means this is enough flexibility (for example) to build applications with the hailed &lt;a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell" rel="noopener noreferrer"&gt;"functional core, imperative (OO) shell"&lt;/a&gt; architecture, and yet provide the option to lean on the &lt;em&gt;safety&lt;/em&gt; of Rust (see Part 3) when coding this way. In this way Rust makes for an incredible language that allows for a wide scope of architectural styles. It says "don't be afraid! If you love the pain of early architectural assumptions that OO enforces via the myth of encapsulation, you can still feel that hurt with Rust!". ;)&lt;/p&gt;

&lt;h4&gt;
  
  
  More Info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.theregister.com/2003/06/09/sun_preps_500m_java_brand/" rel="noopener noreferrer"&gt;Sun preps 500m Java brand push&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=QM1iUe6IofM" rel="noopener noreferrer"&gt;Object-Oriented Programming is Bad, Brian Will, Youtube, 2016&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=QyJZzq0v7Z4" rel="noopener noreferrer"&gt;Why isn't functional programming the norm? Feldman, Youtube 2019&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.sigcis.org/files/Hsu%20--%20Software%20Crisis%20and%20OOP.pdf" rel="noopener noreferrer"&gt;Connections between the Software Crisis and Object-Oriented Programming, Hansen Hsu&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.math.unipd.it/~tullio/IS-1/2004/Approfondimenti/BrooksNoSilverBullet.html" rel="noopener noreferrer"&gt;No Silver Bullet, Brooks, 1987&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell" rel="noopener noreferrer"&gt;Functional Core, Imperative Shell, destroyallsoftware.com, 2012&lt;/a&gt; by &lt;a href="https://twitter.com/garybernhardt" rel="noopener noreferrer"&gt;@garybernhardt&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Type safety
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Static Types
&lt;/h3&gt;

&lt;p&gt;In some of the &lt;a href="https://quorumlanguage.com/evidence.html" rel="noopener noreferrer"&gt;few studies&lt;/a&gt; carried there are observed benefits to statically typed languages. If you haven't heard of &lt;a href="https://quorumlanguage.com/" rel="noopener noreferrer"&gt;Quorum&lt;/a&gt;, check them out - they are bringing evidence to software development - an industry where our &lt;em&gt;practices&lt;/em&gt; &lt;a href="https://cacm.acm.org/magazines/2005/11/6072-the-plot-to-deskill-software-engineering/fulltext" rel="noopener noreferrer"&gt;were prescribed by managers and shaped from a desire to deskill and control developers&lt;/a&gt; (see also, Hsu, above) rather than via gathering evidence of best practice.&lt;/p&gt;

&lt;p&gt;For developer convenience in IDEs static typed languages win all day long, as they do when it comes to automated error checking (e.g. functional signatures) in code. Typing is so important to some developers that JavaScript has an entire superset language created around it that enforces types (cheers Microsoft - 👍 you did a good. I don't get to say that often).&lt;/p&gt;

&lt;h3&gt;
  
  
  Dynamic Types
&lt;/h3&gt;

&lt;p&gt;Look I'm not selling the point - I love me some JavaScript when I'm playing around or I'm guiding friends/relatives to learn code. But if I want to create a financial system/trading bot etc, in that context, explicitness rules. I don't want to second guess type inference anywhere. But I really like dynamic types; enter Rust Generics and Traits...&lt;/p&gt;

&lt;h3&gt;
  
  
  Rust Generics
&lt;/h3&gt;

&lt;p&gt;The generics system in Rust allows for the ability to specify the Traits (see section above) that the the "type" expresses. In simplistic terms this means we can use dynamic types (a generic type) but restrict those types to those that express a specific set of Traits. It often feels like the best of both worlds. It can create some complexity in areas but in general it works extremely well for modelling complex abstractions.&lt;/p&gt;

&lt;h4&gt;
  
  
  More Info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://quorumlanguage.com/evidence.html" rel="noopener noreferrer"&gt;Human factors evidence in language design, Quorum&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.researchgate.net/publication/289128968_Software_as_Labor_Process" rel="noopener noreferrer"&gt;Software as a Labour Process, Ensmenger &amp;amp; Aspray, 2002&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://people.eecs.berkeley.edu/~ksen/papers/typedevil.pdf" rel="noopener noreferrer"&gt;TypeDevil: Dynamic Type Inconsistency Analysis for JavaScript, Pradel, Shuh &amp;amp; Sen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cacm.acm.org/magazines/2005/11/6072-the-plot-to-deskill-software-engineering/fulltext" rel="noopener noreferrer"&gt;The Plot to Deskill Software Engineering, Glass, Communications of the ACM, 2015&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Declarative Programming and Functional Composition features
&lt;/h2&gt;

&lt;p&gt;There are common problems with attempting to encapsulate the world as objects and behaviours all the time (see Separation of Code and Data, above). Once those behaviours start mixing, the variations require more and more complex objects with multiple parents and specific variations of methods, often leading to a lot of duplication of code and code that is difficult to maintain when changes need to be applied (like hybrid objects that facilitate/abstract communication between completely unrelated objects) - The way a method expresses behaviour (the knowledge of it) is spread throughout the 'children' of the objects (it's not very &lt;a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="noopener noreferrer"&gt;DRY&lt;/a&gt; code and it's not very flexible). There's an entire industry of rules and patterns to help deal with this mess.&lt;/p&gt;

&lt;p&gt;Functional Programming uses a paradigm that helps to solve these types of scenarios in a rather elegant way: Functional Composition. Generic, &lt;a href="https://en.wikipedia.org/wiki/Pure_function" rel="noopener noreferrer"&gt;"pure" functions&lt;/a&gt; replace methods-attached-to-data and instead "process" data that conforms to defined characteristics. This way every inherited variation of the data structure can be processed by a set of &lt;em&gt;functions that describe behaviours&lt;/em&gt;, and those behaviours can then be composed to create more complex behaviours. But each behaviour is only expressed once (#極度乾燥). You have probably used these approaches already if you have played with Iterators or Streams.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://doc.rust-lang.org/book/ch13-00-functional-features.html" rel="noopener noreferrer"&gt;How does Rust facilitate this?&lt;/a&gt; With Generics, Traits, the &lt;code&gt;Fn()&lt;/code&gt; and related types, closures, and iterators. Most importantly, these features are not pushed into your face with enforced interior immutability like other FP languages. Rust instead provides a &lt;em&gt;buffet&lt;/em&gt; of functional features that one may or may not choose to use - but it's a heavily stacked buffet, leading to two outcomes: Experienced FP devs generally won't go hungry, and people new to FP can learn, experiment, and introduce architectural changes gradually.&lt;/p&gt;

&lt;p&gt;A small example that will make sense to FP devs but may irritate or confuse people new to the FP paradigm: By default all variables are immutable and functions all implicitly return their "final value", or the result of their final expression, as long as there is no semi-colon ending the expression. If there is no value (if there is a semi-colon that ends the last expression), the function will still return an empty tuple. Taken together this means Rust functions (by default, without usage of the &lt;code&gt;mut&lt;/code&gt; keyword) have no internally mutable variables. They process some input, and output a new value/s based on that input. If we then remove the usage of &lt;code&gt;mut&lt;/code&gt; on the params input to the function, we have an FP style data-processing function that cannot mutate it's input or it's internal variables, and thereby results in an &lt;a href="https://en.wikipedia.org/wiki/Idempotence" rel="noopener noreferrer"&gt;idempotent&lt;/a&gt; function (always produces the same effect as the first time it's run).&lt;/p&gt;

&lt;p&gt;How can these be used with traditional loops and if/else branching? Well that would be clumsy - so Rust also contains high level abstractions like map, filter, fold, and other reducers that allow the building of iterators that apply functions; hello declarative programming! Structured correctly, this raises the possibility of creating highly optimised &lt;a href="https://en.wikipedia.org/wiki/Branch_(computer_science)" rel="noopener noreferrer"&gt;branchless&lt;/a&gt; code - but branchless code that is still readable!&lt;/p&gt;

&lt;p&gt;If this sounds alien to your "normal" coding world, it's likely you are coming from an imperative/object-oriented programming paradigm - &lt;em&gt;and that's fine&lt;/em&gt;. Even without knowing the definition of these mysterious words: We can add &lt;code&gt;mut&lt;/code&gt; to function params, and &lt;code&gt;mut&lt;/code&gt; in front of variables,  and all the usual if/else/for/while tools are present.&lt;/p&gt;

&lt;p&gt;In this way Rust uniquely exists as a bridging or transition tool from one paradigm to the other. &lt;/p&gt;

&lt;h4&gt;
  
  
  More info:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.freecodecamp.org/news/imperative-vs-declarative-programming-difference/" rel="noopener noreferrer"&gt;https://www.freecodecamp.org/news/imperative-vs-declarative-programming-difference/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.fpcomplete.com/blog/2018/10/is-rust-functional/" rel="noopener noreferrer"&gt;https://www.fpcomplete.com/blog/2018/10/is-rust-functional/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/hemanth/functional-programming-jargon" rel="noopener noreferrer"&gt;https://github.com/hemanth/functional-programming-jargon&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://hbfs.wordpress.com/2008/08/05/branchless-equivalents-of-simple-functions/" rel="noopener noreferrer"&gt;https://hbfs.wordpress.com/2008/08/05/branchless-equivalents-of-simple-functions/&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;That's it for Part 1. In Part 2 I'm going to focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Evolved package management&lt;/li&gt;
&lt;li&gt;Concurrency ready std-lib and great async support&lt;/li&gt;
&lt;li&gt;Clear, helpful compiler errors&lt;/li&gt;
&lt;li&gt;Zero cost abstractions (Optimisation)&lt;/li&gt;
&lt;li&gt;Energy efficiency&lt;/li&gt;
&lt;li&gt;WASM&lt;/li&gt;
&lt;li&gt;Tauri - High performance, secure, cross-platform desktop computing&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rust</category>
      <category>performance</category>
      <category>systems</category>
    </item>
    <item>
      <title>Connect to a single MongoDB instance from rust</title>
      <dc:creator>Kushal Joshi</dc:creator>
      <pubDate>Mon, 03 Feb 2020 10:20:28 +0000</pubDate>
      <link>https://forem.com/kushalj/connect-to-a-single-mongodb-instance-from-rust-16gf</link>
      <guid>https://forem.com/kushalj/connect-to-a-single-mongodb-instance-from-rust-16gf</guid>
      <description>&lt;p&gt;The rust driver for Mongo is still in its infancy. It is well documented in terms of the signatures of each function and feature but very limited when it comes to examples. I spent way to long trying to make a simple connection to local and staging Mongo instances and the examples just don't work.&lt;/p&gt;

&lt;p&gt;Solution:&lt;br&gt;
They are missing the direct_connection option flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="c"&gt;// Set URI&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"mongodb://localhost:27017/"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c"&gt;// Setup options&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;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;ClientOptions&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="py"&gt;.direct_connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c"&gt;// Create the Client&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_options&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c"&gt;// Use the Client&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="mi"&gt;_&lt;/span&gt;&lt;span class="n"&gt;dbs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;dbg!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="nf"&gt;.list_database_names&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;None&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;Note for noobs, you don't need the dbg!() wrapper - that is just to log the output easily. You can just:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="mi"&gt;_&lt;/span&gt;&lt;span class="n"&gt;dbs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="nf"&gt;.list_database_names&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>driver</category>
      <category>rust</category>
      <category>database</category>
      <category>mongodb</category>
    </item>
  </channel>
</rss>
