<?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: Sami Pietikäinen</title>
    <description>The latest articles on DEV Community by Sami Pietikäinen (@samipietikainen).</description>
    <link>https://dev.to/samipietikainen</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%2F24032%2Fe330c0b5-d815-4154-aba3-63052048bb3c.jpg</url>
      <title>DEV Community: Sami Pietikäinen</title>
      <link>https://dev.to/samipietikainen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samipietikainen"/>
    <language>en</language>
    <item>
      <title>Don't over-engineer!</title>
      <dc:creator>Sami Pietikäinen</dc:creator>
      <pubDate>Thu, 07 Feb 2019 17:31:55 +0000</pubDate>
      <link>https://dev.to/samipietikainen/dont-over-engineer-3a6e</link>
      <guid>https://dev.to/samipietikainen/dont-over-engineer-3a6e</guid>
      <description>&lt;p&gt;Most if not all programmers nowadays are familiar with the concept of clean code. In short, the clean code principle means a development style that focuses on code that is easy to write, understand and maintain. Yet, it seems that often these same principles are not applied to the overall design.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Any fool can write code that a computer can understand. Good programmers write code that humans can understand. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Martin Fowler&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Still, even if a code base has clear naming practices, meaningful comments and the code in function-level is understandable and clean, it is still possible that the overall design is hard to comprehend and the project is hard to maintain. This is often because the same clean coding methodology that is applied to code in small is not applied to the overall design. The design is over-engineered.&lt;/p&gt;

&lt;p&gt;Sometimes the code base looks as if the author read "the Gang of Four" design pattern book and tried to apply every single one of them in a single code base. There are just facades, decorators, adapters, observers and other design patterns on top of one another and the overall design is nearly impossible to grasp. Other times it seems that the author had an impulse to reinvent every wheel there is. Own containers. Own locking primitives. Own algorithms. Everything written from scratch.&lt;/p&gt;

&lt;p&gt;Overly complex, over-engineered code can be as hard to understand and maintain as badly written spaghetti code. Of course, sometimes projects really are complex and require abstractions and architecture that is challenging to understand. There also certainly are situations where implementing your own algorithms or containers is the right thing to do. The important thing is that there should be a clear reason why that additional complexity is necessary. The design should never be complex just for the sake of complexity. In fact, contrived complexity, forced use of overcomplicated design when simple would suffice, is one of the common &lt;em&gt;code smells&lt;/em&gt; described by Martin Fowler.&lt;/p&gt;

&lt;p&gt;Projects may end up overly complex for a number of reasons. The design might be clean and simple in the beginning, but become muddy and complex when new features and other changes that not quite fit the architecture are glued into it without refactoring. Other projects might be too complex from the get-go when the design tries to accommodate all imaginable changes that &lt;em&gt;might&lt;/em&gt; happen in the future.&lt;/p&gt;

&lt;p&gt;Thing is, designing a good solid architecture is not easy. In real-world projects requirements change along the way and new features are introduced that were not known in the beginning. Also when the project matures, other fixes, tweaks and updates are applied along the way. On top of that, project schedule often adds pressure to get things done quickly. The design should be flexible enough to allow all these changes with reasonable effort while still being as simple as reasonably possible. And even without all these external changes it is hard the get the design right on the first try. It usually takes some iterations and refinements to converge to the final design&lt;/p&gt;

&lt;p&gt;Unfortunately there is no silver bullet that always gives an optimal architecture. But applying the same clean coding principles that work on function and class level to architecture is a good way to avoid muddy and over-engineered design. Probably the most important advice is to keep things simple. Add configurations and use design patterns only when really needed. Create abstractions only when they contribute positively to the design by removing duplication or decoupling modules for instance. Don't try to predict future. Design the system using requirements that are actually known today.&lt;/p&gt;

&lt;p&gt;When the future does come and the design needs to adapt, it is important that the system is easy to change. Often new features require refactoring the existing design or otherwise they are just glued in which decays the architecture. Soon even simple changes can break the system unexpectedly from multiple places and subtle bugs are introduced. Changes start to take longer and longer to implement because the design is too fragile.&lt;/p&gt;

&lt;p&gt;The key to make the design easy to change is to make it easy to test. When the functionality can be verified easily and automatically, developers can make refactorings without being scared of breaking things. When the system is easy to test, it is easier to refactor continuously and thus keep both code in small as well as the larger design clean. Small imperfections and problems tend to grow so deal with them immediately.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One broken window, left unrepaired for any substantial length of time, instills in the inhabitants of the building a sense of abandonment—a sense that the powers that be don’t care about the building. So another window gets broken. People start littering. Graffiti appears. Serious structural damage begins. In a relatively short space of time, the building becomes damaged beyond the owner’s desire to fix it, and the sense of abandonment becomes reality.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Andy Hunt, The Pragmatic Programmer&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Don't live with broken windows. Make sure both the code and the larger architecture stays simple and clean. Refactor continuously to accommodate changes, and add abstraction and complexity only when it is justifiable and contributes positively to the design. Don't over-engineer.&lt;/p&gt;

&lt;p&gt;Originally posted on my &lt;a href="https://pagefault.blog/"&gt;personal blog&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>design</category>
    </item>
    <item>
      <title>Why junior devs should review seniors' commits</title>
      <dc:creator>Sami Pietikäinen</dc:creator>
      <pubDate>Sun, 08 Apr 2018 13:45:19 +0000</pubDate>
      <link>https://dev.to/samipietikainen/why-junior-devs-should-review-seniors-commits-feb</link>
      <guid>https://dev.to/samipietikainen/why-junior-devs-should-review-seniors-commits-feb</guid>
      <description>&lt;p&gt;Code reviews are a great practise to find mistakes, potential bugs and other issues overlooked by the code author. Reviewing improves the overall code quality, but it is also a great way to share knowledge and information. Junior developers' commits should certainly be reviewed by more senior developers, but juniors should also be the reviewers. Here's why.&lt;/p&gt;

&lt;p&gt;In every project there are things that need familiarization when a developer (junior or not) joins the team. These can include for instance code and project organization, testing, build, deployment and the general way of working in the project. Especially in large projects, understanding the existing architecture and design decisions as well as the rationale behind these decisions can be a major challenge for a new developer.&lt;/p&gt;

&lt;p&gt;Both the technical aspects and the way of working can usually be found from the project documentation. Nevertheless, implementing new features or fixing bugs in an unfamiliar code base is still challenging. What would be a better way to learn than following the example of more experienced developers in the same project?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What would be a better way to learn than following the example of more experienced developers in the same project?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Having young developer go through commits of experienced developers by reviewing is a great learning experience for the reviewer. Not only does it give a great reference for design and architectural decisions as well as coding and style practices, but it also shows how to apply these in the actual project in which the developer is working on. For the reviewee, it does not hurt to have an extra set of eyes looking at the code even though the reviewer is not so experienced. In fact, someone who is not so familiar with the project most likely looks at it from a fresh perspective.&lt;/p&gt;

&lt;p&gt;A lot of the benefits from this kind of review goes to the junior developer. However, there are also benefits for the project. Obviously the developer can familiarize quicker, but the reviews also give a wider view to the project than what would be directly necessary for the task at hand. Chances are that the now new developer will at some point maintain the project, so having a wider perspective from the get go is invaluable.&lt;/p&gt;

&lt;p&gt;So far the discussion has been solely technical. Yet, the review practices can also have a social effect. When a new developer is asked to review commits of already established members, it shows that they are accepted as a member of the team and their input and opinions are valued. As always, successful review practices require healthy working environment. Everyone should be able to both give and receive feedback without taking the comments personally. To reinforce this atmosphere it is good to have everyone from juniors to seniors to be both reviewers and reviewees.&lt;/p&gt;

&lt;p&gt;These same practices can also be applied across teams and across people with different competences. Why not have embedded developers review some GUI code or backend developers some front-end changes. Just because something is beyond someones' main competence area, does not mean they would be unable to spot mistakes or give feedback (of course some more subtle mistakes that relate to a specific library or language would still need reviewer with strong competence in that area). &lt;/p&gt;

&lt;p&gt;So let's make the most of code reviews and have everyone from juniors to seniors take part.&lt;/p&gt;

</description>
      <category>codereview</category>
      <category>career</category>
    </item>
    <item>
      <title>Implementing REST client with Rust: an introduction to traits and generics</title>
      <dc:creator>Sami Pietikäinen</dc:creator>
      <pubDate>Wed, 27 Dec 2017 09:21:00 +0000</pubDate>
      <link>https://dev.to/samipietikainen/implementing-rest-client-with-rust-an-introduction-to-traits-and-generics-4pk9</link>
      <guid>https://dev.to/samipietikainen/implementing-rest-client-with-rust-an-introduction-to-traits-and-generics-4pk9</guid>
      <description>&lt;p&gt;After reading the Rust book and &lt;a href="https://pagefault.blog/2017/09/14/my-experiences-learning-rust/"&gt;writing about my initial experiences&lt;/a&gt;, I wanted a small practical project to familiarize myself with the language. So, I decided to write a REST client library. This post shows, step-by-step, how my initial design was refactored to be more robust and easy-to-use. Traits and generics are key concepts in these refactorings. The intent is not to cover the internals of the library in detail and also the basic syntax of traits and generics is not covered here. The Rust Book &lt;a href="https://doc.rust-lang.org/book/second-edition/ch10-00-generics.html"&gt;chapter 10&lt;/a&gt; is an excellent resource for the latter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Restson
&lt;/h2&gt;

&lt;p&gt;The library I implemented is called Restson and it is a client library for JSON encoded REST APIs. The main idea is to provide easy-to-use interface for API requests and also handle URL parsing as well as serialization and deserialization from Rust structs automatically. The library is implemented using Hyper, which is the de-factor HTTP library for Rust. &lt;/p&gt;

&lt;p&gt;The Restson library is available in &lt;a href="https://github.com/spietika/restson-rust"&gt;GitHub&lt;/a&gt; and in &lt;a href="https://crates.io/crates/restson"&gt;crates.io&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design iterations
&lt;/h2&gt;

&lt;p&gt;When I started writing the library I was new to Rust (I still am) and I had also never used Hyper before. So I did not end up with elegant design right away. Though, usually you don't end up with the final design right away even if you are familiar with the language. At least for me it usually takes some iterations and refactoring to improve and refine the initial ideas and structures.&lt;/p&gt;

&lt;p&gt;Lets look at how the GET interface signature looked initially:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Result&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the function took the REST endpoint URL as a parameter, performed the GET request with Hyper library and then returned the request body as a string. From functionality point-of-view the function above is not &lt;em&gt;wrong&lt;/em&gt; because it does get the job done. However, it is not ideal for the library user and there are also multiple disadvantages with this design.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The whole URL needs to be provided every time the interface is used:

&lt;ul&gt;
&lt;li&gt;URL literals scattered in the code&lt;/li&gt;
&lt;li&gt;Changing the base URL requires modifying all the API calls&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Client caller needs to handle URL parsing (which adds boilerplate and is error prone)&lt;/li&gt;
&lt;li&gt;Client caller needs to manually deserialize the returned JSON in GET and serialize data to JSON in POST.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets look at how these disadvantages can be fixed by improving the library interface.&lt;/p&gt;

&lt;h3&gt;
  
  
  Automatic (de)serialization
&lt;/h3&gt;

&lt;p&gt;Lets put the URL parsing aside for now, and look at the return value of the function first. For simple use cases it might be ok to just manipulate the JSON directly, but it is often better to deserialize it to struct. This allows the compiler to do proper type checks and also reduces dependencies to the exact JSON format. For instance, if some field name changes, only the (de)serialization code needs to be updated instead of all the places where the data is used.&lt;/p&gt;

&lt;p&gt;To be able to return deserialized data, the &lt;code&gt;get&lt;/code&gt;-function needs to know the type in which the JSON is deserialized to. However, these types are defined by the library user and cannot be fixed in the library code. This can be solved by making &lt;code&gt;get&lt;/code&gt; a generic function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="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="k"&gt;where&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;serde&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;de&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;DeserializeOwned&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generic type &lt;code&gt;T&lt;/code&gt; is introduced and instead of returning &lt;code&gt;Result&amp;lt;String, Error&amp;gt;&lt;/code&gt; the function now returns &lt;code&gt;Result&amp;lt;T, Error&amp;gt;&lt;/code&gt;. Now the function can return different user defined types that are deserialized from the JSON returned by the server. In Rust, type inference also makes generics convenient to use:&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;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MyType&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="s"&gt;"url"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, the compiler is able to figure out that generic type &lt;code&gt;T&lt;/code&gt; in &lt;code&gt;get&lt;/code&gt; must be &lt;code&gt;MyType&lt;/code&gt; because the return value is assigned to variable of that type (return value of &lt;code&gt;unwrap&lt;/code&gt; to be exact). The generic type could also be explicitly annotated but it is unnecessary here. The call above is equivalent with the example below.&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="py"&gt;.get&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;MyType&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"url"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might be wondering what is the &lt;code&gt;where T: serde::de::DeserializeOwned&lt;/code&gt; part for. Well, the generic type &lt;code&gt;T&lt;/code&gt;cannot be any type. The &lt;code&gt;get&lt;/code&gt;-function can only accept types that implement deserialization interface. In Restson library, the deserialization is implemented using Serde library, so type &lt;code&gt;T&lt;/code&gt; needs to implement &lt;code&gt;DeserializeOwned&lt;/code&gt;trait. This way the implementation of &lt;code&gt;get&lt;/code&gt; can call &lt;code&gt;serde_json::from_str&lt;/code&gt; function for that type.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;where&lt;/code&gt; clause sets a trait bound which makes sure that if &lt;code&gt;T&lt;/code&gt; does not implement &lt;code&gt;DeserializeOwned&lt;/code&gt; and thus does not have &lt;code&gt;serde_json::from_str&lt;/code&gt; function available, the build won't pass. For the library user, implementing this trait is easy thanks to macros provided by Serde.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(Deserialize)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;MyType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;field1&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;field2&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using a generic type for the return value, the library is now able to automatically deserialize the data to user defined types. Relatively minor change on the library that makes the interface more convenient to use.&lt;/p&gt;

&lt;h3&gt;
  
  
  URL handling
&lt;/h3&gt;

&lt;p&gt;First obvious improvement to the URL handling is to give the base URL when the client instance is created. Then the individual API functions like &lt;code&gt;get&lt;/code&gt; would only take the REST path as a parameter.&lt;/p&gt;

&lt;p&gt;This is already better, but still error prone. It would still be easy to use wrong type of return value with an API path. This would mean that deserialization would always fail, and the error would not be caught during compilation. Furthermore, there would still be path string literals scattered in the code.&lt;/p&gt;

&lt;p&gt;One approach to solve this would be to associate the API path with the type that is used with the library. That is, each type that is used in REST requests would be able to return the corresponding API path. This is accomplished by defining a trait for the REST path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;RestPath&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;get_path&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&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;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;fn&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="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="k"&gt;where&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;serde&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;de&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;DeserializeOwned&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;RestPath&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, each type that is used as the return type &lt;code&gt;T&lt;/code&gt;, must implement both &lt;code&gt;DeserializedOwned&lt;/code&gt; and &lt;code&gt;RestPath&lt;/code&gt; traits. This is also enforced by the compiler, and if the traits have not been implemented the build will fail. The URL parameter can be removed from &lt;code&gt;get&lt;/code&gt; altogether because the implementation can call &lt;code&gt;T::get_path()&lt;/code&gt; to get the correct path and the base URL is provided when the client is instantiated. The trait implementation simply needs to return the correct path string:&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;RestPath&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;MyType&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;get_path&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"apipath"&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;Now the API path is only defined once and the interface can be used without even knowing what the actual path string is:&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;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MyType&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="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is still one major weakness in this design. For instance, a path like &lt;code&gt;api/devices/&amp;lt;ID&amp;gt;/status&lt;/code&gt; would be difficult to implement because it contains a parameter that should be easily changeable. However, the current &lt;code&gt;get_path&lt;/code&gt; does not take any parameters.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parametrized paths
&lt;/h3&gt;

&lt;p&gt;To be able to return parametrized API paths, the &lt;code&gt;get_path&lt;/code&gt; functions should take a parameter that is used to format the path string. For instance:&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;RestPath&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;MyType&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;get_path&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="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nd"&gt;format!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"api/devices/{}/status"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This parameter would also need to be forwarded from the interface functions such as &lt;code&gt;get&lt;/code&gt;. The problem is, however, that this is library code and cannot know what types the library user would like to use as path parameters. One way to dodge this problem would be to pass, for instance, a vector of strings. Although this would work, it is not optimal. For one, it would require unnecessary string conversion code for the library user and also make error checking in the trait implementation harder. Better alternative is to make &lt;code&gt;RestPath&lt;/code&gt; trait generic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;RestPath&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;U&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;fn&lt;/span&gt; &lt;span class="nf"&gt;get_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;par&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;U&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;Result&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="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;Now the library user can use whatever parameter type to implement the trait. The return value is also changed to &lt;code&gt;Result&amp;lt;String, Error&amp;gt;&lt;/code&gt; so that the trait implementation can indicate errors with the provided parameters. These changes also require changes to &lt;code&gt;get&lt;/code&gt; interface to pass the parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;U&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;U&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;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="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="k"&gt;where&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;serde&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;de&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;DeserializeOwned&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;RestPath&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;U&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another generic parameter &lt;code&gt;U&lt;/code&gt; is introduced to the interface which is used in the trait bound &lt;code&gt;RestPath&amp;lt;U&amp;gt;&lt;/code&gt;, and also taken in as &lt;code&gt;param&lt;/code&gt;. When the library implementation calls &lt;code&gt;get_path&lt;/code&gt; function, the parameter is passed to it &lt;code&gt;T::get_path(params)&lt;/code&gt;. If a &lt;code&gt;RestPath&lt;/code&gt; implementation for type &lt;code&gt;T&lt;/code&gt; with correct parameter type is not found, the compiler produces an error. The interface now looks pretty complex but it is actually pretty simple to use.&lt;/p&gt;

&lt;p&gt;First the &lt;code&gt;RestPath&lt;/code&gt; is implemented for user type:&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;RestPath&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;MyType&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;get_path&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="nb"&gt;u32&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;Result&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="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="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;format!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"api/devices/{}/status"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the path parameter is simply given to the interface function which passes it to &lt;code&gt;get_path&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="c"&gt;// calls api/devices/1234/status and deserializes&lt;/span&gt;
&lt;span class="c"&gt;// returned JSON to data variable&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MyType&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="mi"&gt;1234&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;RestPath&lt;/code&gt; trait can also be implemented multiple times for the same type with different generic parameters. It is also possible to provide multiple path parameters by using, for instance, a tuple.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;These refactorings improved the initial design which, although working, was not very convenient for the library user. It was also prone to errors that could not easily be detected during compilation. By using a combination of generics and traits the interface was kept simple to use but still a lot of common functionality (such as serialization) was encapsulated inside the library. User types were also allowed with the library which enables more error checking by the compiler (compared to passing things as, for instance, strings).&lt;/p&gt;

&lt;p&gt;All in all the traits and generics are powerful tools to have especially when writing library, utility or other reusable code. &lt;/p&gt;

</description>
      <category>rust</category>
      <category>rest</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>My experiences learning Rust</title>
      <dc:creator>Sami Pietikäinen</dc:creator>
      <pubDate>Thu, 21 Sep 2017 17:30:59 +0000</pubDate>
      <link>https://dev.to/samipietikainen/my-experiences-learning-rust</link>
      <guid>https://dev.to/samipietikainen/my-experiences-learning-rust</guid>
      <description>&lt;p&gt;Originally posted on my &lt;a href="https://pagefault.blog"&gt;personal blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Having seen a lot of positive buzz around the Rust language, I decided to look into it myself aswell. So, past couple of weeks I have been reading the second edition of &lt;a href="https://doc.rust-lang.org/book/second-edition/"&gt;The Rust Programming Language&lt;/a&gt; book which is an introductory to the language. This blog post is a round-up of my first impressions about the language and its features, and it also serves as a quick intro for others interested in it. In my day job I work mostly with C and C++ so those are my main reference points when learning a new language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In the first chapter of the book the main idea of Rust is coined as follows.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rust is a programming language that’s focused on safety, speed, and concurrency. Its design lets you create programs that have the performance and control of a low-level language, but with the powerful abstractions of a high-level language.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Coming from the embedded world I especially like the fact that Rust has a strong focus on safety, performance and concurrency. Many other modern languages have chosen an approach with garbage collection and many also require a runtime which can make them unsuitable for low-level programming. This is probably one of the main reasons that languages such as C and C++ are still very widely used.&lt;/p&gt;

&lt;p&gt;Rust is a compiled language and the binaries are compiled directly to target architecture. Rust also does the safety checks during compilation so the added safety does not have runtime performance penalty, which makes it quite unique.&lt;/p&gt;

&lt;h2&gt;
  
  
  Borrow-checker
&lt;/h2&gt;

&lt;p&gt;Ownership is a key concept in Rust. Each value has a variable that is owner of that data, and there can only be one owner at a time. If the value is moved to a new variable, the ownership is transferred and the old variable becomes invalid. Values can also be borrowed using references but there can only be one mutable reference at a time. These rules are enforced at compile time by a borrow checker.&lt;/p&gt;

&lt;p&gt;In contrast, C/C++ do not have a concept of ownership similar to Rust’s. For instance, in C++ there can be multiple pointers and references to same data simultaneously, and it is the programmer’s responsibility to make sure that there are, for instance, no data races. At first it might feel that the borrow checker “just get’s in the way” because it produces a lot of compiler errors (at least before you get used to the ownership concept), but in the end it provides valuable safe-guards against many common issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  No Exceptions
&lt;/h2&gt;

&lt;p&gt;Developers who are familiar with languages like C++, Python and Java should also be familiar with exceptions as an error handling mechanism. When an error occurs, an exception is thrown and the call stack is unwind until the exception is caught. If the exception is not caught at all, the program is terminated. This scheme allows some parts of the code, for instance libraries, to easily let the application logic to handle errors in a meaningful way. One drawback however, especially in C++, is that it is hard to tell which exceptions can be thrown and also the error handling code can be far away from the part that triggered the error.&lt;/p&gt;

&lt;p&gt;The error handling approach in Rust is not to use exceptions. Instead, Rust has two different mechanisms for error handling. If the error is unrecoverable, the program will panic (i.e. terminate). Alternatively, for recoverable errors  &lt;code&gt;Result&amp;lt;T,E&amp;gt;&lt;/code&gt; type is used. With this approach the compiler can enforce that the error variant is always handled when the return value is obtained from the &lt;code&gt;Result&lt;/code&gt;. This is also much more robust than encoding the failure information to the returned value (e.g. returning -1). Moreover, Rust also provides syntax to easily propagate errors from functions with &lt;code&gt;?&lt;/code&gt; operator.&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;file_len&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;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;io&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="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nf"&gt;.metadata&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nf"&gt;.len&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;In the example above, if either &lt;strong&gt;open&lt;/strong&gt; or &lt;strong&gt;metadata&lt;/strong&gt; call fails, the returned error is automatically propagated without the need to explicitly read and return it. This removes unnecessary boilerplate code and makes the resulting code simpler and cleaner.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enums On Steroids
&lt;/h2&gt;

&lt;p&gt;I have strong C/C++ background so for me enumerations are just a collection of explicitly named constants that use integral type as underlying type. In Rust enums are much more than that, and they are better described as a type that represents one or many variants. These variants can also include data, and what’s more each variant can also have different data.&lt;/p&gt;

&lt;p&gt;This makes the enum type suitable not only to enumerate the possible variants but also to model their data into the same type. Examples can be found from the &lt;a href="https://rustbyexample.com/custom_types/enum.html"&gt;Rust by example&lt;/a&gt; website.&lt;/p&gt;

&lt;h2&gt;
  
  
  It's a Match!
&lt;/h2&gt;

&lt;p&gt;Another cool feature in Rust are patterns. Patterns and pattern matching are used in many places but probably the most powerful is the &lt;code&gt;match&lt;/code&gt; operator. It can be thought of as, well, &lt;code&gt;switch-case&lt;/code&gt; on steroids. Instead of matching simple values, it is possible to have complex patterns and have additional match guards as well. Also, the compiler enforces that all enum variants are always handled. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="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="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"first is 2"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;=&amp;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;"first {}, last {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="mi"&gt;_&lt;/span&gt;                   &lt;span class="k"&gt;=&amp;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;"default..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first case (called a match arm in Rust) matches if the first field in the tuple is 2. The second arm captures first and last field (so that they can be used in the match body) and matches if the first field is larger than 2. Finally the third arm has a wildcard and will mach all remaining cases. This also satisfies the requirement that all cases need to be handled. This is just a simple example and there is a whole &lt;a href="https://doc.rust-lang.org/book/second-edition/ch18-00-patterns.html"&gt;chapter&lt;/a&gt; dedicated for patterns in the Rust book. Basically, the very flexible enums combined with pattern matching provides a very expressive way to model and manipulate data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type Inference And Coercions
&lt;/h2&gt;

&lt;p&gt;Rust is a statically typed language which means that the compiler must know all the types during compilation. In other statically typed languages like C this means that the programmer must explicitly tell what types variables, parameters and return values are. In Rust a technique called type inference is used to deduce the types. So, even though the language is statically typed, most of the time programmer does not need to write the type explicitly. The compiler is able to figure it out from the context.&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="c"&gt;// create a empty vector without annotating the type&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c"&gt;// here the compiler is able to see that it is Vec&amp;lt;String&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rust also uses deref coercion which is easily demonstrated with a simple example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Arc&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;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;String&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;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&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="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Arc&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;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="cm"&gt;/* access value in Arc with * and then take reference with &amp;amp; */&lt;/span&gt;
    &lt;span class="nf"&gt;func&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;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="cm"&gt;/* Compiler does deref coercion and translates &amp;amp;s automatically to &amp;amp;*s */&lt;/span&gt;
    &lt;span class="nf"&gt;func&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;s&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;First atomically reference counted String object is created (String wrapped in &lt;a href="https://doc.rust-lang.org/std/sync/struct.Arc.html"&gt;Arc&lt;/a&gt;). Then a function that takes a reference to string is called with this object. First the object inside &lt;code&gt;Arc&lt;/code&gt; is explicitly accessed with &lt;code&gt;*&lt;/code&gt; operator. Deref coercion allows us to just type &amp;amp;s because it is clear that we intend to use the value inside the &lt;code&gt;Arc&lt;/code&gt;. Overall, the coercions and inference allow to simplify the code in situations where the compiler can figure out proper types without explicit annotations. Modern C++ has &lt;code&gt;auto&lt;/code&gt; keyword, but Rust takes this concept further.&lt;/p&gt;

&lt;h2&gt;
  
  
  Zero-cost Abstractions
&lt;/h2&gt;

&lt;p&gt;One of the selling points of Rust are zero-cost abstractions. In practice this means an ability to use high-level concepts such as closures and iterator adapters without any performance costs. Also, when using these high level aspects the produced code is as fast or &lt;a href="https://doc.rust-lang.org/book/second-edition/ch13-04-performance.html"&gt;faster&lt;/a&gt; than hand coded implementation.&lt;/p&gt;

&lt;p&gt;Here's an example that shows functional programming in Rust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;collections&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;HashMap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;HashMap&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;char&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test string"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.chars&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.for_each&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dict&lt;/span&gt;&lt;span class="nf"&gt;.entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.or_insert&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="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;dict&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code calculates occurences of characters from input string, and generates &lt;code&gt;HashMap&lt;/code&gt; with char-count pairs. The example above outputs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{'t': 3, 'e': 1, 's': 2, ' ': 1, 'r': 1, 'i': 1, 'g': 1, 'n': 1}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  There's No Null
&lt;/h2&gt;

&lt;p&gt;One interesting idea in Rust is that the language does not have a concept of null. At first thought this might seem very strange since we’re all so used to it, but it does remove a myriad of common bugs altogether. In other languages like C/C++ pointers have to be explicitly checked for null or the application will crash and burn if the null object is dereferenced.&lt;/p&gt;

&lt;p&gt;Usually null value is used to indicate that some resource is not valid, and this information is indicated by a special null value. The problem is that the null condition needs to be manually checked which is error prone. The concept of something not necessarily being valid is still needed in Rust. It is just implemented differently.&lt;/p&gt;

&lt;p&gt;Instead of indicating the invalid state of an object by special value, Rust implements this by wrapping the actual object in &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt; or &lt;code&gt;Result&amp;lt;T,E&amp;gt;&lt;/code&gt; type. This way when the concrete type is accessed the compiler will enforce that also the fail case is handled or the code won’t compile.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fearless Concurrency
&lt;/h2&gt;

&lt;p&gt;One of the big goals, and also one of the &lt;a href="https://doc.rust-lang.org/book/second-edition/ch16-00-concurrency.html"&gt;chapters&lt;/a&gt; in the Rust Book, is fearless concurrency. As one would expect, Rust provides the familiar primitives like threads, mutexes and also channels for message passing. However, the lifetime model and ownership rules makes working with concurrency much safer than in many other languages.&lt;/p&gt;

&lt;p&gt;Rust is, for instance, able to detect data races between threads. That is, when multiple threads try to modify same data without proper mutual exclusion. The ownership system and the way mutexes are implemented also guarantees that locks are always acquired and released. Also the trait system is used to “mark” types that are thread-safe which means that the program will not compile if unsafe methods are used in a threaded context.&lt;/p&gt;

&lt;h2&gt;
  
  
  Convention Over Configuration
&lt;/h2&gt;

&lt;p&gt;Rust uses a convention over configuration design paradigm which aims to decrease the number of decisions developers need to make. This paradigm is used heavily on code and test organization. Code modules, source files, unit tests and integrations tests are organized in a certain way and the compiler is able to find these items without explicit configuration.&lt;/p&gt;

&lt;p&gt;One of the main benefits of this paradigm, besides the reduced configuration, is that different projects are organized in a similar manner which makes navigating other project’s sources easier. This is certainly not the case with C++.&lt;/p&gt;

&lt;h2&gt;
  
  
  Useful Compiler Error Messages
&lt;/h2&gt;

&lt;p&gt;If you have ever made a mistake with C++ templates (for instance the STL), you probably know what useful error message does not look like. You’ll most likely get couple of screenfuls of incomprehensible text. The error messages can be cryptic even for the most simplest of errors.&lt;/p&gt;

&lt;p&gt;My experience with Rust so far is that the compiler errors are really helpful and often point out the exact error and even suggest the fix. For instance, if the &lt;code&gt;file_len&lt;/code&gt; example earlier had an extra &lt;code&gt;;&lt;/code&gt; in the &lt;code&gt;Ok(File::open(“test.txt”)?.metadata()?.len());&lt;/code&gt; line, the compiler error is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error[E0308]: mismatched types
 --&amp;gt; src/main.rs:5:41
  |
5 |   fn file_len() -&amp;gt; Result&amp;lt;u64, io::Error&amp;gt; {
  |  _________________________________________^
6 | |     Ok(File::open("test.txt")?.metadata()?.len());
  | |                                                  - help: consider removing this semicolon
7 | | }
  | |_^ expected enum `std::result::Result`, found ()
  |
  = note: expected type `std::result::Result&amp;lt;u64, std::io::Error&amp;gt;`
             found type `()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;“Consider removing this semicolon”&lt;/em&gt; and it is even pointed out in the code. Pretty useful. This is important because especially in the beginning you are likely to get errors often (at least I did) due to the many checks Rust does. This of course is a good thing because the checks point out potential bugs in the code. But it certainly helps a lot when the compiler points you out to right direction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Strict Types And Casts
&lt;/h2&gt;

&lt;p&gt;In C and C++ the compiler converts numeric types automatically. So a C/C++ developer would expect that assigning a uint8 to uint32 should just work.&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;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, Rust is more strict about types and the code above would produce an error. The programmer must explicitly cast the value to correct type &lt;code&gt;let b: i32 = a as i32;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object-Oriented Or Not?
&lt;/h2&gt;

&lt;p&gt;Inheritance sets Rust apart from many common high-level languages. That is because Rust does not have it even though it does have other object-oriented features like encapsulation. On the other hand, Rust has a feature called traits to describe a common behavior same way as interfaces are used in other languages. Also trait objects can be used in a same way as polymorphic types in languages with inheritance.&lt;/p&gt;

&lt;p&gt;Whether Rust is categorized as object-oriented depends on the definition. Object-oriented patterns can still be implemented in Rust, but probably the approach needs to be a bit different from the “textbook” implementation. There is a complete &lt;a href="https://doc.rust-lang.org/book/second-edition/ch17-00-oop.html"&gt;chapter&lt;/a&gt; dedicated to this discussion in the Rust Book.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;There is so much more that could be written about Rust. For instance, support for &lt;a href="https://doc.rust-lang.org/book/second-edition/ch11-00-testing.html"&gt;unit and integration testing&lt;/a&gt; or the &lt;a href="https://doc.rust-lang.org/book/second-edition/ch14-00-more-about-cargo.html"&gt;cargo tool and crates.io&lt;/a&gt;. Though, the main idea here was to briefly introduce some aspects about the language and standard library that I find especially interesting. If this got you interested, I would really recommend the &lt;a href="https://doc.rust-lang.org/book/second-edition/ch01-00-introduction.html"&gt;Rust Book&lt;/a&gt;. It’s easy to follow, covers the language comprehensively and best of all is completely free.&lt;/p&gt;

&lt;p&gt;From what I have learned so far, Rust seems to address many of the common challenges in programming. Best of all, these additional checks and guarantees are done during compilation so they do not have a run-time penalty. The language is also compiled directly to machine code which makes it suitable for embedded targets (where garbage collection or runtimes would be unsuitable). As my first project I started to work on &lt;a href="https://github.com/spietika/restson-rust"&gt;REST client&lt;/a&gt; with automatic serialization and deserialization. Overall my first impression is that a lot has been done right in this language. Can’t wait to dive into deeper!&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
    </item>
    <item>
      <title>Easily auto-squash changes to previous commit</title>
      <dc:creator>Sami Pietikäinen</dc:creator>
      <pubDate>Wed, 12 Jul 2017 17:49:07 +0000</pubDate>
      <link>https://dev.to/samipietikainen/easily-auto-squash-changes-to-previous-commit</link>
      <guid>https://dev.to/samipietikainen/easily-auto-squash-changes-to-previous-commit</guid>
      <description>&lt;p&gt;After committing, I always like to go through the diff before pushing the changes upstream. Just to make sure that everything is as it should be. Well, quite often I still spot some typos, commented out code that I was supposed to remove or some other minor things that need to be fixed. So I ended up running the same exact commands time and time again. First new commit for the fixes and then interactive rebase to squash the changes to the previous commit. Perfect opportunity to make an alias to save those precious keystrokes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;fixlast &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;git commit &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="nt"&gt;--fixup&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;HEAD &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;GIT_EDITOR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;git rebase &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nt"&gt;--autosquash&lt;/span&gt; HEAD~2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can add the aliases to, for instance, your &lt;strong&gt;~/.gitconfig&lt;/strong&gt; file. With this alias you can easily autosquash changes to the previous commit by running &lt;strong&gt;git fixlast&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZBV23udW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pagefaultblog.files.wordpress.com/2017/07/git-autosquash-to-previous.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZBV23udW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pagefaultblog.files.wordpress.com/2017/07/git-autosquash-to-previous.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's break it down quickly. To easily run multiple commands I've used &lt;strong&gt;!&lt;/strong&gt; to run shell commands instead of only git sub-commands. The first part takes all changes and creates a fixup commit for the previous commit, &lt;strong&gt;HEAD&lt;/strong&gt;. The fixup argument adds &lt;strong&gt;fixup!&lt;/strong&gt; prefix to the commit message which will be recognized by the autosquash later.&lt;/p&gt;

&lt;p&gt;The second part starts interactive rebase and tells git to process all fixup and squash commits (&lt;strong&gt;--autosquash&lt;/strong&gt; argument). The &lt;strong&gt;HEAD~2&lt;/strong&gt; takes two topmost commits to the rebase.&lt;/p&gt;

&lt;p&gt;Normally, the interactive rebase opens an editor, but in this case we really just want to accept the autosquash without any manual selections. To do so &lt;strong&gt;GIT_EDITOR&lt;/strong&gt; environment variable is set to true (i.e. /bin/true application). So when git wants to open the rebase editor, it just receives success return code immediately and carries on with the rebase. That's all there is to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edit:&lt;/strong&gt; Just realized that the solution above is bit overly complicated. You could accomplish the same with --amend commit flag. Still, using the GIT_EDITOR environment variable can be used to avoid the amend editor pop up.&lt;/p&gt;

&lt;p&gt;This was also posted on my &lt;a href="https://pagefault.blog"&gt;blog&lt;/a&gt;. Check it out if you're interested.&lt;/p&gt;

</description>
      <category>git</category>
    </item>
  </channel>
</rss>
