<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Rust on nidomiro</title><link>https://nidomiro.de/tags/rust/</link><description>Recent content in Rust on nidomiro</description><generator>Hugo</generator><language>en</language><lastBuildDate>Fri, 14 Mar 2025 10:43:41 +0100</lastBuildDate><atom:link href="https://nidomiro.de/tags/rust/index.xml" rel="self" type="application/rss+xml"/><item><title>Rust-04: More operators and loops</title><link>https://nidomiro.de/code/learning-rust/04-more-operators-and-loops/</link><pubDate>Sat, 16 Jul 2022 12:00:00 +0100</pubDate><guid>https://nidomiro.de/code/learning-rust/04-more-operators-and-loops/</guid><description>&lt;p>In the last post we improved the simple program to be extendable with other operators.
In this post we will implement more operators and a loop.&lt;/p>
&lt;p>Let&amp;rsquo;s start with the simple task: adding more operators.
To support more operators we have to change two parts of the calculator, the parsing and the evaluation.
First let&amp;rsquo;s look into changing the parser.
For this we simply add the new operators (&lt;code>-*/&lt;/code>) to the regex inside &lt;code>Operation::from_string&lt;/code>.
The resulting regex will look like this: &lt;code>^\s*(\d+)\s*([+\-*/])\s*(\d+)\s*$&lt;/code>.
Now the parser accepts the new operators; first part done.&lt;/p></description></item><item><title>Rust-03: Improving the simple program</title><link>https://nidomiro.de/code/learning-rust/03-improving-the-simple-program/</link><pubDate>Mon, 11 Apr 2022 12:00:00 +0100</pubDate><guid>https://nidomiro.de/code/learning-rust/03-improving-the-simple-program/</guid><description>&lt;p>In the last post we created a simple program that would accept an input like &lt;code>5+5&lt;/code> and calculates the result.
In this post we will improve this program.&lt;/p>
&lt;p>To prepare for adding more operators we need to improve the input-parsing.
Currently, we just split the input-string at the plus-sign.
If we want to know the operator, we have to choose another approach.&lt;/p>
&lt;p>In this case I chose regex:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-rust" data-lang="rust">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff79c6">#[derive(Debug)]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff79c6">struct&lt;/span> &lt;span style="color:#50fa7b">Operation&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> left: &lt;span style="color:#8be9fd">i32&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> operator: &lt;span style="color:#8be9fd">char&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> right: &lt;span style="color:#8be9fd">i32&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff79c6">impl&lt;/span> Operation {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff79c6">fn&lt;/span> &lt;span style="color:#50fa7b">from_string&lt;/span>(input: &lt;span style="color:#ff79c6">&amp;amp;&lt;/span>&lt;span style="color:#8be9fd;font-style:italic">String&lt;/span>) -&amp;gt; &lt;span style="color:#8be9fd;font-style:italic">Option&lt;/span>&lt;span style="color:#ff79c6">&amp;lt;&lt;/span>Operation&lt;span style="color:#ff79c6">&amp;gt;&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#8be9fd;font-style:italic">let&lt;/span> regex &lt;span style="color:#ff79c6">=&lt;/span> Regex::new(&lt;span style="color:#f1fa8c">r&lt;/span>&lt;span style="color:#f1fa8c">&amp;#34;^\s*(\d+)\s*([+])\s*(\d+)\s*$&amp;#34;&lt;/span>).unwrap();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> regex.captures(&lt;span style="color:#ff79c6">&amp;amp;&lt;/span>input).map(&lt;span style="color:#ff79c6">|&lt;/span>capture&lt;span style="color:#ff79c6">|&lt;/span> Operation {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> left: (&lt;span style="color:#ff79c6">&amp;amp;&lt;/span>capture[&lt;span style="color:#bd93f9">1&lt;/span>]).parse().unwrap(),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> operator: (&lt;span style="color:#ff79c6">&amp;amp;&lt;/span>capture[&lt;span style="color:#bd93f9">2&lt;/span>]).parse().unwrap(),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> right: (&lt;span style="color:#ff79c6">&amp;amp;&lt;/span>capture[&lt;span style="color:#bd93f9">3&lt;/span>]).parse().unwrap(),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> })
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>As you can see we have a new thing called &lt;code>struct&lt;/code> in the code.
If you are coming from a language like Java or Typescript, a struct can be seen as a class without any methods.
This struct is called &lt;code>Operation&lt;/code> and as the name suggests it holds an operation.
The fields &lt;code>left&lt;/code> and &lt;code>right&lt;/code> are both typed as an &lt;code>i32&lt;/code> or in other words a &amp;ldquo;signed 32-bit integer&amp;rdquo;.
The field &lt;code>operator&lt;/code> is a &lt;code>char&lt;/code> since we only use one character for an operator.
In rust &lt;code>strings&lt;/code> and &lt;code>chars&lt;/code> are utf-8, therefore a &lt;code>char&lt;/code> can contain one unicode codepoint, not just a byte.&lt;/p></description></item><item><title>Rust-02: The beginning</title><link>https://nidomiro.de/code/learning-rust/02-the-beginning/</link><pubDate>Fri, 25 Mar 2022 12:00:00 +0100</pubDate><guid>https://nidomiro.de/code/learning-rust/02-the-beginning/</guid><description>&lt;p>At first, I didn&amp;rsquo;t like Rust.
Managing memory myself is something I don&amp;rsquo;t want to do.
After a first look at Rust and setting it aside for a few weeks, it appeared back on my radar.
I thought: &amp;ldquo;This time I&amp;rsquo;m looking deeper into it&amp;rdquo;.
So I started by reading the &lt;a href="https://doc.rust-lang.org/book/">Rust Book&lt;/a>; and I&amp;rsquo;m still reading it and find new concepts that are really clever.&lt;/p>
&lt;p>One of the first things I discovered is that I do not need to manage memory myself.
The majority of the management will be done by the Rust compiler; I just have to follow some Rules.
These Rules, called &amp;ldquo;Ownership&amp;rdquo;, are a bit confusing to start with, but make more sense, the more you think about them, and the more you use them.
In combination with the strict differentiation between mutable and immutable structures, you can write code that is easy to read, easy to think about and resource efficient - a combination, I thought never possible.&lt;/p></description></item><item><title>Rust-01: Why am I learning Rust?</title><link>https://nidomiro.de/code/learning-rust/01-why-learning-rust/</link><pubDate>Tue, 01 Mar 2022 12:00:00 +0100</pubDate><guid>https://nidomiro.de/code/learning-rust/01-why-learning-rust/</guid><description>&lt;p>This is a good question, a very good question.
Why am I learning &lt;a href="https://www.rust-lang.org/">Rust&lt;/a>?&lt;/p>
&lt;p>To begin with, I programmed in a lot of languages so far: C++, Python, Swift, Java, Kotlin and TypeScript to name a few.
All languages have some features I missed when programming in other languages.&lt;/p>
&lt;p>Currently, my favorite languages are Kotlin and TypeScript; with Kotlin having the lead.
These languages are expressive, and really nice to write.
I can express my thoughts in code without having to convert them too much in order to make the compiler happy.&lt;/p></description></item></channel></rss>