<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bastian Gruber</title>
    <description>The latest articles on DEV Community by Bastian Gruber (@gruberb).</description>
    <link>https://dev.to/gruberb</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%2F146875%2F68ea14ce-cc5e-4ed7-87fb-31d36b79a3ff.jpg</url>
      <title>DEV Community: Bastian Gruber</title>
      <link>https://dev.to/gruberb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gruberb"/>
    <language>en</language>
    <item>
      <title>Announcing my book "Rust Web Development"</title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Tue, 09 Nov 2021 20:31:20 +0000</pubDate>
      <link>https://dev.to/gruberb/announcing-my-book-rust-web-development-5b4b</link>
      <guid>https://dev.to/gruberb/announcing-my-book-rust-web-development-5b4b</guid>
      <description>&lt;h2&gt;
  
  
  A pragmatic and "in-depth-enough" book about writing web services/applications in Rust
&lt;/h2&gt;

&lt;p&gt;Hey folks! Long time since I have posted here! &lt;a href="https://dev.to/gruberb/intro-to-web-programming-in-rust-for-nodejs-developers-lp"&gt;My original articles&lt;/a&gt; on this platform kicked it all off for me: Getting into Rust, trying to explain to myself how to think about Rust when coming from NodeJS etc. &lt;/p&gt;

&lt;p&gt;One thing lead to another and now I am currently writing a book with Manning titled "Rust Web Development". I want to take you on a journey into Rust and how to think about Web Development when working with Rust.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ready for prime time?
&lt;/h3&gt;

&lt;p&gt;Many things are new, different and at first glance, not as easy when comparing it to Java or NodeJS. However, once you understood the moving pieces, Rust is a joy to work with, a super solid language and enough great tooling to use it, right now, in production. &lt;/p&gt;

&lt;p&gt;It also has some tooling from the ecosystem which make it, in my view, a no-brainer to use for any future web service you are going to push to production. &lt;/p&gt;

&lt;p&gt;So in this short post, I want to invite you to come along with me on this journey, and figure it out as we go along converting to Rust! The language itself will eventually be used in the Linux Kernel (fingers-crossed), replace legacy systems and will find many friends in the new world which is currently built right before our eyes (Facebook calls it the metaverse...). &lt;/p&gt;

&lt;h3&gt;
  
  
  See for yourself
&lt;/h3&gt;

&lt;p&gt;There is a &lt;a href="https://rustwebdevelopment.com/"&gt;website&lt;/a&gt; to the book, where I hopefully soon publish more little articles about Rust and the web, and I will list all talks I gave and Podcasts I am on, so you can learn more about this exciting new way of building for the web. &lt;/p&gt;

&lt;p&gt;And, if you made it this far, with &lt;a href="http://mng.bz/yJmy"&gt;this link you will get a discount purchasing the book&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It is currently in MEAP, but will be ready starting 2022. &lt;/p&gt;

&lt;p&gt;I am looking forward to your feedback!&lt;/p&gt;

</description>
      <category>rust</category>
      <category>book</category>
    </item>
    <item>
      <title>Getting paid in Bitcoin - Experiences and misconceptions</title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Wed, 03 Jun 2020 11:20:38 +0000</pubDate>
      <link>https://dev.to/gruberb/getting-paid-in-bitcoin-experiences-and-misconceptions-3f2o</link>
      <guid>https://dev.to/gruberb/getting-paid-in-bitcoin-experiences-and-misconceptions-3f2o</guid>
      <description>&lt;p&gt;For the last year or so, I worked as a self employed consultant for companies based in the US. Therefore, I was getting paid in a foreign currency.&lt;/p&gt;

&lt;p&gt;Now, I never encountered a situation like this and quickly found out how expensive it is to move money across borders and banks. Now my wife is Canadian and we both live in Germany, so I experienced the hassle of getting money inside the family across the border from second hand.&lt;/p&gt;

&lt;p&gt;Getting paid for a job is a little different though, because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have to pay taxes &lt;/li&gt;
&lt;li&gt;You want it in a timely manner&lt;/li&gt;
&lt;li&gt;You don’t want to pay a lot of fees&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Luckily, I had the option between US Dollar and Bitcoin, so I tried both for a while to see what’s the cheapest and fastest way for me.&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting paid in USD vs. BTC
&lt;/h1&gt;

&lt;p&gt;A rough TL;DR looks like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If your company has to buy Bitcoin to be able to pay you in Bitcoin, it is more expensive then just accepting US Dollar&lt;/li&gt;
&lt;li&gt;If your company already owns Bitcoin, it is way faster and cheaper to get paid in Bitcoin vs. in USD&lt;/li&gt;
&lt;li&gt;If you have to transfer your Bitcoin to Euro (which you probably have to do to pay rent etc.), it is still cheaper to get Bitcoin, sell it on an exchange and get the money onto your bank account. To some degree it is as fast or even faster then USD-&amp;gt;EUR&lt;/li&gt;
&lt;li&gt;You have to use Transferwise to be able to get the cheapest and fastest way to get paid in USD and transfer it to EUR, everything else is way more expensive and slower then to use Bitcoin&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I am not a big Bitcoin maximalist, but I like the idea of programmable money. Having worked for larger financial institutions, I am aware of the tech stack and slow processes banks have to fight against. &lt;/p&gt;

&lt;h1&gt;
  
  
  Numbers
&lt;/h1&gt;

&lt;p&gt;Lets say you bill your client 10k USD. If you want to transfer this amount of money to EUR:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transferwise: &lt;a href="https://transferwise.com/gb/pricing/send-money?source=USD&amp;amp;target=EUR&amp;amp;payInMethod=BANK_TRANSFER&amp;amp;sourceAmount=10000"&gt;51.18 USD&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Bitcoin: Around 30 Dollar (give or take) where the transport from one address to the other can cost you around 2 Dollar, and around 26 Dollar to sell the Bitcoin onto the market (0.26% at some exchanges).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Transferwise vs. Bitcoin
&lt;/h1&gt;

&lt;p&gt;Why using Transferwise? Well with this service, after creating an account and verifying yourself (which can take up to two days), you can create US bank accounts and EUR bank accounts with a few clicks. &lt;/p&gt;

&lt;p&gt;After you have a virtual US bank account with Transferwise, you can give these details to your company or person which pays you. After receiving the Dollars, you can then convert them to EUR (based on the conversation rate + fee on Transferwise), and then transfer the money to your own bank account in Germany.&lt;/p&gt;

&lt;p&gt;Transferwise is cheaper because they basically trick the banking system. They won’t immediately send the money across borders. They store transactions and make larger bulk transfer after a certain timeframe (lets say after each month). Transferwise has partnered with banks around the globe, so they can internally book transactions, and then rebalance after each month. &lt;/p&gt;

&lt;p&gt;These bulk transfers make it faster (because they just internally move money) and cheaper (because of the bulk transfers each month). &lt;/p&gt;

&lt;p&gt;Now it is pretty mind boggling to argue against Bitcoin when you have to cheat your way through the banking system to be able to compete against a, by design, slower method of transfer.&lt;/p&gt;

&lt;h1&gt;
  
  
  Experiences using Transferwise
&lt;/h1&gt;

&lt;p&gt;The service itself is great, cheap and fast. I used it a few times and my Dollars got converted and were on my bank account in Germany in Euro at the same day.&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting paid in Bitcoin
&lt;/h1&gt;

&lt;p&gt;Lets assume the company which pays you already has Bitcoin. Therefore the speed of transaction is around 2-15 Minutes based on my experiences. Once the company send the transaction, you can follow it “live” on the Blockchain and see when it should arrive. I never felt so much in control of my money then when receiving Bitcoin.&lt;/p&gt;

&lt;p&gt;I used an address on an exchange, so I could, after receiving BTC, sell it and transfer the money to my German bank account. Now I would love the price of BTC to be stable and my landlord and other suppliers to accept it, but for this to be happen, the price of BTC has to rise a magnitude of what it is now.&lt;/p&gt;

&lt;p&gt;The whole process of receiving BTC, selling it and getting the money to my bank account was in a range of 5-8 hours. The most time lag came from sending Euros from the exchange to my bank account.&lt;/p&gt;

&lt;h1&gt;
  
  
  What about taxes?
&lt;/h1&gt;

&lt;p&gt;Now people assume you gamble when you are getting paid in Bitcoin. That’s not true. It’s just another currency. Instead of being backed by the largest military in the world, it is in the hands of everyone who want to participate in it. &lt;/p&gt;

&lt;p&gt;The German government doesn't particularly care in what and where you are getting paid. Which means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have a contract with a company or you write a bill&lt;/li&gt;
&lt;li&gt;After you receive the money &lt;em&gt;somehow&lt;/em&gt;, you have to declare this as income in your taxes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So even if you get paid in apartments on a Greek island, you still have to pay taxes on the value of this payment in EUR, in Germany after the end of the year.&lt;/p&gt;

&lt;p&gt;Now when you receive Bitcoin and you sell it, you can either make a loss or a profit (or you sell it for the exact same price you received it). If you make a profit off this transaction, you have to pay taxes on this profit as well. If you make a loss, you can even reduce your income and pay less income at the end of the year. That's it. &lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;All in all, it's actually not very exciting. I chose Bitcoin because it was faster and I believe it's good to get familiar with the future of money sooner rather than later. &lt;/p&gt;

&lt;p&gt;If you want to get paid in Bitcoin, make sure you choose an exchange you can trust, and don't let Bitcoins pile up at your exchange, but rather use a hardware wallet to store them. &lt;/p&gt;

&lt;p&gt;So if your company owns Bitcoin, it is faster and cheaper to get paid then it is in a foreign currency. If your company operates in Europe and you could already choose EUR, you will pay a higher fee to get paid in BTC if you have to sell it (which you probably will).&lt;/p&gt;

&lt;h1&gt;
  
  
  Side note
&lt;/h1&gt;

&lt;p&gt;Be sure you understand that Bitcoin might not compete with the European banking system, and there are cheaper and faster currencies out there. Bitcoin could be used as a transition currency for the old FIAT world to move over to the digital space, and to be established as a form of reserve currency. But, as of now, it is a nice and easy way to cross borders with it.&lt;/p&gt;

</description>
      <category>bitcoin</category>
      <category>money</category>
      <category>exchange</category>
    </item>
    <item>
      <title>Experiences of working fully remote </title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Wed, 29 Apr 2020 10:11:59 +0000</pubDate>
      <link>https://dev.to/gruberb/experiences-of-working-fully-remote-461c</link>
      <guid>https://dev.to/gruberb/experiences-of-working-fully-remote-461c</guid>
      <description>&lt;p&gt;&lt;em&gt;Info: This article was written in December 2019, before the pandemic hit the world.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;My personal biggest factor being a software developer is to be able to work at the cutting edge of technology + industries and generally think about how life should look like in 10 or 20 years from now.&lt;/p&gt;

&lt;p&gt;One of the biggest aspects, at least in my opinion, will be how we see companies, jobs and our current system. I am meeting more people who don't want to join a big brand (lets say Amazon or Google) if they have currently a better lifestyle/money ratio. &lt;/p&gt;

&lt;p&gt;How I see it is, that we are fortunate enough to earn a decent living while being able to dictate (to some degree) how we want to earn it. This might not be forever, or it might be the future, we will see.&lt;/p&gt;

&lt;p&gt;So, being a freelancer for a bunch of years now, I wanted to take the next step in my "dream working life" and trying remote work. There are actually not that many projects or companies who offer a complete remote setup. I was lucky enough to have found one, and I was working remotely in a complete remote company for more than 6 months now. Here are my experiences.&lt;/p&gt;

&lt;h3&gt;
  
  
  The "Get dressed and showered" myth
&lt;/h3&gt;

&lt;p&gt;Many people told me to get dressed, walk around the block, have a routine to start the day. For me personally, this didn't hold true (maybe just yet?). Having a small daughter at home, the nights can be up and downs. I enjoy the most to hold her in the morning until my planned start of the day, without having any delay in between. So sometimes, I don't get showered or properly dressed until the evening. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If I feel a mental road block, I use the showering and eating time to unblock myself&lt;/li&gt;
&lt;li&gt;I can work from 6am on until noon, go for a run, and shower and eat then&lt;/li&gt;
&lt;li&gt;If I had a rough night, I need a shower and proper breakfast to start the day&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Maybe I am still new to the game, but my days vary a lot and how to get started and inspired. Sometimes I work on super interesting tasks where a spark comes up in the morning and I don't want to waste time with other things. That's the beauty of having your work environment within a 1 minute reach.&lt;/p&gt;

&lt;h3&gt;
  
  
  I love text meetings
&lt;/h3&gt;

&lt;p&gt;The biggest factor of working remotely is to ask people via a team communication tool (whatever this might be). I work with people in different time zones so to be able to just ask a question, and have an answer when I wake up is sometimes a perfect start to the day.&lt;/p&gt;

&lt;p&gt;I can always scroll back days, weeks and even months to rehash a discussion. I was sick lately for a week and had to dive back into a complex tasks. Instead of having the office noise around me, I could take a coffee and re-read my conversation with one of my colleagues a week ago to get back into the mindset.&lt;/p&gt;

&lt;h3&gt;
  
  
  It's hard to unlearn "just being there"
&lt;/h3&gt;

&lt;p&gt;On some days, nothing really flows. When you are in an office and get paid for sitting in chair no matter what, you just wait a few hours, try to get into the zone. But sometimes, you have meeting interruptions etc. So everyone knows: Nobody is 100% productive for 21 days a month. And, as a software developer and depending on personal profile, you can make up for 1 or 2 days of not-productive work in a few hours.&lt;/p&gt;

&lt;p&gt;At home, that's different. When I hit a road block, I have to write down why I am blocked and make an active choice about it. Do I take a break, go for a run and try to solve the problem later ones it could sit a bit with me? Or do I have to contact colleagues to unblock me?&lt;/p&gt;

&lt;p&gt;At the office, a colleague will say something or you chat in the coffee break and this sometimes help your mental flow. Not at home. Nobody is here to help you out. This is one of the reasons why companies might just want to hire more Senior Developers, because you have to tell from experience which type of block you are facing and take ownership of it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Feeling the vibe of the team
&lt;/h3&gt;

&lt;p&gt;It is really hard to see where the team is at. Like you can have a few bad days and really feel you are letting the team down? Are other people also having slower days or weeks? In an office, you see the mood of the team. Just before christmas when people are more talking about the christmas events then actually being productive all the time. It's part of the social office experience.&lt;/p&gt;

&lt;p&gt;But at home, every day is the same. Even if it's not, you can't really tell what other people are going through. Reaching out, just checking in on people with a big disclaimer that they don't need to answer right away is a big help.&lt;/p&gt;

&lt;h3&gt;
  
  
  Block your most productive hours
&lt;/h3&gt;

&lt;p&gt;That one is a biggie. If you don't have a family, you literally can set your own working times. Lets say you work best from 5am to noon? That's alredy 7 hours of work. Nobody distracts you when you sent your team communication tool on silent and close your E-Mail application. &lt;/p&gt;

&lt;p&gt;Need to get some serious work done? Tell the team that for the next few days you are just available for emergencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  You have to motivate yourself
&lt;/h3&gt;

&lt;p&gt;You have to know yourself quite a lot. What gets you out of bed? What are the signs that you don't want to get out of bed? Are you in a rut, depressed etc? Untangling your emotional state, finding this intrinsic motivation can be a challenge sometimes. &lt;/p&gt;

&lt;h3&gt;
  
  
  Isolation
&lt;/h3&gt;

&lt;p&gt;Many people I met still prefer going to the office. It creates routine, structure and social contact. All of this has to be done manually from you if you work remotely. Meeting friends, setting working hours for yourself, actually getting work done. It's sometimes not easy, especially if you have to follow up on defining work etc. &lt;/p&gt;

&lt;p&gt;It can be lonely, and a week can pass by without you having realized you actually didn't meet anyone. So making sure to join movie or football groups, meeting up with friends, leaving the house at least once day etc. can help to keep you going.&lt;/p&gt;

&lt;h3&gt;
  
  
  You can't work remote in a semi-remote company
&lt;/h3&gt;

&lt;p&gt;I worked "remotely" at my clients projects here and there, but this doesn't work. Why?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The team and company still have the "office chair == being paid" mentality. If they don't see you, they think they pay for something they don't get&lt;/li&gt;
&lt;li&gt;You miss out on coffee break chatter and office mentality. You will be considered a bit of an outsider if nobody else is doing it&lt;/li&gt;
&lt;li&gt;Meetings just don't work. People will chit-chat in between and you never know if this was important know or not (also sound and video quality!!)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;There is a lot of talk around work, remote work and office culture in the last few years. We have to accept that not everyone sees work as their primary calling or target in their life. It's hard to do pure remote work if you are not motivated and need a combination of sort-of-meaningful work and social environment to function. Right now, people who work or want to work remotely have the following characteristics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't need social games or hierarchies to function&lt;/li&gt;
&lt;li&gt;Really love what they do, but need more flexibility to function properly&lt;/li&gt;
&lt;li&gt;Don't agree with the open office culture &lt;/li&gt;
&lt;li&gt;Don't see value in playing corporate games&lt;/li&gt;
&lt;li&gt;Have a family or want to travel more&lt;/li&gt;
&lt;li&gt;Don't want to move to big city to do interesting work &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What you have to remember is: People might need a network (co-working spaces, MeetUp groups etc.) to do this type of work. Rent prices are increasing and not everyone has a spare room at home to build an office in. &lt;/p&gt;

&lt;p&gt;Companies right now are still going to major tech hubs (San Francisco, Toronto, New York, Berlin and London in my experience) to build their company and hire talent. These cities however are getting so expensive that new talent just can't afford to move there. &lt;/p&gt;

&lt;p&gt;Not to miss out on talent might be the biggest driver for companies to switch to a remote-first company. For everything else, time will tell. We are still social people and need connections. Work doesn't have the status for newer generations as it had for the Baby Boomer Generation. So if you like your colleagues, have a nice office and decent pay, an office job has many advantages.&lt;/p&gt;

&lt;p&gt;If you already have a network or want to build your own one, want to join a company you can't reach in your current home town or advance your skills unavailable to location-wise, remote work might be a good fit. &lt;/p&gt;

</description>
      <category>remote</category>
      <category>productivity</category>
      <category>programming</category>
    </item>
    <item>
      <title>Rust for the Brave and True</title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Tue, 11 Jun 2019 13:12:29 +0000</pubDate>
      <link>https://dev.to/gruberb/rust-for-the-brave-and-true-4766</link>
      <guid>https://dev.to/gruberb/rust-for-the-brave-and-true-4766</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bTJvQ6Vb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jtwjm41l472ciamvqzi6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bTJvQ6Vb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jtwjm41l472ciamvqzi6.jpg" alt="thinkpad and linux"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A few years ago, I wanted to make the switch from Frontend to Backend. One day I fell down the YouTube rabbit hole and (partly) watched a few tech talks. I realised that many of the really good talks were held at Clojure conferences. I looked into it, and decided to learn Clojure. &lt;/p&gt;

&lt;p&gt;The lust for learning a new programming language often times comes from the sheer boredom of your old work, so you also want to redo part of yourself. After a bit of research, I found the book called &lt;a href="https://www.braveclojure.com/"&gt;"Clojure for the Brave and True"&lt;/a&gt;, and absolutely loved it.&lt;/p&gt;

&lt;p&gt;After a few weeks however, I gave up. The book wants you to jump all in and learn emacs at the same time as well, since it provides, according to the book, the best environment. I really liked the idea, but abandoned the project also due to the lack of Clojure positions for Juniors in my area (Berlin, Germany).&lt;/p&gt;

&lt;h1&gt;
  
  
  Rust
&lt;/h1&gt;

&lt;p&gt;A few years later, today, I started to learn Rust and got excited about the whole ecosystem, community and possibilities. I read, wrote and programmed a lot in Rust during the last few months. So much so that I got a Rust position at a company starting August (yay!).&lt;/p&gt;

&lt;h1&gt;
  
  
  Brave
&lt;/h1&gt;

&lt;p&gt;But, as I digged deeper and deeper into the language, and watched tutorials, I figured: I can't do Systems Programming on a MacBook Pro running macOS. Don't get me wrong: Once you mastered a skill, it doesn't matter where you operate it on. Also a different machine doesn't make a huge difference.&lt;/p&gt;

&lt;p&gt;Although, I might disagree a bit with that. Take loosing weight for example. Just cutting down a few kcal per day won't make a huge difference. Your fridge has to look completely different so you are less tempted to eat all the bad things and naturally will nurture your body in a more healthy way. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I remembered the "Clojure for the Brave an True" concept, and wanted to apply it to my Rust environment&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is exactly what I aimed for when I ordered a Thinkpad X1, with the intention to install Arch Linux on it. Instead of nurturing my body, I wanted to nurture my brain in the right way. If I want to learn about TCP/IP for example, I don't want to &lt;code&gt;brew install&lt;/code&gt; the right package every now and then.&lt;/p&gt;

&lt;h3&gt;
  
  
  My reasons:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;I want to spend almost all of my time on the command line&lt;/li&gt;
&lt;li&gt;I realized that even macOS is constructed as a perfect source of leisure and wasting time: I wanted a pure work machine instead&lt;/li&gt;
&lt;li&gt;My plan was to spend idle time not in front of a screen, but read a book or write&lt;/li&gt;
&lt;li&gt;I wanted less distractions from my operating system&lt;/li&gt;
&lt;li&gt;I wanted to feel closer to the machine&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  True
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0doHD3ww--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/gruberb/dotfiles/master/code.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0doHD3ww--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/gruberb/dotfiles/master/code.png" alt="vim and Rust on Arch on a Thinkpad X1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After receiving my Thinkpad, I removed Windows and installed Arch from a USB stick, &lt;a href="https://github.com/gruberb/dotfiles"&gt;following these steps&lt;/a&gt;. After 4 tries I finally succeeded. When I booted the first time, I installed &lt;a href="https://wiki.archlinux.org/index.php/I3"&gt;&lt;code&gt;i3&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://github.com/jwilm/alacritty"&gt;&lt;code&gt;alacritty&lt;/code&gt;&lt;/a&gt; as my terminal. Next to this, I installed &lt;code&gt;Firefox&lt;/code&gt; for browsing. That's it. I need a terminal, a window manager and a browser. I barely touch my mouse any more.&lt;/p&gt;

&lt;h3&gt;
  
  
  What I learned from it
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Using my browser to keep up with Slack and E-Mail, I finally reached my goal of just checking it twice a day (because it's just less convenient).&lt;/li&gt;
&lt;li&gt;Just installing Arch made me touch so many aspects of an operating system (again), and it's not all that hard to understand certain areas &lt;/li&gt;
&lt;li&gt;I can buy books like &lt;a href="https://nostarch.com/tlpi"&gt;The Linux Programming Interface&lt;/a&gt; or &lt;a href="https://nostarch.com/tcpip.htm"&gt;TCP/IP Guide&lt;/a&gt; and dig deep into the system&lt;/li&gt;
&lt;li&gt;I can read the &lt;a href="https://www.2600.com/"&gt;2600 magazine&lt;/a&gt; and follow along the tutorials&lt;/li&gt;
&lt;li&gt;It made me less distracted during work, since I literally have no notifications or other distractions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Working under macOS, I constantly opened other programs while I didn't know how to proceed on a task. With Arch, it's much harder to do so (and less fun). I have a physical notebook instead next to me and I will, if I can't proceed on a problem, note down my thoughts in the notebook and stare out of the window to think again.&lt;/p&gt;

&lt;p&gt;I am excited again to be closer to the hardware I operate on. Being able to master &lt;code&gt;nvim&lt;/code&gt; and setup a proper &lt;code&gt;tmux&lt;/code&gt; setup is so much faster then doing the same workflow inside Visual Code or other editors.&lt;/p&gt;

&lt;h1&gt;
  
  
  Rust Coding Environment
&lt;/h1&gt;

&lt;p&gt;I am using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://neovim.io/"&gt;NeoVIM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/tmux/tmux"&gt;tmux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/rust-lang/rls"&gt;Rust Langauge Server&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the following PlugIns for NeoVim:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://aur.archlinux.org/packages/neovim-airline/"&gt;neovim-airline&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/autozimu/LanguageClient-neovim"&gt;LanguageClient NeoVim&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/Shougo/deoplete.nvim"&gt;Deoplete Autocomplete&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it. You can easily setup a &lt;code&gt;tmux&lt;/code&gt; session, split the window where you have your code inside &lt;code&gt;nvim&lt;/code&gt; and on the other half of the window &lt;code&gt;cargo run/watch&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Moving through code like that makes it really hard after a while to use a normal code editor again. Allthough you can install &lt;code&gt;vim&lt;/code&gt; bindings to VS Code I guess.&lt;/p&gt;

&lt;h1&gt;
  
  
  2 Warnings/Hints
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Always have a workable Live-USB stick with Arch installed with you in case of an emergency. I tinkered with the startup script and got myself into a loop. I had to boot from the stick, decrypt and mount my hard drive and then revert the changes to be able to boot again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In addition to my Thinkpad, I operate a remote server (for around 10 Euros/month) for backups (via &lt;a href="https://wiki.archlinux.org/index.php/Rsync"&gt;&lt;code&gt;rsync&lt;/code&gt;&lt;/a&gt;) and other fun activities. It can help to have another machine for backups just in case you want to take a snapshot of finally perfect configured Arch installation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Additions
&lt;/h1&gt;

&lt;p&gt;In addition to the Thinkpad, I got a &lt;a href="https://www.amazon.com/Vertical-Mouse-Ergonomic-Optical-Wireless/dp/B07HSMS6Y2/ref=sr_1_11_sspa?keywords=vertical+mouse+csl&amp;amp;qid=1560258082&amp;amp;s=gateway&amp;amp;sr=8-11-spons&amp;amp;psc=1"&gt;vertical mouse&lt;/a&gt; and a &lt;a href="https://mechanicalkeyboards.com/shop/index.php?l=product_detail&amp;amp;p=4322"&gt;proper keyboard&lt;/a&gt;. Both of which even increase the fun of my setup. My monitor is a Dell 4k 27" which is hooked up on a monitor stand.&lt;/p&gt;

&lt;p&gt;This is so nice to work with that I even don't miss the beautiful 5k LG UltraFine at all, which I previously had.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;I provide &lt;a href="https://github.com/gruberb/dotfiles"&gt;the basic dotfiles and steps&lt;/a&gt; for my setup on my GitHub account. It's good to run contrary to certain environments. The iPad for example became so powerful that the casual user doesn't need a Notebook anymore to do every day tasks. That's a perfect time to reconsider your relationship with your tools you are using.&lt;/p&gt;

&lt;p&gt;If you follow the steps, it's really simple to operate within Arch on a Thinkpad. You need to tinker with HDPI here and there, and adjust some hibernate functionality. But overall it's what I wanted: The bare minimum to get to a comfortable coding environment, with almost no distractions. &lt;/p&gt;

&lt;p&gt;In the end, the Thinkpad also has a much better keyboard than the MacBook Pro. Also the form factor is just perfect. A bit bigger than the 13" and smaller than a 15" model.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>linux</category>
    </item>
    <item>
      <title>Explained: Futures in Rust for Web Development</title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Thu, 02 May 2019 09:44:50 +0000</pubDate>
      <link>https://dev.to/gruberb/explained-rust-futures-for-web-development-a10</link>
      <guid>https://dev.to/gruberb/explained-rust-futures-for-web-development-a10</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;As I explained &lt;a href="https://dev.to/gruberb/explained-how-does-async-work-in-rust-46f8"&gt;in one of my last articles&lt;/a&gt;, Futures are one part of an asynchrounous environment. We need Syntax, Type and a Runtime to be able to run code in an non blocking way. Futures are the type part of the Rust async story.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Content
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;The two perspectives&lt;/li&gt;
&lt;li&gt;What actually is a Future?&lt;/li&gt;
&lt;li&gt;When to use Futures?&lt;/li&gt;
&lt;li&gt;Why does using Futures save time?&lt;/li&gt;
&lt;li&gt;How to use Futures?&lt;/li&gt;
&lt;li&gt;What's so different or hard about Futures?&lt;/li&gt;
&lt;li&gt;How to execute more than one Future?&lt;/li&gt;
&lt;li&gt;What is the future of Futures?&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The two perspectives
&lt;/h2&gt;

&lt;p&gt;If you are coming from NodeJS, Futures in Rust don't make much sense. In NodeJS, everything happens asynchronously. Therefore for you to be able to say "Hey, I really need to wait for the answer for this GET HTTP call", you are putting&lt;code&gt;.then()&lt;/code&gt; on a &lt;code&gt;Promise&lt;/code&gt;, so you can make sure you just execute the code inside the &lt;code&gt;.then()&lt;/code&gt; when the HTTP call is finished. &lt;/p&gt;

&lt;p&gt;In Rust, everything is blocking and synchronous by default, so you might ask yourself: "Why bothering with the complexity, that's exactly what I wanted in the first place!"&lt;/p&gt;

&lt;p&gt;Rust is a Systems Programming Language. So for you to craft applications in Rust, you always have to wear two hats. The "Systems Hat"(⛑) and the "Programmer Hat"(🎩). The Systems Hat(⛑) lets you consider what is actually best from a machine point of view, the Programmer Hat(🎩) takes care of the syntax and how a software is written. &lt;/p&gt;

&lt;p&gt;If you are coming from NodeJS, the Systems Hat is taken care of from Googles V8 runtime, so you can focus on syntax. In Rust you get help from crates, although you need to make certain decisions yourself.&lt;/p&gt;

&lt;p&gt;The Systems Hat is the reason why we want to use Futures. Therefore, you need to handle Futures as a type in your application, and afterwards make sure that you use a runtime to actually execute them. If you are consuming Futures (for example when a crate you are using is returning a &lt;code&gt;Future&lt;/code&gt;), you have to understand where the data comes from. In this case, the Programmer Hat &lt;strong&gt;and&lt;/strong&gt; the Systems Hat are needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  What actually is a &lt;code&gt;Future&lt;/code&gt;? 🎩
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;Future&lt;/code&gt; in Rust is actually a &lt;code&gt;trait&lt;/code&gt;, which if you want to implement it, looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Future&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Error&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;poll&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Poll&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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;Item&lt;/span&gt;&lt;span class="p"&gt;,&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;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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;poll&lt;/code&gt; method is important. This will get called from your runtime and will return &lt;a href="https://docs.rs/futures/0.1/futures/enum.Async.html" rel="noopener noreferrer"&gt;&lt;code&gt;Async::Ready&lt;/code&gt; or &lt;code&gt;Async::NotReady&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to fetch different magazine issues from a remote location or from the file system, you can create and return your own &lt;code&gt;Future&lt;/code&gt; for that:&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;Magazine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;issues&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;u8&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and &lt;code&gt;impl&lt;/code&gt; the &lt;code&gt;Future&lt;/code&gt; trait on it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Future&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Magazine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// here we return a single byte&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// this method is getting called from the runtime. Everytime we can read&lt;/span&gt;
    &lt;span class="c1"&gt;// a byte into the buffer, we return `Async::Ready`&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;poll&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Poll&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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;Item&lt;/span&gt;&lt;span class="p"&gt;,&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;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="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="na"&gt;.0&lt;/span&gt;&lt;span class="nf"&gt;.poll_read&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="n"&gt;buf&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="nn"&gt;Async&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Ready&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_num_bytes_read&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;Async&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Ready&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&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="nn"&gt;Async&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NotReady&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;Async&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NotReady&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The runtime will fill the buffer with the information you are asking for, and the &lt;code&gt;Future&lt;/code&gt; will return &lt;code&gt;Async::Ready&lt;/code&gt; once the buffer is full. &lt;/p&gt;

&lt;p&gt;This is the basic essence of a &lt;code&gt;Future&lt;/code&gt; in Rust.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use Futures? 🎩
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Implementing the &lt;code&gt;Future&lt;/code&gt; trait for a type is your way of telling the machine "Hey, this might take a while, consider this when executing this part of the code". &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Rust is a synchronous language. Everything happens line by line, and waits until each result is processed. This is not a problem until you want to do tasks which take longer time to process. &lt;/p&gt;

&lt;p&gt;If you create a micro service with Rust, and you need to fetch three other micro services for some data, and afterwards you merge the data and write it into a database, that's a use case where you want to consider using Futures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does using Futures save time? ⛑
&lt;/h2&gt;

&lt;p&gt;In our test scenario, each of these HTTP calls could take, lets say, 1 Minute. Instead of now waiting 3 Minutes for all of the calls to finish, you want to run them in parallel so you still just wait 1 Minute instead of 3.&lt;/p&gt;

&lt;p&gt;Therefore you create three methods which return a &lt;code&gt;Future&lt;/code&gt; each, collect them, and then pass them on to a runtime (to tokio via &lt;code&gt;tokio::run&lt;/code&gt; for example).&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use Futures? 🎩 + ⛑
&lt;/h2&gt;

&lt;p&gt;If a crate returns a &lt;code&gt;Future&lt;/code&gt;, you can use &lt;a href="https://docs.rs/futures/0.1.26/futures/future/struct.AndThen.html" rel="noopener noreferrer"&gt;&lt;code&gt;.and_then()&lt;/code&gt;&lt;/a&gt; to work with the result once the &lt;code&gt;Future&lt;/code&gt; is ready. Using &lt;a href="https://docs.rs/futures/0.1.26/futures/future/struct.Map.html" rel="noopener noreferrer"&gt;&lt;code&gt;.map()&lt;/code&gt;&lt;/a&gt; on a &lt;code&gt;Future&lt;/code&gt; lets you alter the type of the &lt;code&gt;Future&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Inside the async Rust world, we have to take care of different types of data. Instead of using Strings and Numbers for example, we deal with streams of values. You will most likely deal with Streams.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Stream&lt;/code&gt; is an extension of a Future. Where a Future is about producing a single value, a &lt;code&gt;Stream&lt;/code&gt; is yielding values as long as they are present. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://docs.rs/futures/0.1.26/futures/stream/index.html" rel="noopener noreferrer"&gt;Stream&lt;/a&gt;: A &lt;code&gt;Stream&lt;/code&gt; is, like a &lt;code&gt;Future&lt;/code&gt;, a trait you can &lt;code&gt;impl&lt;/code&gt; on a type. This lets you iterate over the return values (which are &lt;code&gt;Some(_)&lt;/code&gt; or &lt;code&gt;None&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.rs/futures/0.1.26/futures/sink/index.html" rel="noopener noreferrer"&gt;Sink&lt;/a&gt;: For writing data continuously (in an asynchronous way) to either a socket or to a file system&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So Streams are for reading data, Sinks are for writing them. We have two types of Streams in our web ecosystem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stream of Bytes (like HTTP bodies or TCP streams)&lt;/li&gt;
&lt;li&gt;Stream of Messages (like WebSocket Frames or UDP Packets), where each message has a fixed size&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code example
&lt;/h3&gt;

&lt;p&gt;Lets look at a use case where a crate you are using returns a &lt;code&gt;Future&lt;/code&gt;. When doing HTTP calls, &lt;code&gt;reqwest&lt;/code&gt; is a good example. On the &lt;code&gt;Future&lt;/code&gt; returned from &lt;code&gt;reqwest&lt;/code&gt;, you can use &lt;code&gt;.and_then()&lt;/code&gt; to process the results (🎩):&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="c1"&gt;// We have to use "r#" before "async" because "async" is a reserved keyword.&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;reqwest&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="k"&gt;async&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="c1"&gt;// The return type has to be `Future&amp;lt;Item=(), Error=()&amp;gt;` to be able&lt;/span&gt;
&lt;span class="c1"&gt;// to use `tokio::run`. &lt;/span&gt;
&lt;span class="c1"&gt;// If it has a different type, you have to use `tokio::block_on`&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;fetch_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;=&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;=&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="nn"&gt;Client&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;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.send&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.and_then&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="nf"&gt;.into_body&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.concat2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;err&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;"request error: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;))&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;body&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// here you can use the body to write it to a file&lt;/span&gt;
            &lt;span class="c1"&gt;// or return it via Ok()&lt;/span&gt;
            &lt;span class="c1"&gt;// If you return it via for example Ok(users)&lt;/span&gt;
            &lt;span class="c1"&gt;// then you need to adjust the return type in impl Future&amp;lt;Item=TYPE&lt;/span&gt;
            &lt;span class="c1"&gt;// Examples can be found here: &lt;/span&gt;
            &lt;span class="c1"&gt;// https://github.com/gruberb/futures_playground&lt;/span&gt;

            &lt;span class="c1"&gt;// For now, lets just turn the body into a Vector&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="nf"&gt;.to_vec&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;Once you created the method which returns a &lt;code&gt;Future&lt;/code&gt; (&lt;code&gt;fetch_data()&lt;/code&gt;), &lt;strong&gt;you have to pass it on to a runtime&lt;/strong&gt; like tokio (⛑):&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="nn"&gt;tokio&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;fetch_data&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  High Level Overview
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;You receive a &lt;code&gt;Future&lt;/code&gt; from an external crate&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;Future&lt;/code&gt; is likely to return a &lt;code&gt;Stream&lt;/code&gt; of values, so you have to form this &lt;code&gt;Stream&lt;/code&gt; into a type you can work with in a synchronous way (like a Vector or String)&lt;/li&gt;
&lt;li&gt;You return the whole Future from a method via &lt;code&gt;-&amp;gt; impl Future&amp;lt;Item=(), Error=()&amp;gt;&lt;/code&gt;, where the braces &lt;code&gt;()&lt;/code&gt; are placeholders for the actual type you want to return&lt;/li&gt;
&lt;li&gt;You pass the method on to a runtime like tokio via &lt;code&gt;tokio::run(method())&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;run&lt;/code&gt;​ call will start the runtime, set up the required resources and then put this future on a threadpool and start polling your future&lt;/li&gt;
&lt;li&gt;It will then try to pass the work on to the operating system&lt;/li&gt;
&lt;li&gt;Each time the runtime polls your &lt;code&gt;Future&lt;/code&gt;, if the underlying I/O resource your &lt;code&gt;Future&lt;/code&gt; is waiting on is not ready, it will return &lt;code&gt;NotReady​&lt;/code&gt;. The runtime sees this &lt;code&gt;NotReady​&lt;/code&gt; return value and puts your &lt;code&gt;Future&lt;/code&gt; to sleep&lt;/li&gt;
&lt;li&gt;Once an event from the underlying I/O resource comes, the runtime checks if this I/O resource is associated with your &lt;code&gt;Future&lt;/code&gt;, and starts polling again. This time, your &lt;code&gt;Future&lt;/code&gt; will be able to return a &lt;code&gt;Ready&lt;/code&gt;​ with a value, since the underlying I/O resource has provided a value&lt;/li&gt;
&lt;li&gt;The runtime will then set the status of the &lt;code&gt;Future&lt;/code&gt; to ready and will process the &lt;code&gt;.and_then()&lt;/code&gt; part of the code&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;code&gt;Future&lt;/code&gt; is getting, unlike in NodeJS, executed via &lt;code&gt;tokio::run&lt;/code&gt; and not before. In Node, as soon as you write a &lt;code&gt;Promise&lt;/code&gt;, the object is being returned immediately.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's so different or hard about Futures? ⛑
&lt;/h2&gt;

&lt;p&gt;Lets walk through our example above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We create a new client with &lt;code&gt;Client::new()&lt;/code&gt; and &lt;code&gt;.send()&lt;/code&gt; our request&lt;/li&gt;
&lt;li&gt;We will get a &lt;a href="https://github.com/seanmonstar/reqwest/blob/master/src/async_impl/response.rs#L24" rel="noopener noreferrer"&gt;&lt;code&gt;Response&lt;/code&gt;&lt;/a&gt; back:
&lt;/li&gt;
&lt;/ul&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;Response&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;HeaderMap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;url&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="n"&gt;Url&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;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Decoder&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The body itself is a Decoder, which can be turned into a &lt;code&gt;Body&lt;/code&gt; via &lt;a href="https://github.com/seanmonstar/reqwest/blob/master/src/async_impl/response.rs#L113" rel="noopener noreferrer"&gt;&lt;code&gt;.into_body()&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Body&lt;/code&gt; itself &lt;a href="https://github.com/seanmonstar/reqwest/blob/master/src/async_impl/decoder.rs#L155" rel="noopener noreferrer"&gt;implements&lt;/a&gt; a &lt;a href="https://docs.rs/futures/0.1.26/futures/stream/trait.Stream.html" rel="noopener noreferrer"&gt;Stream&lt;/a&gt; (as mentioned earlier).&lt;/li&gt;
&lt;li&gt;Now we can look into the Futures API from Rust and find out: We can turn a Stream of Bytes into single item via &lt;a href="https://docs.rs/futures/0.1.26/futures/stream/struct.Concat2.html" rel="noopener noreferrer"&gt;&lt;code&gt;.concat2()&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&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="nf"&gt;.and_then&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="nf"&gt;.into_body&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.concat2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We use this single item in the &lt;code&gt;.map()&lt;/code&gt; part as &lt;code&gt;body&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;With the help of your code editor, you will find out that this &lt;code&gt;body&lt;/code&gt; is actually a &lt;a href="https://github.com/hyperium/hyper/blob/master/src/body/chunk.rs" rel="noopener noreferrer"&gt;&lt;code&gt;Chunk&lt;/code&gt;&lt;/a&gt;, returned from the library &lt;a href="https://github.com/hyperium/hyper" rel="noopener noreferrer"&gt;&lt;code&gt;Hyper&lt;/code&gt;&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;We can turn this &lt;code&gt;Chunk&lt;/code&gt; now into a &lt;a href="https://doc.rust-lang.org/std/vec/struct.Vec.html" rel="noopener noreferrer"&gt;&lt;code&gt;Vector&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&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="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;body&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;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="nf"&gt;.to_vec&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="c1"&gt;// do whatever with v&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From then on, we are back in "normal" Rust land and can forget what just happened 🙃. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can find the full example in this &lt;a href="https://github.com/gruberb/futures_playground" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;. There I receive a &lt;code&gt;JSON&lt;/code&gt; and write it to a file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is one of the reasons why dealing with Futures is so hard in the beginning. You have to think much lower level than for example in NodeJS. In addition, the &lt;code&gt;async/await&lt;/code&gt; syntax &lt;a href="https://internals.rust-lang.org/t/await-syntax-discussion-summary/9914" rel="noopener noreferrer"&gt;is not final yet&lt;/a&gt;, which leads to much more boiler code.&lt;/p&gt;

&lt;p&gt;These mental steps help you to not get lost when dealing with Futures:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What's the return type or value I am getting from this library?&lt;/li&gt;
&lt;li&gt;How can I access the &lt;code&gt;Stream&lt;/code&gt; of values on this response?&lt;/li&gt;
&lt;li&gt;What will the library turn this &lt;code&gt;Stream&lt;/code&gt; into when I collect all the values via &lt;code&gt;.concat2()&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;How can I turn this new type into a Vector or another Rust std format so I can pass it on to synchronous methods?&lt;/li&gt;
&lt;/ol&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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Ffutures_01.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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Ffutures_01.png" alt="futures_how_they_work"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basically, you always want to figure out how to access the stream of values, collect them, and then process the resulting object.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to execute more than one &lt;code&gt;Future&lt;/code&gt;? 🎩
&lt;/h2&gt;

&lt;p&gt;Generally you want to collect your values as &lt;code&gt;Streams&lt;/code&gt;, so that for each item you get over a &lt;code&gt;Stream&lt;/code&gt;, you can spawn off a new &lt;code&gt;Future&lt;/code&gt; to handle it.&lt;/p&gt;

&lt;p&gt;The Rust Futures API has a method called &lt;a href="https://docs.rs/futures/0.1.26/futures/stream/futures_unordered/struct.FuturesUnordered.html" rel="noopener noreferrer"&gt;&lt;code&gt;FuturesUnordered&lt;/code&gt;&lt;/a&gt; which you can use to add more than one &lt;code&gt;Future&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="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="nn"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;futures_unordered&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FuturesUnordered&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;hyper&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="nn"&gt;client&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ResponseFuture&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="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;setup_requests&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;FuturesUnordered&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ResponseFuture&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;list_of_futures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;FuturesUnordered&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="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;new&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;first&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;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;list_of_futures&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;first&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;second&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;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;list_of_futures&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;second&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;list_of_futures&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this example we used &lt;a href="https://github.com/hyperium/hyper" rel="noopener noreferrer"&gt;&lt;code&gt;hyper&lt;/code&gt;&lt;/a&gt; for our HTTP calls. You can find the rest of the code &lt;a href="https://github.com/chicagohaskell/async-futures-talk/blob/master/rustlb/examples/multi-http/src/main.rs" rel="noopener noreferrer"&gt;over here on Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The syntax will look slightly different if you are using &lt;code&gt;reqwest&lt;/code&gt;. Here you &lt;code&gt;.join()&lt;/code&gt; multiple requests and return it as "one Future".&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;fetch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;=&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;=&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="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;new&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;json&lt;/span&gt; &lt;span class="o"&gt;=&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;res&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="py"&gt;.json&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;STRUCT_TYPE&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;let&lt;/span&gt; &lt;span class="n"&gt;request1&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;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;.send&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;.and_then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&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;request2&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;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;.send&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;.and_then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;request1&lt;/span&gt;&lt;span class="nf"&gt;.join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request2&lt;/span&gt;&lt;span class="p"&gt;)&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;res1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res2&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res1&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;err&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;"stdout error: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&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 full code can also be found on &lt;a href="https://github.com/seanmonstar/reqwest/blob/master/examples/async_multiple_requests.rs" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the future of Futures? 🎩 + ⛑
&lt;/h2&gt;

&lt;p&gt;Futures are getting into stable Rust at 1.37, so around June. There are also changes around the syntax and runtime, which will benefit the amount of code you have to write to get this stream of values out of a &lt;code&gt;Future&lt;/code&gt; and into a synchronous Rust format. &lt;/p&gt;

&lt;p&gt;You can also use the &lt;a href="https://github.com/rustasync/runtime" rel="noopener noreferrer"&gt;&lt;code&gt;Runtime&lt;/code&gt;&lt;/a&gt; crate, which saves you almost all of the boilerplate code. Although going through the process above helps you understand Futures on a deeper level. &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;If you perform asynchronous operations, like fetching files from the operating system or making HTTP requests to a remote server, then Futures let you work with the return values in a non-blocking way.&lt;/p&gt;

&lt;p&gt;If it would be synchronous, you would have to block a thread the operation is running on, and wait for the result until you continue. To do this in an asynchronous way, we have a runtime which creates threads themselves, and takes on Futures. It will fill the value inside the &lt;code&gt;Future&lt;/code&gt; when the operating system returns a value to the runtime.&lt;/p&gt;

&lt;p&gt;Once the &lt;code&gt;Future&lt;/code&gt; is fulfilled, the runtime sets &lt;code&gt;Async::Ready&lt;/code&gt; and the &lt;code&gt;.and_then()&lt;/code&gt; part of the code will get executed. &lt;/p&gt;

</description>
      <category>rust</category>
      <category>async</category>
    </item>
    <item>
      <title>Web Development with Rust — 03/x: Create a REST API</title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Mon, 15 Apr 2019 08:52:41 +0000</pubDate>
      <link>https://dev.to/gruberb/web-development-with-rust-03-x-create-a-rest-api-3i82</link>
      <guid>https://dev.to/gruberb/web-development-with-rust-03-x-create-a-rest-api-3i82</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Follow me on &lt;a href="https://twitter.com/byteadventures" rel="noopener noreferrer"&gt;twitter&lt;/a&gt; to always get the latest information about web development in Rust. Also checkout the &lt;a href="https://github.com/gruberb/web-programming-in-rust" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt; to this series.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Content
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;HTTP Requests&lt;/li&gt;
&lt;li&gt;POST/PUT/PATCH/DELETE are special&lt;/li&gt;
&lt;li&gt;The Job of a Framework&lt;/li&gt;
&lt;li&gt;Creating an API spec&lt;/li&gt;
&lt;li&gt;Crafting the API&lt;/li&gt;
&lt;li&gt;Input Validation&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;APIs are the bread and butter of how a modern and fast-paced web environment. Frontend application, other web services and IoT devices need to be able to talk to your service. API endpoints are like doors to which you decide what comes in and in which format.&lt;/p&gt;

&lt;p&gt;Since Rust is a static typed language with a strong compiler you won't face many of the common pitfalls about running a web service in production. Although there are still run time errors which you have to cover.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Requests
&lt;/h2&gt;

&lt;p&gt;When we talk about creating an API we basically mean a web application which listens on certain paths and responds accordingly. But first things first. For two devices to be able to communicate with each other there has to be an established TCP connection. &lt;/p&gt;

&lt;p&gt;TCP is a protocol which the two parties can use to establish a connection. After establishing this connection, you can receive and send messages to the other party. HTTP is another protocol, which is built on top of TCP, and it's defining the contents of the requests and responses.&lt;/p&gt;

&lt;p&gt;So on the Rust side of things, TCP is implemented in the Rust core library, HTTP is not. Whatever framework you chose in the &lt;a href="https://dev.to/gruberb/web-programming-in-rust-02x-deploy-your-first-app-1k05"&gt;previous article&lt;/a&gt; they all implement HTTP and therefore are able to receive and send HTTP formatted messages.&lt;/p&gt;

&lt;p&gt;An example GET requests for example looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;GET / HTTP/1.1
Host: api.awesomerustwebapp.com
Accept-Language: en
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET&lt;/code&gt;: the HTTP method&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/&lt;/code&gt;: The path &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HTTP/1.1&lt;/code&gt;: The version of the HTTP protocol&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HOST&lt;/code&gt;: The host/domain of the server we want to request data from&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Accept-Language&lt;/code&gt;: Which language we prefer and understand&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most common used HTTP methods are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GET&lt;/li&gt;
&lt;li&gt;POST&lt;/li&gt;
&lt;li&gt;PUT&lt;/li&gt;
&lt;li&gt;PATCH&lt;/li&gt;
&lt;li&gt;DELETE&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  POST/PUT/PATCH/DELETE are special
&lt;/h2&gt;

&lt;p&gt;We are using &lt;code&gt;GET&lt;/code&gt; every time we browse the web. If we want to alter data however (like using &lt;code&gt;POST&lt;/code&gt; to send data over to another server), we need to be more cautions and precise.&lt;/p&gt;

&lt;p&gt;First, not everyone is allowed to just send a bunch of data to another server. Our API can for example say: "I just accept data from the server with the host name &lt;code&gt;allowed.awesomerustapp.com&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Therefore, when you send a &lt;code&gt;POST&lt;/code&gt; to another server, what actually happens is the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" rel="noopener noreferrer"&gt;CORS workflow&lt;/a&gt;:&lt;/p&gt;

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

&lt;p&gt;We first ask the server what is allowed, where do you accept requests from and what are your accepted headers. If we fulfill all of these requirements, then we can send a &lt;code&gt;POST&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The web framework &lt;a href="https://actix.rs/" rel="noopener noreferrer"&gt;actix&lt;/a&gt; for example has its own &lt;a href="https://actix.rs/actix-web/actix_web/middleware/cors/index.html" rel="noopener noreferrer"&gt;cors middleware&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Disclaimer: Not all frameworks (like &lt;a href="https://github.com/SergioBenitez/Rocket/issues/25" rel="noopener noreferrer"&gt;rocket&lt;/a&gt; and &lt;a href="https://github.com/rustasync/tide/pull/104" rel="noopener noreferrer"&gt;tide&lt;/a&gt;) are implementing CORS in their core. However, in a professional environment, you handle CORS on the &lt;a href="https://imti.co/kubernetes-ingress-nginx-cors/" rel="noopener noreferrer"&gt;DevOps side&lt;/a&gt; of things and put it for example in your NGINX config.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Job of a Framework
&lt;/h2&gt;

&lt;p&gt;We use the hard work of other people to create web applications. Everything has to be implemented at some point, just not from you for most of the time. A framework covers the following concerns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start a web server and open a PORT&lt;/li&gt;
&lt;li&gt;Listen to requests on this PORT&lt;/li&gt;
&lt;li&gt;If a request comes in, look at the Path in the HTTP header &lt;/li&gt;
&lt;li&gt;Route the request to the &lt;code&gt;handler&lt;/code&gt; according to the Path&lt;/li&gt;
&lt;li&gt;Help you extract the information out of the request&lt;/li&gt;
&lt;li&gt;Pack the generated &lt;code&gt;data&lt;/code&gt; and &lt;code&gt;HTTP StatusCode&lt;/code&gt; (created from you) and form a &lt;code&gt;response&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Send the &lt;code&gt;response&lt;/code&gt; back to the sender&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Rust web framework &lt;a href="https://github.com/rustasync/tide" rel="noopener noreferrer"&gt;tide&lt;/a&gt; includes &lt;a href="https://github.com/rustasync/http-service" rel="noopener noreferrer"&gt;http-service&lt;/a&gt;, which provides the basic abstractions you need when working with HTTP calls. The crate http-service is built on top of &lt;a href="https://github.com/hyperium/hyper" rel="noopener noreferrer"&gt;hyper&lt;/a&gt;, which transforms TCP-Streams to valid HTTP requests and responses. &lt;/p&gt;

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

&lt;p&gt;Your job is to create &lt;code&gt;routes&lt;/code&gt; like &lt;code&gt;/users/:id&lt;/code&gt; and add a &lt;code&gt;route_handler&lt;/code&gt; which is a function to handle the requests on this particular path. The framework makes sure that it directs the incoming HTTP requests to this particular handler.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating an API spec
&lt;/h2&gt;

&lt;p&gt;You have to define your resources first to get an idea what your application needs to handle and uncover relationships between them. So if you want to build a idea-up-voting site, you would have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users&lt;/li&gt;
&lt;li&gt;Ideas&lt;/li&gt;
&lt;li&gt;Votes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A simple spec for this scenario would look like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users

&lt;ul&gt;
&lt;li&gt;POST &lt;code&gt;/users&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;GET &lt;code&gt;/users&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;PUT &lt;code&gt;/users/:user_id&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;PATCH &lt;code&gt;/users/:user_id&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;DELETE &lt;code&gt;/users/:user_id&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;GET &lt;code&gt;/users/:user_id&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Ideas and Votes behave accordingly. A spec is helpful for two reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It gives you guidelines not to forget a path&lt;/li&gt;
&lt;li&gt;It helps to communicate to your API users what to expect&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can tools like &lt;a href="https://swagger.io/" rel="noopener noreferrer"&gt;swagger&lt;/a&gt; to write a full spec which also describes the structure of the data and the messages/responses for each path and route.&lt;/p&gt;

&lt;p&gt;A more professional spec would include the return values for each route and the request and response bodies. However, the spec can be finalized once you know how your API should look like and behave. To get started, a simple list is enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  Crafting the API
&lt;/h2&gt;

&lt;p&gt;Depending on the framework you are using, your implementation will look different. You have to have the following features on your radar to look out for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating routes for each method (like &lt;code&gt;app.at("/users").post(post_users_handler)&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Extracting information from the request (like headers, uri-params and JSON from the request body)&lt;/li&gt;
&lt;li&gt;Creating responses with proper HTTP codes (&lt;code&gt;200&lt;/code&gt;, &lt;code&gt;201&lt;/code&gt;, &lt;code&gt;400&lt;/code&gt;, &lt;code&gt;404&lt;/code&gt; etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I am using the latest version of &lt;a href="https://github.com/rustasync/tide" rel="noopener noreferrer"&gt;tide&lt;/a&gt; for this web series. You can add it in your &lt;strong&gt;Cargo.toml&lt;/strong&gt; file and use it for your web app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[dependencies]
tide = "0.1.0"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our first &lt;code&gt;User&lt;/code&gt; implementation will look like this:&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;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;handle_get_users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Database&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;EndpointResult&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="nn"&gt;response&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="nf"&gt;.app_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.get_all&lt;/span&gt;&lt;span class="p"&gt;()))&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;fn&lt;/span&gt; &lt;span class="nf"&gt;handle_get_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Database&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;EndpointResult&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;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="nf"&gt;.param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.client_err&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;if&lt;/span&gt; &lt;span class="k"&gt;let&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;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="nf"&gt;.app_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&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="nn"&gt;response&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NOT_FOUND&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="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;handle_update_user&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;cx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Database&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;EndpointResult&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="n"&gt;user&lt;/span&gt; &lt;span class="o"&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;cx&lt;/span&gt;&lt;span class="nf"&gt;.body_json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="nf"&gt;.client_err&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;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="nf"&gt;.param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.client_err&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;if&lt;/span&gt; &lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="nf"&gt;.app_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NOT_FOUND&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="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;handle_create_user&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;cx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Database&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;EndpointResult&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&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;cx&lt;/span&gt;&lt;span class="nf"&gt;.body_json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="nf"&gt;.client_err&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="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="nf"&gt;.app_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.to_string&lt;/span&gt;&lt;span class="p"&gt;())&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;fn&lt;/span&gt; &lt;span class="nf"&gt;handle_delete_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Database&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;EndpointResult&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="nf"&gt;.param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.client_err&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="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="nf"&gt;.app_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.to_string&lt;/span&gt;&lt;span class="p"&gt;())&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="c1"&gt;// We create a new application with a basic, local database&lt;/span&gt;
    &lt;span class="c1"&gt;// You can use your own implementation, or none: App::new(())&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;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;App&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;Database&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="n"&gt;app&lt;/span&gt;&lt;span class="nf"&gt;.at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handle_create_user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handle_get_users&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="nf"&gt;.at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users/:id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handle_get_user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handle_update_user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handle_delete_user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="nf"&gt;.serve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"127.0.0.1:8000"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;You can find the full implementation of the code in the &lt;a href="https://github.com/gruberb/web-programming-in-rust/tree/master/03-x" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt; to this series.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We see that we first have to create a new App&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;App&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;add routes&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="n"&gt;app&lt;/span&gt;&lt;span class="nf"&gt;.at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and for each route add the HTTP requests we want to handle&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="n"&gt;app&lt;/span&gt;&lt;span class="nf"&gt;.at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handle_get_users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each framework has a different method of extracting parameters and JSON bodies. Actix is using &lt;a href="https://actix.rs/docs/extractors/" rel="noopener noreferrer"&gt;Extractors&lt;/a&gt;, rocket is using &lt;a href="https://api.rocket.rs/v0.4/rocket/request/trait.FromQuery.html" rel="noopener noreferrer"&gt;Query Guards&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With tide, you can access request parameters and bodies and database connections through &lt;code&gt;Context&lt;/code&gt;. So when we want to update a &lt;code&gt;User&lt;/code&gt; with a specific &lt;code&gt;id&lt;/code&gt;, we send a &lt;code&gt;PATCH&lt;/code&gt; to &lt;code&gt;/users/:id&lt;/code&gt;. From there, we call the &lt;code&gt;handle_update_user&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;Inside this method, we can access the &lt;code&gt;id&lt;/code&gt; from the URI like this:&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;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cx&lt;/span&gt;&lt;span class="nf"&gt;.param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.client_err&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each framework is also handling its own way of sending responses back to the sender. Tide is using &lt;code&gt;EndpointResult&lt;/code&gt;, rocket is using &lt;a href="https://api.rocket.rs/v0.4/rocket/struct.Response.html" rel="noopener noreferrer"&gt;Response&lt;/a&gt; and actix &lt;a href="https://actix.rs/docs/response/" rel="noopener noreferrer"&gt;HttpResponse&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Everything else is completely up to you. The framework might help you with session management and authentication, but you can also implement this yourself. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My suggestion is: Build the first skeleton of your app with the framework of your choice, figure out how to extract information out of requests and how to form responses. Once this is working, you can use your Rust skills to build small or big applications as you wish.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Input Validation
&lt;/h2&gt;

&lt;p&gt;Your best friend in the Rust world will be &lt;a href="https://github.com/serde-rs/serde" rel="noopener noreferrer"&gt;serde&lt;/a&gt;. It will help you parse JSON and other formats, but will also allow you to serialize your data. &lt;/p&gt;

&lt;p&gt;When we talk about input validation, we want to make sure the data we are getting has the right format. Lets say we are extracting the JSON body out of a request:&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;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;serde_json&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_str&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;request_body&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are using &lt;a href="https://docs.serde.rs/serde_json/" rel="noopener noreferrer"&gt;serde_json&lt;/a&gt; here to transform a JSON-String into a Struct of our choice. So if we created this struct:&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;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&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;we want to make sure the sender is including &lt;code&gt;name&lt;/code&gt; &lt;strong&gt;and&lt;/strong&gt; &lt;code&gt;height&lt;/code&gt;. If we just do &lt;code&gt;serde_json::from_str&lt;/code&gt;, and the sender forgot to pass on the height, the app will panic and shut down, since we expect the response to be a user: &lt;code&gt;let user: User&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We can improve the error handling like this:&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;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="nn"&gt;serde_json&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_str&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;request_body&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="n"&gt;user&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;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handle_error_case&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&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;We catch the error and call our &lt;code&gt;handle_error_case&lt;/code&gt; method to handle it gracefully. &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Pick a framework of your choice 

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://rocket.rs/" rel="noopener noreferrer"&gt;rocket&lt;/a&gt; is nightly &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://actix.rs/" rel="noopener noreferrer"&gt;actix&lt;/a&gt; is stable&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/rustasync/tide" rel="noopener noreferrer"&gt;tide&lt;/a&gt; is fostered close to the Rust Core and also works on Rust nightly&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Know that there is no common CORS handling (yet). Recommendation is to handle this on the DevOps side (NGINX for example)&lt;/li&gt;
&lt;li&gt;After picking a framework, spec out your resources (&lt;code&gt;/users&lt;/code&gt;: GET, POST etc.)&lt;/li&gt;
&lt;li&gt;Figure out how the framework of your choice is handling extracting parameters and JSON from the request and how to form a response&lt;/li&gt;
&lt;li&gt;Validate your input via &lt;code&gt;match&lt;/code&gt; and &lt;a href="https://docs.serde.rs/serde_json/" rel="noopener noreferrer"&gt;serde_json&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>api</category>
      <category>rust</category>
      <category>rest</category>
    </item>
    <item>
      <title>Explained: How does async work in Rust?</title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Tue, 02 Apr 2019 10:26:08 +0000</pubDate>
      <link>https://dev.to/gruberb/explained-how-does-async-work-in-rust-46f8</link>
      <guid>https://dev.to/gruberb/explained-how-does-async-work-in-rust-46f8</guid>
      <description>&lt;p&gt;This article will outline an overview of the why and how async exists in Rust. The differences between concurrency, parallelism  and asynchronous code won't be covered. &lt;/p&gt;

&lt;h2&gt;
  
  
  Async Concept
&lt;/h2&gt;

&lt;p&gt;Asynchronous programming is a concept which allows not blocking the program workflow when waiting for the results of certain actions. So you can open a large file or query a database, but your program will continue processing each line afterwards. &lt;/p&gt;

&lt;p&gt;This concept was first needed on the kernel level, because you want to be able to listen to music while you type something on your keyboard. On a software level, this was achieved through multi-threading. On the CPU side, you can have multiple processes running on each core at the same time.&lt;/p&gt;

&lt;p&gt;Later on, web servers came into play and needed to be able to hold millions of connections while performing I/O tasks. To be able to do this in a non-blocking way, we can either use threads on the kernel level, or implement our own way of handling threads and events.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's needed and why
&lt;/h2&gt;

&lt;p&gt;The kernel already has the concept implemented (through threads and other concepts), however they are quite "expensive", which means there is just a finite amount of resources available and dealing with this problem on OS level adds a whole new level of complexity.&lt;/p&gt;

&lt;p&gt;Therefore it would be nice to handle our internal async flow on program level. We need a so called runtime, which can handle async code and is able to communicate to the kernel.&lt;/p&gt;

&lt;p&gt;The general idea is: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement your own way of handling threads and queues on program level (green threads)&lt;/li&gt;
&lt;li&gt;Add syntactic sugar to your language so the runtime/compiler can identify async parts of the code&lt;/li&gt;
&lt;li&gt;Add async types so they can notify when they are "done"&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Instead of dealing with Strings for example, an async type needs to have certain states (processing and done). The runtime can handle these types and set the state in them. Afterwards in your code you can access the value at a later point or wait for them to be done before you continue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Workflow
&lt;/h2&gt;

&lt;p&gt;You mark a method in your code as async, in this async method you can now use your async types. You can either wait for them to finish ("fetch data from GitHub...") or you "start" them, continue with your flow and later on check if they finished and use the value from them.&lt;/p&gt;

&lt;p&gt;Once done writing the code, you need a runtime which can take this async part of your code and actually run it. The runtime needs also to take processes from the queue and hand it over to the operating system, since there is where the real work happens.&lt;/p&gt;

&lt;p&gt;After the operating system is done with the processing, it will notify the runtime, which in return will set the state inside the async type and hand it back to the program workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  NodeJS vs. Go. vs. Rust
&lt;/h2&gt;

&lt;p&gt;Lets look at how Node, Go and Rust are implementing the concepts we talked about, namely: Syntax, Type and Runtime.&lt;/p&gt;

&lt;h3&gt;
  
  
  NodeJS
&lt;/h3&gt;

&lt;p&gt;In NodeJS you have the &lt;code&gt;async/await&lt;/code&gt; syntax and &lt;code&gt;Promises&lt;/code&gt;. You can &lt;code&gt;await&lt;/code&gt; a &lt;code&gt;Promise&lt;/code&gt; aka an action which might need more time to process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frycux658tbbsgd6fzfer.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frycux658tbbsgd6fzfer.png" alt="NodeJS async" width="357" height="202"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const async_method = async () =&amp;gt; {
    const dbResults = await dbQuery();
    const results = await serviceCall(dbResults);
    console.log(results);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Go
&lt;/h2&gt;

&lt;p&gt;In Go, you start &lt;code&gt;goroutines&lt;/code&gt; instead of &lt;code&gt;Promises&lt;/code&gt;. And instead of &lt;code&gt;async/await&lt;/code&gt; you simple write &lt;code&gt;go method_name()&lt;/code&gt;. Instead of V8, Go ships with its own Go runtime.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Felky0e5t99pemq8enbi6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Felky0e5t99pemq8enbi6.png" alt="Go async" width="358" height="204"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(greeting string) {
    fmt.Println(greeting, ", World!")
}

go f("Hello")

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Rust
&lt;/h2&gt;

&lt;p&gt;The Rust Async ecosystem is still in progress and not final yet. The proposal here is to also use &lt;code&gt;async/await&lt;/code&gt;, instead of &lt;code&gt;Promises&lt;/code&gt; and &lt;code&gt;Goroutines&lt;/code&gt; you have &lt;a href="https://github.com/rust-lang-nursery/futures-rs"&gt;&lt;code&gt;Futures&lt;/code&gt;&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;The Rust Language Team decided &lt;a href="https://github.com/rust-lang/rfcs/blob/0806be4f282144cfcd55b1d20284b43f87cbe1c6/text/0230-remove-runtime.md"&gt;not to include&lt;/a&gt; any runtime. Rust wants to be as small as possible, and to be able to swap parts in and out as needed. Therefore you need to rely on crates to provide the appropiate runtime for you.&lt;/p&gt;

&lt;p&gt;The most popular one is &lt;a href="https://tokio.rs/"&gt;&lt;code&gt;tokio&lt;/code&gt;&lt;/a&gt;, which uses &lt;a href="https://github.com/carllerche/mio"&gt;&lt;code&gt;mio&lt;/code&gt;&lt;/a&gt; internally as its event queue. Even other runtimes are using &lt;code&gt;mio&lt;/code&gt; since it’s providing abstraction over kernel methods like epoll , kqueue and IOCP.&lt;/p&gt;

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

&lt;p&gt;One special feature about Rust is also that you have to "start" a Future. So just declaring it like a Promise in NodeJS or writing &lt;code&gt;go name_of_goroutine()&lt;/code&gt; doesn't trigger the Future to do something yet. So in case you are using tokio, you need to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let response = client.get("http://httpbin.org")

let response_is_ok = response
    .and_then(|resp| {
        println!("Status: {}", resp.status());
        Ok(())

tokio::run(response_is_ok);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the hopefully not so distant future, you can use &lt;code&gt;async&lt;/code&gt; in Rust like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[async]
fn async_function_name(...) -&amp;gt; Result&amp;lt;ReturnType, ErrorType&amp;gt; {
    let db_results = await!(query_database());
    let more_data = await!(fetch_another_service(db_resukts));
    process(more_data)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/alexcrichton/futures-await"&gt;The &lt;code&gt;async/await&lt;/code&gt; syntax&lt;/a&gt; is still in process and needs to be approved, merged and parts of the language adjusted to the new form.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rust Async in Detail
&lt;/h2&gt;

&lt;p&gt;Lets zoom in a bit on how a runtime works or can work:&lt;/p&gt;

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

&lt;p&gt;Tokio is using &lt;a href="https://tokio.rs/docs/going-deeper/building-runtime/"&gt;internally&lt;/a&gt; the &lt;a href="https://www.puncsky.com/blog/2015/01/13/understanding-reactor-pattern-for-highly-scalable-i-o-bound-web-server/"&gt;Reactor-Executor pattern&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;What tokio and other runtimes want to achieve is a highly scalable server for high raw data throughput. They don't want to block when doing I/O operations. We have basically two options here: Thread-Based or Event-Driven Architecture. To make it short: Thread-Based is limiting because of the limited physical resources.&lt;/p&gt;

&lt;p&gt;So Event-Driven is best in our case. It is registering incoming Future requests and saves a pointer to the async function handler. It then triggers an event in the kernel. Once the I/O operation is done, we call the pointer and execute the async method with the results from the I/O (kernel). &lt;/p&gt;

&lt;p&gt;For this, we need a Reactor, which notifies if data is coming over the network or a file writing operation is in progress, and an executor which takes this data and executes the async function (Future) with it.&lt;/p&gt;

&lt;p&gt;In addition, each runtime needs to understand kernel methods (like &lt;a href="https://people.eecs.berkeley.edu/~sangjin/2012/12/21/epoll-vs-kqueue.html"&gt;epoll&lt;/a&gt;) for starting I/O operations. For Rust there is a crate called &lt;a href="https://github.com/carllerche/mio"&gt;mio&lt;/a&gt; which implements these kernel methods. Tokio is using mio internally.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is it usable?
&lt;/h2&gt;

&lt;p&gt;There is a lot happening at the moment in async Rust world. It will take a bit of time to have a final version out there which is easy to use and understand. Until then you can use your web frameworks like you are used to, since they already ship with a runtime.&lt;/p&gt;

&lt;p&gt;Dropbox for example is using Futures in combination with &lt;code&gt;tokio&lt;/code&gt; in production to serve data from the disk on Dropbox's servers. Futures is in Rust stable in version 0.1, and in Rust nightly in version 0.3. The runtime &lt;code&gt;tokio&lt;/code&gt; is relying on Rust stable, so it is using Futures 0.1. &lt;/p&gt;

&lt;p&gt;You can transform 0.3 into 0.1 Futures and vice-versa via the &lt;a href="https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.9/futures/compat/index.html"&gt;compat module&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Rust needs a few more months to get ready for its easy to use and powerful Futures. They are less expensive as in other languages, and you can have a thin or thick runtime, it's totally up to you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get started
&lt;/h2&gt;

&lt;p&gt;As mentioned, tokio is one of the runtimes you can use. Another one is a combination of &lt;a href="https://github.com/withoutboats/romio"&gt;Romio&lt;/a&gt; and &lt;a href="https://github.com/withoutboats/juliex"&gt;Juliex&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;If you are building web applications, there is a crate called &lt;a href="https://hyper.rs/"&gt;hyper&lt;/a&gt;, which already includes tokio. So here you can use Futures 0.1 in you application. &lt;/p&gt;

&lt;h3&gt;
  
  
  Keep up to date
&lt;/h3&gt;

&lt;p&gt;You can check out the website &lt;a href="https://areweasyncyet.rs/"&gt;areweasyncyet&lt;/a&gt; to follow the progress on async Rust. Similarly, &lt;a href="http://www.arewewebyet.org/"&gt;arewewebyet&lt;/a&gt; is tracking the progress of frameworks and tools around building web applications.&lt;/p&gt;

&lt;h5&gt;
  
  
  Further reading
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://manishearth.github.io/blog/2018/01/10/whats-tokio-and-async-io-all-about/"&gt;What Are Tokio and Async IO All About?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://slides.com/wraithm/async-io-in-rust-and-haskell/#/"&gt;Futures in Rust and Haskell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://v8.dev/blog/fast-async"&gt;Fast async in NodeJS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gobyexample.com/goroutines"&gt;Go by example: Goroutines&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://tokio.rs/docs/going-deeper/building-runtime/"&gt;Tokio: Building a runtime&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>rust</category>
      <category>async</category>
    </item>
    <item>
      <title>Take a Business Approach for Learning a Programming Language</title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Wed, 27 Mar 2019 19:48:00 +0000</pubDate>
      <link>https://dev.to/gruberb/take-a-business-approach-for-learning-a-programming-language-1nmp</link>
      <guid>https://dev.to/gruberb/take-a-business-approach-for-learning-a-programming-language-1nmp</guid>
      <description>&lt;p&gt;The one most overlooked aspect of every field is to merge ideas from other areas of life. A business approach runs mostly contrary to programming. Business is outcome focused, programming is, for most of the time, done out of joy and the input (for the compiler) you are creating. &lt;/p&gt;

&lt;p&gt;However, every field runs and behaves like any other. Teaching has mostly the same problems as programming, which has has the same complications as investing. You just have to dig deep enough, cut out the complex and look at it from different angles. &lt;/p&gt;

&lt;p&gt;All fields share humans, and humans share psychology. What we also share is to improve, create and change. We want things faster and cheaper. This will always be true, no matter where you go. We also share our appetite for convenience and pleasure. &lt;/p&gt;

&lt;h2&gt;
  
  
  Birds eye view
&lt;/h2&gt;

&lt;p&gt;In economics, macro is always deciding micro. Which means: How a country governs, the age and distribution of a population decides how successful a company operating within this border will be. On city level, being for example in San Francisco at the right time has a bigger impact on a companies success then having a superior product in a poor country with no access. &lt;/p&gt;

&lt;p&gt;So what makes a company successful? You could say that they have the right market fit. They brought out a product at a time when people are ready for it. When the underlying infrastructure is there. Smartphones and mobile banking are two examples. Once there, not successful, but the idea came back and now everywhere. &lt;/p&gt;

&lt;h2&gt;
  
  
  If Rust would be a company, would you invest in it?
&lt;/h2&gt;

&lt;p&gt;When investing in companies, investors generally ask themselves: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Will this company play a bigger role in 5 years then it does now?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If this answer is yes, then you are on the right track. So first we have to ask ourselves: &lt;strong&gt;Will Rust play a bigger role in 5 years then it does now?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets make a few assumptions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.bloomberg.com/professional/blog/iot-market-predicted-double-2021-reaching-520-billion/"&gt;IoT will play a bigger role&lt;/a&gt;. Factories start to get into IoT, they want to improve processes. The amount of money saved is not comparable to customer applications. Speeding up production line by 1 hour each day has a massive impact. Much more then speeding up a web page by a few seconds. &lt;/li&gt;
&lt;li&gt;The last 10 years were focused on e-commerce and the creation of basic CRUD applications. The next 10 years will focus on &lt;a href="https://a16z.com/2018/01/21/next-3billion-financial-services-summit/"&gt;moving assets&lt;/a&gt;, getting real time information and processing a massive amount of information.&lt;/li&gt;
&lt;li&gt;At the same time, services will be more centralized and more de-centralized at the same time. Look at &lt;a href="https://store.google.com/magazine/stadia"&gt;Googles Stadia&lt;/a&gt;  gaming service. And think about Blockchain applications. I don't talk about money, but some form of Blockchain environment will be out there. &lt;/li&gt;
&lt;li&gt;Applications will run almost exclusively in the browser, and the underlying operating system will play a smaller role.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the past 10 years, NodeJS and JavaScript enabled a massive push towards the web. Picking up Node and creating a web service is super easy. And there will still be companies out there who will use NodeJS. There are still companies out there who need COBOL, PHP and C++ developers. But the push happened, e-commerce happens in the browser. The next move however will be to digitize assets, to put sensors on machines and humans, calculate the data and give feedback. &lt;/p&gt;

&lt;h2&gt;
  
  
  Two movements in the current programming environment
&lt;/h2&gt;

&lt;p&gt;There are two movements happening right now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.rust-lang.org/"&gt;Rust&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://hacks.mozilla.org/2018/10/webassemblys-post-mvp-future/"&gt;WASM&lt;/a&gt; and &lt;a href="https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/"&gt;WASI&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of just being a new programming language, Rust wants to be safe and fast. If you process billions of MQTT messages per hour, you want to rely on a fast and secure application stack. With more complex applications, you want to reduce server costs to a minimum, since you will host more on servers then in the last 10 years.&lt;/p&gt;

&lt;p&gt;Rust can be compiled to a &lt;a href="https://dev.to/gruberb/web-programming-in-rust-02x-deploy-your-first-app-1k05"&gt;single binary&lt;/a&gt;, with statically linked C libraries. &lt;/p&gt;

&lt;p&gt;In combination with WASM, which follows the trend that more and more applications will run in the browser, you can enable the feeling of using a native application. You could say that the browser is our new operating system. If there is no browser installed, how do you get your work done?&lt;/p&gt;

&lt;p&gt;Just look at Atlassian, Google Docs and others. WASM will enable many languages to compile nativaley to WASM, which will then execute the bytecode in the browser. The web as we know it is long dead. There is just a limited space for "Inspect Source". Which doesn't need to be bad, there will be tons of other tools out there which can teach you to code and dig into things. People generally just have a hard time saying goodbye to once loved tools and environments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/"&gt;WASI takes this idea even one step further&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is our Macro in Programming?
&lt;/h2&gt;

&lt;p&gt;Software is not really depended on countries (at least to some degree). So instead of countries, our macro are corporations. So if corporations push towards IoT, streaming games and processing much more data to be able to target customers, the company which does that in the most reliable way will succeed.&lt;/p&gt;

&lt;p&gt;Lets look why Mozilla (and others) want WebAssembly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The wasm format makes it possible to support graphics-heavy games in a browser without plug-ins. It can be used to port scientific simulation and other compute-intensive applications to the web platform. It also has non-web applications such as the Internet of Things, mobile apps and JavaScript virtual machines. (&lt;a href="https://research.mozilla.org/webassembly/"&gt;source&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So our Macro is companies who want to charge you a monthly fee by running their applications. And what is easier then distributing a product and charging for it through the browser? You also have less gatekeepers since the mobile platform is pretty much owned by Apple and Google. &lt;/p&gt;

&lt;p&gt;Many languages will compile to WASM, so you could say it will help other languages stay alive as well. The trend of processing billions of messages, reducing server costs and having one language for the whole stack (embedded, server, frontend) brings Rust into a leading position.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=4Oo3QcpGk84"&gt;Steve Jobs goal&lt;/a&gt; with NeXT was to to bring powerful computing into education. Well, maybe we are there now: You can spend 200 Dollar on a Computer but run a high-tech simulation program within your browser. Streamed from more powerful servers, rented for a monthly fee. &lt;/p&gt;

&lt;h2&gt;
  
  
  Which languages will thrive in this new macro environment?
&lt;/h2&gt;

&lt;p&gt;Certainly Go has a big share. Why? Because companies use it to develop deployment pipelines and tooling for operations. Just have a look at &lt;a href="https://github.com/digitalocean"&gt;DigitalOcean&lt;/a&gt;. Deploying software will play a bigger part in the future then it does now. So where Rust doesn't thrive, Go will certainly stay and take a piece of the pie.&lt;/p&gt;

&lt;p&gt;As we said before, the next generation of companies who were at the sidelines are moving into the market as well. Manufacturers. &lt;/p&gt;

&lt;p&gt;For these companies, one minute of downtime or production halts can cost millions. Creating reliable software, creating reliable data stores is worth a fortune. Rust with its low level, safety and speed focus will thrive in this environment. In addition it will soon be able to run on embedded devices as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Will one language rule them all?
&lt;/h2&gt;

&lt;p&gt;No. The reason is that it's too expensive to switch every stack in the world. An example:&lt;/p&gt;

&lt;p&gt;The Deutsche Bahn in Germany, which runs the rail roads, discovered, that if they increase the width between the tracks just by a few centimeters, the train would run much smoother. Easy solution right? Not so fast. The cost of this improvement outweight any gain. So it wasn't done. &lt;/p&gt;

&lt;p&gt;The same happens to programming languages. Once a product earns money and supports workers, it will run until the company dies. There will be a fair share of NodeJS, Python, Ruby and other languages out there until you retire.&lt;/p&gt;

&lt;p&gt;But, the question is: What would you teach your children? Which language (if they would like to become programmers) would you advice them to learn? Under this assumptions from before, I assume it would be Rust. &lt;/p&gt;

&lt;h2&gt;
  
  
  Which languages are a safe bet?
&lt;/h2&gt;

&lt;p&gt;Under my current assumptions, languages with the biggest leverage will be Go and Rust. Go is straight forward, is reducing discussion time with developers and with its type system is easier to handle then JavaScript. &lt;/p&gt;

&lt;p&gt;Rust will thrive due to its focus on safety and speed. Both of these things will be even more needed in the future then they are today. Just think about running async web servers. NodeJS was so much faster and easier to use then anything before. It focused on the right metrics. Rust does the same for the future metrics.&lt;/p&gt;

&lt;p&gt;TypeScript, Python and Java will still be there. It doesn't mean you won't find jobs, but under the new circumstances, Go and Rust outshine the rest. I wouldn't advice Junior Developers or colleague graduates to focus on Java or TypeScript at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Take some time every now and then and take a strategic approach. Reading The Economist or Financial Times can be as valuable as reading a programming blog. Companies will decide in which environment you are operating in. The once who can leverage technology to fit the current needs will be the once who strive. But never forget:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tools don't solve problems, people solve problems. You use the tools to solve problems.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>rust</category>
      <category>business</category>
      <category>productdevelopment</category>
    </item>
    <item>
      <title>What are your biggest obstacles/questions for moving from NodeJS to Rust?</title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Sat, 23 Mar 2019 15:32:41 +0000</pubDate>
      <link>https://dev.to/gruberb/what-are-your-biggest-obstaclesquestions-for-moving-from-nodejs-to-rust-4ckm</link>
      <guid>https://dev.to/gruberb/what-are-your-biggest-obstaclesquestions-for-moving-from-nodejs-to-rust-4ckm</guid>
      <description>&lt;p&gt;NodeJS is the perfect entry for building web applications. I am getting many messages lately that people want to give Rust a try and implement their first web service in it. I would love to hear from the dev.to community what your biggest hurdles are or which questions need to be answered before you feel comfortable jumping in!&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>How to combat JavaScript fatigue with Rust</title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Wed, 20 Mar 2019 19:05:24 +0000</pubDate>
      <link>https://dev.to/gruberb/how-to-combat-javascript-fatigue-with-rust-2o5l</link>
      <guid>https://dev.to/gruberb/how-to-combat-javascript-fatigue-with-rust-2o5l</guid>
      <description>&lt;p&gt;Do you find yourself in any of those thoughts when creating applications with NodeJS?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"I for sure need to pick the right npm module to solve this problem, but which one fits best and is getting supported well?"&lt;/li&gt;
&lt;li&gt;"Uh, my app is growing so big, I have no idea what the difference between &lt;code&gt;user&lt;/code&gt; and &lt;code&gt;customer&lt;/code&gt; is anymore and which properties they have."&lt;/li&gt;
&lt;li&gt;"TypeScript will definitely solve my problems!"&lt;/li&gt;
&lt;li&gt;"Hm, I don't know if this is an error with TypeScript or in the transpiled JavaScript? Or did I just disable the wrong ESLint option?"&lt;/li&gt;
&lt;li&gt;"Is this all what I will do for the rest of my career?"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Creating JavaScript applications is easy at first but it's getting harder and harder once the application grows. You can wait and hope that any of the big tech companies will solve your problems through TypeScript and other tools. A few years go by and you ask yourself: Is this all? Do I create CRUD application for the next 20 years?&lt;/p&gt;

&lt;p&gt;It is sometimes harder to keep up with the latest &lt;a href="https://dev.to/tomekponiat/javascript-predictions-for-2019-by-npm-pie"&gt;hype train&lt;/a&gt; then actually learning fundamentals. &lt;/p&gt;

&lt;p&gt;NodeJS became a victim of its own success. The market is flooded with developers, every company is using Node in some part of their tech stack. You can say that companies don 't pick it because of it's underlying technology but because they can get cheap labor and feel safe because all the other companies are also using Node, so it must be a good choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Meet Rust
&lt;/h2&gt;

&lt;p&gt;You probably know the saying that you should learn Lisp, even if you won't use it, just because the learned mindset will make you a better developer? Instead of learning Lisp to become a better developer, you can learn a language which is actually in use and will improve you as good as Lisp does. Meet Rust. Rust is not the latest hype, but has &lt;a href="https://arxiv.org/pdf/1903.00982.pdf"&gt;fundamentals&lt;/a&gt; which offer you a wide range of opportunities, even in the long run. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://www.rust-lang.org/"&gt;Rust&lt;/a&gt; is a systems language, but it's ergonomic syntax empowers the developer after the first struggles. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From a NodeJS perspective, Rust can be used to create web server applications and  services. You can write &lt;a href="https://aws.amazon.com/blogs/opensource/rust-runtime-for-aws-lambda/"&gt;Lambda functions&lt;/a&gt; or full fledged applications. Instead of using TypeScript, Rust has a strong build-in Type system. Instead of using ESLint, it ships with the &lt;a href="https://vorner.github.io/difficult.html"&gt;pickiest compiler out there&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;The pain you feel in the beginning is different. I call it good pain. Good pain lets you grow as a person (and developer), bad pain just hurts. I would categorize many of the TypeScript and NodeJS pains as bad pain. Not because the tools are bad, but a language which was created in &lt;a href="https://en.wikipedia.org/wiki/JavaScript#History"&gt;10 days&lt;/a&gt; just gets you so far. &lt;/p&gt;

&lt;p&gt;At some point you need to make a personal decision of how your life should look like. Your day to day has a huge impact on your happiness and the well-being of your surroundings. Rust can help you rediscover the joy of programming again.&lt;/p&gt;

&lt;p&gt;After you learned Rust, you may think you just replaced tool X with tool Y and end up in the same place. But, there is a difference. Once learned, you can use Rust to create:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Games&lt;/li&gt;
&lt;li&gt;CLI tools&lt;/li&gt;
&lt;li&gt;Operating Systems&lt;/li&gt;
&lt;li&gt;Web Services&lt;/li&gt;
&lt;li&gt;Browser Applications&lt;/li&gt;
&lt;li&gt;Applications on Embedded Devices&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One would say you can cover the whole stack. Rust creates a self feeding system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Developers from C, C++, Java, Kotlin, Python, Ruby and JavaScript flock to the language&lt;/li&gt;
&lt;li&gt;Rust and its features are getting discussed not only in a web context but in a wide range of scenarios&lt;/li&gt;
&lt;li&gt;Different areas, perspectives and use cases are getting fed back into the language in an &lt;a href="https://predictablynoisy.com/rust-governance"&gt;open way&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Lets picture a perfect world 5 years from now
&lt;/h2&gt;

&lt;p&gt;You are writing your web service in Rust. Instead of installing five different npm modules which help you cover your mistakes, you are using build-in tools to harden your code base. After the compiler gives you a green light you are 98% sure your application will run &lt;a href="https://youtu.be/cDFSrVhnZKo?t=1865"&gt;safe and fast&lt;/a&gt; in the years to come.&lt;/p&gt;

&lt;p&gt;But, hold on, you are still sometimes frustrated by the repetitive work environment. But now you mastered a language which you can use to build your own &lt;a href="https://os.phil-opp.com/"&gt;Operating System&lt;/a&gt; on the side, you can implement the &lt;a href="https://www.youtube.com/watch?v=bzja9fQWzdA"&gt;TCP protocol&lt;/a&gt;, you can &lt;a href="http://arewegameyet.com/"&gt;create Games&lt;/a&gt; and join a local game community. You can even create applications for the Browser which are &lt;a href="https://webassembly.org/"&gt;compiled to WASM&lt;/a&gt;. If this is not enough, you can get yourself a piece of hardware and &lt;a href="https://www.rust-lang.org/what/embedded"&gt;run Rust on it&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Node is not gone
&lt;/h2&gt;

&lt;p&gt;NodeJS won't be completely gone, and your skills neither. However you found a great addition and you might just focus on Rust more in the future. Remember, no language survived forever. NodeJS could end up with the same faith as PHP and others. Picking a systems language like Rust gives you strength and opportunities for many years to come.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should you learn Rust?
&lt;/h2&gt;

&lt;p&gt;From a developers perspective: Yes, definitely. Rust has many unique features which will be copied also to other programming languages (&lt;a href="https://github.com/apple/swift/blob/master/docs/OwnershipManifesto.md"&gt;like Ownership in Swift&lt;/a&gt;). Learning Rust sharpens your thinking and will make you more considered in your day-to-day language of choice.&lt;/p&gt;

&lt;p&gt;From a safety and correctness perspective: You should aim to replace part of your web service with Rust. Start small and see where it leads you. Other companies like &lt;a href="https://www.rust-lang.org/static/pdfs/Rust-npm-Whitepaper.pdf"&gt;NPM&lt;/a&gt;, &lt;a href="https://www.wired.com/2016/03/epic-story-dropboxs-exodus-amazon-cloud-empire/"&gt;Dropbox&lt;/a&gt; and &lt;a href="https://www.youtube.com/watch?v=u6ZbF4apABk"&gt;Yelp&lt;/a&gt; are doing it and had big success.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.arewewebyet.org/"&gt;Rust for the web is not completely there&lt;/a&gt;, but once fully landed, it can not only convert your web services into safe bets. Once mastered, Rust lets you jump up and down the stack, explore Computer Science concepts first hand and build tools in your free time other then the next CRUD application.&lt;/p&gt;

&lt;p&gt;So, take the chance and make 2019 the year you learn Rust. Have &lt;a href="https://dev.to/gruberb/web-programming-in-rust-02x-deploy-your-first-app-1k05"&gt;something in production&lt;/a&gt; by Christmas and try to replace part of your services at work in Rust and see if it's successful. Rust will not only let you sleep calmer at weekends, knowing that a rock solid &lt;a href="https://doc.rust-lang.org/book/ch14-01-release-profiles.html"&gt;binary&lt;/a&gt; is running in production, but the skills you gained let you explore a vast universe of possibilities around technology.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I will help you with that. Follow my series on &lt;a href="https://dev.to/gruberb/intro-to-web-programming-in-rust-for-nodejs-developers-lp"&gt;"Practical Web Development with Rust"&lt;/a&gt; and build with me a shiny web application in the next few months.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>node</category>
      <category>rust</category>
    </item>
    <item>
      <title>Web Development with Rust— 02/x: Deploy your first App</title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Tue, 19 Mar 2019 09:12:53 +0000</pubDate>
      <link>https://dev.to/gruberb/web-programming-in-rust-02x-deploy-your-first-app-1k05</link>
      <guid>https://dev.to/gruberb/web-programming-in-rust-02x-deploy-your-first-app-1k05</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;You can find the &lt;a href="https://dev.to/gruberb/intro-to-web-programming-in-rust-for-nodejs-developers-lp"&gt;Introduction to web programming in Rust&lt;/a&gt; over here. Follow me on &lt;a href="https://twitter.com/byteadventures" rel="noopener noreferrer"&gt;twitter&lt;/a&gt; to always get the latest information about web development in Rust. Also checkout the &lt;a href="https://github.com/gruberb/web-programming-in-rust" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt; to his series.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;em&gt;Update 08.07.2019: tide 0.2.0&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  This series has 3 goals
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Show the obstacles new developers will face when programming in Rust but also its advantages when it comes to the specific topic (this time: Deployments).&lt;/li&gt;
&lt;li&gt;Show different options for web development in Rust.&lt;/li&gt;
&lt;li&gt;Always have an updated application in production afterwards.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Number three is super important for me. That’s the reason why we start part 02/x with a few ideas on what you can build, and why this tutorial will look like as it does. Whenever you learn something new, take this mental model:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Never do things for their own sake&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Which translate to: Never learn Rust just because you want to learn Rust. This is the biggest factor why you will fail to learn a new language or basically anything in life. You have to have a goal in mind, a reason to do something.&lt;/p&gt;

&lt;h2&gt;
  
  
  “So how should I learn Rust then?”
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Have an app or idea in mind you want to see in production. This can be a service to calculate prime numbers, a web app to track your programming goals, a service which fetches your latest likes on GitHub and tracks their activities etc. You decide what you want to build.&lt;/li&gt;
&lt;li&gt;Hold yourself accountable to it. Tell your friends, work colleagues or your partner that you promise them that in 6 months time, they will be able to use this service. Every few days or weeks, you will keep them updated about your current status.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It doesn’t need to be a polished idea or a service to compete with other apps. It has to be something you would like to have. It will help you staying through the hardships in the beginning, and through the phase when the honey moon is over and you see that Rust can be hard at times.&lt;/p&gt;

&lt;h4&gt;
  
  
  I will make mine public as well:
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;At the end of the next 6 months, I have a running web service with frontend for my MeetUp “Rust and Tell Berlin” up an running so speakers can submit proposals and slides and videos can be watched from talks which were held the previous events.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I will hold myself accountable through this tutorial series.&lt;/p&gt;




&lt;p&gt;Lets move on. In this part of the series, we want to deploy our first application. If you come from NodeJS, the deployment life cycle looks like 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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fnodejs_dev.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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fnodejs_dev.png" alt="nodejs_dev_cycle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With NodeJS, you can push any code to a production server. You have to have good tests, ESLint and other tools to catch undefined and Type errors.&lt;/p&gt;

&lt;p&gt;In an ideal world, we have a development cycle which looks like 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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Frust_dev.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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Frust_dev.png" alt="ideal_dev_cycle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we want to break things as early and close to the code (your local machine) as possible. Once we figured out a working code base, we would like to bring exactly this working solution onto a server. Because of Rusts Type System and strong compiler, we would be able to pack a working binary and move it to production. Tests would cover the rest of the errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rust moves possible errors closer to the coding environment
&lt;/h3&gt;

&lt;p&gt;a) The Rust Compiler will catch a lot of problems, almost all of them.&lt;/p&gt;

&lt;p&gt;b) You can catch the rest with good tests (in our case: Error handling when receiving the wrong parameters).&lt;/p&gt;

&lt;p&gt;c) After you can compile your Rust Code, you have a binary which can be shipped in many different ways.&lt;/p&gt;

&lt;h3&gt;
  
  
  Difference between local and production-ready code
&lt;/h3&gt;

&lt;p&gt;When we talk about deploying, we have to make sure that our code is able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;randomly assign a PORT based on the environment it is running&lt;/li&gt;
&lt;li&gt;handle errors gracefully&lt;/li&gt;
&lt;li&gt;respond to not expected input with proper return codes and messages&lt;/li&gt;
&lt;li&gt;fail early in the deployment pipeline with a proper test setup&lt;/li&gt;
&lt;li&gt;log events so errors can be traced&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article we will cover the first must-have (randomly assigning a PORT). Each article in the series will cover the rest of the requirements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Four different deployment options
&lt;/h3&gt;

&lt;p&gt;We generally have different deployment and hosting options. Some are more suited for large scale application and some are better for private projects and to get a project off the ground without too much complexity. Our options are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Managed Deployments / Hosting (Heroku)&lt;/li&gt;
&lt;li&gt;Self managed via Docker and a Docker registry&lt;/li&gt;
&lt;li&gt;Self managed via Docker and a Git registry&lt;/li&gt;
&lt;li&gt;Managed Serverless Lambda functions (AWS Lambda, ZEIT now)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will cover each of these options in this article and see advantages, disadvantages and how to prepare your Rust Code so it can be deployed (in the best possible way).&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the first version of your app
&lt;/h2&gt;

&lt;p&gt;As we said in the beginning, we need an idea and what we want to build. Even if we map out a bigger picture of the application in the next article (03/x), we can get started and choose a framework we want to build it with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://rocket.rs/" rel="noopener noreferrer"&gt;rocket&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://actix.rs/" rel="noopener noreferrer"&gt;actix&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gotham.rs/" rel="noopener noreferrer"&gt;gotham&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/rustasync/tide" rel="noopener noreferrer"&gt;tide&lt;/a&gt; (work in progress)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As seen in the &lt;a href="https://dev.to/gruberb/intro-to-web-programming-in-rust-for-nodejs-developers-lp"&gt;first article&lt;/a&gt;, you can go lower level if you want:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://hyper.rs/" rel="noopener noreferrer"&gt;hyper&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.rs/reqwest/0.9.11/reqwest/" rel="noopener noreferrer"&gt;reqwest&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/carllerche/h2" rel="noopener noreferrer"&gt;h2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will pick one framework for the written version of this article. I will pick &lt;a href="https://github.com/rustasync/tide" rel="noopener noreferrer"&gt;tide&lt;/a&gt;, since I am planning to contribute to it more in the future. I will map out solutions for rocket and actix in the &lt;a href="https://github.com/gruberb/web-programming-in-rust" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt; for this series.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up our app
&lt;/h3&gt;

&lt;p&gt;We want to make sure to use asynchronous code, which is not in Rust stable yet. Therefore we need to install and set the &lt;a href="https://doc.rust-lang.org/book/appendix-07-nightly-rust.html" rel="noopener noreferrer"&gt;nightly version&lt;/a&gt; of 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;span class="nv"&gt;$ &lt;/span&gt;rustup &lt;span class="nb"&gt;install &lt;/span&gt;nightly-2019-02-25
&lt;span class="nv"&gt;$ &lt;/span&gt;rustup default nightly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will generate our first folder structure. The bare bones of a running web app with tide look like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cargo.toml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[package]
name = "my-cool-web-app"
version = "0.1.0"
authors = ["YOUR NAME + EMAIL"]
edition = "2018"

[dependencies]
tide = "0.2.0"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;main.rs&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#![feature(async_await)]

fn main() {
    let mut app = tide::App::new(());
    app.at("/").get(async move |_| "Hello, world!");

    app.serve();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we said earlier, we need to give the hosting environment the chance to assign a PORT to our application. &lt;/p&gt;

&lt;p&gt;Our &lt;strong&gt;main.rs&lt;/strong&gt; has to accompany these requirements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#![feature(async_await)]

extern crate tide;

use tide::App;
use std::{env, net::SocketAddr};


fn main() {
    let mut app = App::new(());
    let address = SocketAddr::from(([127, 0, 0, 1], get_server_port()));

    app.at("/").get(async move |_| "hello world");
    app.serve(address).expect("Start server");
}

fn get_server_port() -&amp;gt; u16 {
    env::var("PORT")
        .ok()
        .and_then(|port| port.parse().ok())
        .unwrap_or_else(|| 8186)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setup ready, we can go over each deployment option.&lt;/p&gt;

&lt;h2&gt;
  
  
  Managed Deployments via Heroku
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Managed environments are for the most part just an abstraction. They internally do the same as you would with your own pipeline: Push code to a git repository. A “hook” is watching this repository and on changes will start to compile the latest version and run it. For you however, it’s just a &lt;code&gt;git push heroku master&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fheroku.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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fheroku.png" alt="heroku_deployments"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get started, you need a &lt;a href="https://signup.heroku.com/" rel="noopener noreferrer"&gt;Heroku account&lt;/a&gt; (free). Login with your new account and create a new app:&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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2FBildschirmfoto%25202019-03-17%2520um%252011.35.50.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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2FBildschirmfoto%25202019-03-17%2520um%252011.35.50.png" alt="heroku_new_app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After clicking “Create app”, Heroku explains under the “Deploy” tab how to push your code to their servers:&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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2FBildschirmfoto%25202019-03-17%2520um%252011.36.13.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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2FBildschirmfoto%25202019-03-17%2520um%252011.36.13.png" alt="heroku_deploy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Prepare your code
&lt;/h4&gt;

&lt;p&gt;First, we need to be able to push our code base to the remote location (Heroku). Therefore please install the &lt;a href="https://devcenter.heroku.com/articles/heroku-cli" rel="noopener noreferrer"&gt;Heroku tool chain&lt;/a&gt;. Afterwards we can add the remote location to our GIT repository:&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;my-cool-web-app
&lt;span class="nv"&gt;$ &lt;/span&gt;heroku login
&lt;span class="nv"&gt;$ &lt;/span&gt;heroku git:remote &lt;span class="nt"&gt;-a&lt;/span&gt; my-cool-web-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we need to tell Heroku how to run our application after it is build. Heroku expects a file with the name &lt;strong&gt;Procfile&lt;/strong&gt;, which has the start command in it:&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;Procfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And put the following line it it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web ./target/release/my-cool-web-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also have to tell Heroku which version of Rust we are using. Since we want to use nightly, we create a file called &lt;strong&gt;RustConfig&lt;/strong&gt; in the root directory:&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;RustConfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with the following line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VERSION=nightly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Caveat
&lt;/h4&gt;

&lt;p&gt;Rust is so new that Heroku doesn’t support it out of the box. We need to install and activate a “buildpack” for Rust. So inside the root directory of your application, execute the following commands:&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="nv"&gt;$ &lt;/span&gt;heroku create &lt;span class="nt"&gt;--buildpack&lt;/span&gt; emk/rust
&lt;span class="nv"&gt;$ &lt;/span&gt;heroku buildbpacks:set emk/rust
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will activate the language support for Rust.&lt;/p&gt;

&lt;p&gt;Now we can:&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="nv"&gt;$ &lt;/span&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Init"&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git push heroku master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When succeeded, we go back to the Heroku dashboard in the browser and click on the the generated domain (under “Settings”). A browser windiw should open and display “Hello, World!”.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Heroku makes it easy to deploy your application&lt;/li&gt;
&lt;li&gt;In less then 5 minutes you have a running version of your app live&lt;/li&gt;
&lt;li&gt;You can assign your own domain and activate HTTPS (if you pay for it)&lt;/li&gt;
&lt;li&gt;Heroku ist the best option when it comes to this tutorial and starting side projects: Cheap, easy to use and removes the overhead of deplyoments especially in the beginning&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Docker
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Using Docker has the huge advantage of being free in choosing your pipelines and environments. You can either build the image locally and push it as-is to a Docker registry. From there a server can take(download) and execute (&lt;code&gt;docker run&lt;/code&gt;) it. Or you create a blueprint (Dockerfile) which other service can use to build on their servers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you are using Docker for your deployments, you have two options. The first one is to push your code (with a Dockerfile) to a Git registry (like GitHub or Bitbucket) and then have a configured deployment server which listens to changes, SSHs into the Git registry, takes the code, deploys and runs 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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fgit_docker.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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fgit_docker.png" alt="docker_git_registry"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your second option is to use a Docker registry. There you have the advantage to pre build your container and ship it as-it-is. This makes it sometimes faster to run deployments and you have to ship less code (especially in case of Rust).&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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fdocker_registry.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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fdocker_registry.png" alt="docker_registry"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use Rusts feature of being able to be compiled to a binary. We can even go one step further and compile a static Rust binary with no external dependencies. What we would need for this, is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build a Rust binary&lt;/li&gt;
&lt;li&gt;Statically linked the needed C libraries to it so it can run on it’s own&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The result would be to have a binary which doesn’t even need Rust to run. Thanks to the Open Source community and Erik Kidd, there is already a solution out there which helps us with that.&lt;/p&gt;

&lt;p&gt;The result is a super small Docker image with no external dependencies. Meet &lt;a href="https://github.com/emk/rust-musl-builder" rel="noopener noreferrer"&gt;rust-musl-builder&lt;/a&gt;. It is a Docker image which helps you build static Rust binaries. It will download the whole image just after the first execution.&lt;/p&gt;

&lt;p&gt;Everything we type and create happens from the root directory of our application.&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;my-cool-web-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we create our Dockerfile, lets see what we actually trying to do. We are using the rust-musl-builder to statically link the musl-libc library into our binary.&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="nv"&gt;$ &lt;/span&gt;docker run &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;:/home/rust/src ekidd/rust-musl-builder cargo build &lt;span class="nt"&gt;--release&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create our super small binary. You can inspect it like that:&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-lh&lt;/span&gt; target/x86_64-unknown-linux-musl/release/my-cool-web-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is just a few MB small (in my example: 4,4MB). To be able to recreate this procedure over and over again, and not just on our local machine but also in a deployment pipeline on different servers, we create a multi-stage Dockerfile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM ekidd/rust-musl-builder:nightly AS build
COPY . ./
RUN sudo chown -R rust:rust .
RUN cargo build --release

FROM scratch
COPY --from=build /home/rust/src/target/x86_64-unknown-linux-musl/release/my-cool-web-app /
ENV PORT 8181
EXPOSE ${PORT}
CMD ["/my-cool-web-app"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can build the image now via:&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="nv"&gt;$ &lt;/span&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; my-cool-web-app:latest &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And run it with:&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="nv"&gt;$ &lt;/span&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;-P&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; heroku heroku:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can open your browser (in macOS) via:&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="nv"&gt;$ &lt;/span&gt;open http://&lt;span class="si"&gt;$(&lt;/span&gt;docker container port my-cool-web-app 8181&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We just created a super minimal Docker image which contains our binary with no external dependencies. You can inspect your just created image via:&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="nv"&gt;$ &lt;/span&gt;docker image &lt;span class="nb"&gt;ls &lt;/span&gt;my-cool-web-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fcarbon.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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fcarbon.png" alt="small_docker_image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Docker is a beast, but when used wisely can be quite helpful&lt;/li&gt;
&lt;li&gt;Especially with Rust: You can create statically linked binaries which are super small and don’t even need a Rust environment to run in&lt;/li&gt;
&lt;li&gt;You also have much more options to host and run your application when choosing Docker&lt;/li&gt;
&lt;li&gt;However, managed hosting environments like Heroku don’t allow pushing Docker images to their environment&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Serverless runtimes — ZEIT/now
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Serverless is a different mindset then the first two options. Serverless also means stateless, so you are not building web applications but functions. Instead of having API endpoints build into your app, you basically just have those API endpoints (in serverless terms: handlers). Our web frameworks like rocket and actix might be an overkill here. &lt;a href="https://github.com/zeit/now-builders/issues/297#issuecomment-473682599" rel="noopener noreferrer"&gt;Right now, ZEIT is not supporting Rust nightly builds in their new serverless environment&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So instead of creating a binary (with &lt;code&gt;cargo new web-app&lt;/code&gt;), we create a library:&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="nv"&gt;$ &lt;/span&gt;cargo new now-service &lt;span class="nt"&gt;--lib&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;now-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have to create a file called &lt;code&gt;now.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "now-service",
  "version": 2,
  "builds": [
    {
      "src": "src/index.rs",
      "use": "@now/rust"
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And our &lt;code&gt;src/lib.rs&lt;/code&gt; example looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use http::{Request, Response, StatusCode, header};

fn handler(request: Request&amp;lt;()&amp;gt;) -&amp;gt; http::Result&amp;lt;Response&amp;lt;String&amp;gt;&amp;gt; {
    let response = Response::builder()
        .status(StatusCode::OK)
        .header(header::CONTENT_TYPE, "text/html")
        .body("&amp;lt;!doctype html&amp;gt;&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;A simple deployment with Now!&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;body&amp;gt;&amp;lt;h1&amp;gt;Welcome to Rust on Now&amp;lt;/h1&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;".to_string())
        .expect("failed to render response");

    Ok(response)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As with Heroku, you need to install the &lt;a href="https://zeit.co/docs/v2/getting-started/installation/" rel="noopener noreferrer"&gt;ZEIT toolchain&lt;/a&gt;, which is called “now”. There are several options. If you are on macOS, you can do it via:&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="nv"&gt;$ &lt;/span&gt;brew cask &lt;span class="nb"&gt;install &lt;/span&gt;now
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which installs the Now application. Find it in your /Applications folder and open it. You can finish the installation by typing in your email address. This will also install the command line tool chain.&lt;/p&gt;

&lt;p&gt;That’s basically it. You can type:&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="nv"&gt;$ &lt;/span&gt;now
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and hit Enter. This will start the upload of your application. Login to your &lt;a href="https://zeit.co/dashboard" rel="noopener noreferrer"&gt;ZEIT dashboard&lt;/a&gt; and click on the provided link.&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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fzeit_dashboard.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%2Fraw.githubusercontent.com%2Fgruberb%2Fweb-programming-in-rust%2Fmaster%2Fassets%2Fzeit_dashboard.png" alt="ZEIT_dashboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Serverless lets you save costs: The service is just running when requested&lt;/li&gt;
&lt;li&gt;This ends up in higher boot times which need to be considered&lt;/li&gt;
&lt;li&gt;The mindset of serverless lets you rethink state and if you really need a fully fledged web application for some use cases&lt;/li&gt;
&lt;li&gt;The deployment can take a bit longer when using AWS Lambda or ZEITs now&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Follow me on &lt;a href="https://twitter.com/byteadventures" rel="noopener noreferrer"&gt;twitter&lt;/a&gt; to always get the latest information about web development in Rust. Also checkout the &lt;a href="https://github.com/gruberb/web-programming-in-rust" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt; to his series.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>node</category>
      <category>rust</category>
      <category>devops</category>
      <category>docker</category>
    </item>
    <item>
      <title>Intro to Web Development with Rust for NodeJS Developers</title>
      <dc:creator>Bastian Gruber</dc:creator>
      <pubDate>Mon, 18 Mar 2019 21:37:37 +0000</pubDate>
      <link>https://dev.to/gruberb/intro-to-web-programming-in-rust-for-nodejs-developers-lp</link>
      <guid>https://dev.to/gruberb/intro-to-web-programming-in-rust-for-nodejs-developers-lp</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;You can find the second article in this series ("deploy your first Rust app") &lt;a href="https://dev.to/gruberb/web-programming-in-rust-02x-deploy-your-first-app-1k05"&gt;over here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Rust is different. You can pick up Python or Ruby over the weekend, create a first CRUD application and be happy with the results.&lt;/p&gt;

&lt;p&gt;With Rust… with Rust you will struggle to pass a String to a different method, change and return it. You then will order the Rust book, see its size, &lt;em&gt;sigh&lt;/em&gt; and get started.&lt;/p&gt;

&lt;p&gt;After a few weeks of fighting through the book after work, you give up and wait until someone else creates an easy-to-follow tutorial.&lt;/p&gt;

&lt;h3&gt;
  
  
  Here is your “easy” tutorial
&lt;/h3&gt;

&lt;p&gt;I struggled with the same problems. Life circumstances however gave me a few months time on my hands to really focus on Rust.&lt;/p&gt;

&lt;p&gt;What follows is a first overview, concept, and paths to follow. In the coming weeks and months, I’ll publish a series of articles to help you to get from concept to product.&lt;/p&gt;

&lt;h3&gt;
  
  
  NodeJS vs Rust
&lt;/h3&gt;

&lt;p&gt;After installing them (I chose brew for macOS in this example, the method doesn’t matter), the underlying stack looks different. NodeJS needs V8, the runtime engine from Google, and bindings to the JavaScript library to run JavaScript code.&lt;/p&gt;

&lt;p&gt;Rust depends almost completely on Rust itself. Just the compiler is using &lt;code&gt;llvm&lt;/code&gt; &lt;a href="https://llvm.org/" rel="noopener noreferrer"&gt;libraries&lt;/a&gt;, which are written in C and C++.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  How much "web" is in Rust?
&lt;/h3&gt;

&lt;p&gt;It was and is a design decision &lt;a href="https://users.rust-lang.org/t/announcing-the-http-crate/12123" rel="noopener noreferrer"&gt;not to include a standard http library&lt;/a&gt; in Rust. The OSI layer is therefore covered differently:&lt;/p&gt;

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

&lt;p&gt;Node covers the whole stack, and offers with Koa and Express, two well-known and “rock-solid” web frameworks which help you to build applications on top of HTTP.&lt;/p&gt;

&lt;p&gt;On the Rust side of things, just &lt;a href="https://doc.rust-lang.org/std/net/struct.TcpStream.html" rel="noopener noreferrer"&gt;TCP&lt;/a&gt; is implemented in the Rust Core. The current web frameworks (&lt;a href="https://actix.rs/actix/actix/" rel="noopener noreferrer"&gt;actix&lt;/a&gt; and &lt;a href="https://rocket.rs/" rel="noopener noreferrer"&gt;rocket&lt;/a&gt;) are implementing everything up until HTTP though. So you don’t need to care where this is coming from.&lt;/p&gt;

&lt;p&gt;If you want to use pure HTTP calls without any larger framework, you can install “crates” (equivalent to npm packages in the Node world) which implement the HTTP protocol (like &lt;a href="https://github.com/hyperium/hyper" rel="noopener noreferrer"&gt;hyper&lt;/a&gt; and &lt;a href="https://github.com/tiny-http/tiny-http" rel="noopener noreferrer"&gt;tiny_http&lt;/a&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  npm vs cargo
&lt;/h3&gt;

&lt;p&gt;Node is using npm for its package management:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm install&lt;/code&gt; is installing dependencies&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm run xyz&lt;/code&gt; is executing scripts inside the &lt;code&gt;package.json&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the Rust side, &lt;code&gt;cargo&lt;/code&gt; is &lt;a href="https://doc.rust-lang.org/cargo/" rel="noopener noreferrer"&gt;handling everything related to your project&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cargo new NAME --bin&lt;/code&gt; is creating an application&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cargo new NAME --lib&lt;/code&gt; to create a library&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cargo run&lt;/code&gt; is executing the code&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cargo build&lt;/code&gt; is creating an executable&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cargo test&lt;/code&gt; is running all tests inside the project&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is an &lt;a href="https://github.com/rust-lang/cargo/issues/5586" rel="noopener noreferrer"&gt;open PR&lt;/a&gt; to add &lt;code&gt;cargo add&lt;/code&gt; to install dependencies. Right now you have to add them by hand to your &lt;code&gt;Cargo.toml&lt;/code&gt; file. As you see, you don’t need to include scripts in a &lt;code&gt;package.json&lt;/code&gt; to run tests or build and test your application.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Mindset change: Cargo is fetching the packages after &lt;code&gt;cargo run&lt;/code&gt;, and just if the version changed. So the first time it will fetch all packages, the second time just when a change in the version number happened. Unlike &lt;code&gt;npm i&lt;/code&gt; which fetches the packages right away, and will add it to the &lt;code&gt;package.json&lt;/code&gt; with the &lt;code&gt;save&lt;/code&gt; notation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Ecosystem
&lt;/h3&gt;

&lt;p&gt;Node is not successful for no reason. The ecosystem is rich and flourishing. Rust is still developing, but has already many great “crates”. The website &lt;a href="http://www.arewewebyet.org/" rel="noopener noreferrer"&gt;arewewebyet.org&lt;/a&gt; is tracking the progress and showing you interesting packages in the Rust world.&lt;/p&gt;

&lt;p&gt;There is also an attempt to create an official Rust Web Framework, called &lt;a href="https://github.com/rustasync/tide" rel="noopener noreferrer"&gt;Tide&lt;/a&gt;. It is already pretty mature and can be used for side projects. Feel free to contribute and help craft a great environment for web development in Rust.&lt;/p&gt;

&lt;h3&gt;
  
  
  Asnyc programming aka Promises aka Futures
&lt;/h3&gt;

&lt;p&gt;Nodes killer feature are Promises. Although not always easy to understand and handle, Promises and the event loop are what makes Node so lucrative.&lt;/p&gt;

&lt;p&gt;Rust is also implementing an asynchronous mechanism, which are not yet in the final version. They are called &lt;a href="https://github.com/rust-lang-nursery/futures-rs" rel="noopener noreferrer"&gt;Futures&lt;/a&gt;. A library called &lt;a href="https://tokio.rs/" rel="noopener noreferrer"&gt;Tokio&lt;/a&gt; is already offering an asynchronous run time. You can track the progress on asynchronous programming in Rust over at &lt;a href="https://areweasyncyet.rs/" rel="noopener noreferrer"&gt;areweasyncyet&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to get started?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Install Rust: &lt;code&gt;curl https://sh.rustup.rs -sSf | sh&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create a new project: &lt;code&gt;cargo new web-app --bin&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cd web-app&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now you can choose your web framework of choice. You can either start with &lt;a href="https://rocket.rs/" rel="noopener noreferrer"&gt;rocket&lt;/a&gt; or &lt;a href="https://actix.rs/" rel="noopener noreferrer"&gt;actix&lt;/a&gt;. You can follow the tutorials on the website to get a first web application running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Heads up: undefined, borrowing and types
&lt;/h3&gt;

&lt;p&gt;To not to get frustrated until my next post, here are the main four things about Rust you will have to get used to (which are quite awesome after a while).&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;There is no undefined. Rust has no real null value. This is a feature. Rust has a type called &lt;a href="https://doc.rust-lang.org/std/option/index.html" rel="noopener noreferrer"&gt;Option&lt;/a&gt;, which encapsulates either the return value or None. In this case, we use the &lt;a href="https://doc.rust-lang.org/std/result/#result-and-option" rel="noopener noreferrer"&gt;Result&lt;/a&gt; type to return a value. You could, instead of returning a String, return an Option which might have a String value, or None if the website we are fetching from doesn’t contain any text. An easy solution, which you should not use in production, is to &lt;code&gt;.unwrap()&lt;/code&gt; results to get the String out of the encapsulation. &lt;a href="https://doc.rust-lang.org/std/result/#result-and-option" rel="noopener noreferrer"&gt;Homework: Try to check if the GET request errored and return an Error in this case instead of Ok().&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;You must have heard about &lt;a href="https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html" rel="noopener noreferrer"&gt;borrowing&lt;/a&gt;. In short: Every assignment (=) to a non trivial type (everything which doesn’t have a fixed size) moves the ownership over. The method &lt;code&gt;fetch_text()&lt;/code&gt; is not taking ownership over the url but just using a reference to it (via the &lt;code&gt;&amp;amp;&lt;/code&gt; ). &lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2018&amp;amp;gist=d7527a2b8570393dd70c2083172d621a" rel="noopener noreferrer"&gt;Homework: Figure out why this code here fails and how to solve it.&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;You always have to know which type is returned from a method. In this example, our HTTP crate &lt;code&gt;reqwest&lt;/code&gt; is returning a Response struct (type) which implements certain methods. The documentation is, as with so many crates, excellent. &lt;a href="https://docs.rs/reqwest/0.9.11/reqwest/struct.Response.html#method.text" rel="noopener noreferrer"&gt;So have a look here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;You don’t have to type &lt;code&gt;return&lt;/code&gt; to return a value from a method. Just don’t put a &lt;code&gt;;&lt;/code&gt; at the end of a statement and this becomes your return value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use this example to play around, see where you can get errors and learn to fix them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is this all?
&lt;/h3&gt;

&lt;p&gt;Two opinions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“Sadly no!”&lt;/li&gt;
&lt;li&gt;“No, but that’s a good thing!”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You actually have to learn a decent amount of Rust to get started. This is what I am here for. In the next few days, weeks and months I will cover the basics up until creating a solid web application.&lt;/p&gt;

&lt;p&gt;Up until then, I can recommend the &lt;a href="https://exercism.io/my/tracks/rust" rel="noopener noreferrer"&gt;Rust track on Exercism.io&lt;/a&gt; and the Rust Book which you can find also in a paper version at your local book store or at Amazon.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@gruberbastian/moving-from-nodejs-to-rust-in-2019-62c0b85d2fa7" rel="noopener noreferrer"&gt;As mentioned in my first article&lt;/a&gt;, Rust is making you a better developer, so the road will be long at times, but always worth it. Follow me on &lt;a href="https://twitter.com/byteadventures" rel="noopener noreferrer"&gt;twitter&lt;/a&gt;, &lt;a href="https://medium.com/@gruberbastian" rel="noopener noreferrer"&gt;Medium&lt;/a&gt; or on &lt;a href="https://dev.to/gruberb"&gt;dev.to&lt;/a&gt; to stay up to date!&lt;/p&gt;

</description>
      <category>rust</category>
      <category>node</category>
    </item>
  </channel>
</rss>
