<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rust Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/rust/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/rust/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Sat, 05 Nov 2022 08:05:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>
	<item>
		<title>Code Kata: Christmas Tree</title>
		<link>https://creatronix.de/code-kata-christmas-tree/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Sat, 24 Dec 2016 21:32:00 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[roman numeral]]></category>
		<category><![CDATA[rust]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=3</guid>

					<description><![CDATA[<p>Inspired by a blog post of Dave Thomas I started to implement my technology learning roadmap by writing little code katas. To kill two birds with one stone I first solve a programming puzzle with my lingua franca Python. That helped me to concentrate on solving the algorithmic part of the puzzle because I don&#8217;t&#8230;</p>
<p>The post <a href="https://creatronix.de/code-kata-christmas-tree/">Code Kata: Christmas Tree</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Inspired by a <a href="http://codekata.com/kata/codekata-how-it-started/">blog post</a> of Dave Thomas I started to implement my technology learning roadmap by writing little code katas.</p>
<p>To kill two birds with one stone I first solve a programming puzzle with my lingua franca Python. That helped me to concentrate on solving the algorithmic part of the puzzle because I don&#8217;t have to constantly worry about syntax and semantics.</p>
<p>Then I did the whole thing again this time in the language I want to learn: Rust</p>
<p>To give You an example how that works I will elaborate on the Christmas tree kata. <span id="more-3"></span> Ok, what is the Christmas tree? It&#8217;s a neat little programming exercise where You have to print a Christmas tree on the console.</p>
<p>e.g. a tree with height 2 looks like this:</p>
<pre><code> X
XXX
 |
</code></pre>
<p>and a tree with height 3 looks like this:</p>
<pre><code>  X
 XXX
XXXXX
  |
</code></pre>
<p>How do You solve this? First of all You have to understand how many whitespaces and &#8220;X&#8221;s You will find in each line depending on the level.<br />
So for a tree with height 2 there is one whitespace and one X in the first and no whitespace and 3 &#8220;X&#8221; in the second row and two whitespaces and one &#8220;|&#8221; in the third line.</p>
<p>Let&#8217;s make a little table to see where this is going:</p>
<pre>row  WS   X   |
0    1    1   0
1    0    3   0
2    1    0   1
</pre>
<p>Same thing again for height 3</p>
<pre>row  WS   X   |
0    2    1   0
1    1    3   0
2    0    5   0
3    2    0   1
</pre>
<p>I hope it becomes quite easy to see how the characters have to be printed out:<br />
for whitespaces it is &#8220;height &#8211; current_row &#8211; 1&#8221;,<br />
for &#8220;X&#8221; it is &#8220;2 * current_row + 1&#8221;<br />
and the last line is always &#8220;height &#8211; 1&#8221; whitespaces plus the &#8220;|&#8221; character.</p>
<p>In Python it looks like this:</p>
<pre><code>height = 3
for line in range(height):
    print (" " * (height-line-1)) + "X" * (2*line+1)
print (" " * (height-1)) + "|"
</code></pre>
<p>And now for something completely different: Rust<br />
Rust is a strongly and statically typed language, so there is a bit more effort in writing this Kata down. It looks like this:</p>
<pre><code>
fn draw_christmas_tree(height: i32){
    let mut val: String = String::new();
    for i in 0..height {
        for j in 0..height-i{
            val.push_str(" ");
        }
         for k in 0..(2*i+1){
            val.push_str("X");
        }
        val.push_str("\n");
    }
    for j in 0..height{
        val.push_str(" ");
    }
    val.push_str("|");
    println!("{}", val);

}
fn main() {
    draw_christmas_tree(3);
}
</code></pre>
<p>Because in Rust You don&#8217;t have the smooth &#8220;repeat a string n times&#8221; syntax You have to iterate more often. I also tried to append the characters to a string buffer before printing them out so it looks a bit more different then the solution in python.</p>
<p>The post <a href="https://creatronix.de/code-kata-christmas-tree/">Code Kata: Christmas Tree</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
