<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>REBELSCIENCE</title><link>https://rebelscience.club/</link><description>Bioinformatics, Programming and Open-Source Science</description><item><title>From Python to Rust: Part 1.</title><link>https://rebelscience.club/2020/05/from-python-to-rust-part-1/</link><guid isPermaLink="true">https://rebelscience.club/2020/05/from-python-to-rust-part-1/</guid><pubDate>Sun, 17 May 2020 17:16:29 GMT</pubDate><description>In this series of articles, we will explore a young and very exciting programming language — Rust. We will see how Rust can help us
</description><content:encoded><![CDATA[
<p class="wp-block-paragraph">In this series of articles, we will explore a young and very exciting programming language — Rust. We will see how Rust can help us to speed up some of our existing and future Python code. Many agree, that the key features of Python are its simplicity and readability. Let’s try and see if we can have the same or similar simplicity and readability from Rust and get that incredible execution speed of a compiled language.</p>



<p class="wp-block-paragraph">While Python is an amazing, general purpose language and is super easy to write and understand, it might be too slow when we need to write an algorithm that needs to ‘crunch’ through a lot of data. Of course, well written Python code that uses something like NumPy or Cython, is still, very, very fast and probably enough in most cases. All the checks that are done by interpreted language like Python, will slow our code a lot.</p>



<p class="wp-block-paragraph">We will look at Python and Rust code side-by-side and attempt to port (rewrite) some of our Bioinformatics related code from Python to Rust while comparing code execution speed of both. We will see, that we don’t necessarily need/have to sacrifice simplicity of Python for Rust’s speed. And some functionality, written in Python, will stay in Python and just call our faster code, rewritten in Rust. This way, we will attempt to get a ‘sweet spot’ (best from both languages) to solve our Bioinformatics problems.</p>



<hr class="wp-block-separator has-text-color has-header-gradient-color has-css-opacity has-header-gradient-background-color has-background"/>



<h4 class="wp-block-heading">Prerequisites:</h4>



<ul class="wp-block-list">
<li>Good Python knowledge.</li>



<li>Basic Rust knowledge (I assume you are here because you know what you are doing) 😛</li>



<li>You understand the difference between compiled and interpreted language.</li>



<li>You understand the difference between statically and dynamically typed languages.</li>



<li>Code Editor with both, Python and Rust integrations.</li>
</ul>



<h4 class="wp-block-heading">Good to have:</h4>



<ul class="wp-block-list">
<li>Previous C-style (C-type?) language experience.</li>



<li>Desire to learn a new, exciting, compiled, memory safe and fast systems programming language.</li>
</ul>



<hr class="wp-block-separator has-text-color has-header-gradient-color has-css-opacity has-header-gradient-background-color has-background"/>



<p class="wp-block-paragraph">Make sure you have the latest Python version installed: <a href="https://www.python.org/downloads/" target="_blank" rel="noreferrer noopener">Link</a><br>Make sure you have the latest Rust/Cargo version installed: <a href="https://www.rust-lang.org/tools/install" target="_blank" rel="noreferrer noopener">Link</a></p>



<p class="wp-block-paragraph">We start by creating a folder, I called <code>py_rust</code>. In that, we create one folder &#8216;by hand&#8217;: <code>python</code> and add an empty file called <code>main.py</code>. This is where our Python code will live. For the Rust folder, we will use Rust&#8217;s project and package manager, <code>Cargo</code>. This will create a small project for us, with all the necessary files, needed to run and configure the Rust project. </p>



<p class="wp-block-paragraph">Start a prompt (bash/shell/cmd) in our newly created <code>py_rust</code> folder,&nbsp;which&nbsp;already contains Python and execute the following:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text/x-sh&quot;,&quot;theme&quot;:&quot;mbo&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Shell&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}">cargo new rust --bin</pre></div>


<p class="wp-block-paragraph">So parameters are:</p>



<p class="wp-block-paragraph"><code>new</code> &#8211; makes the cargo tool create a new project.<br><code>rust</code> &#8211; the name of the folder for the project.<br><code>--bin</code> &#8211; make the cargo tool create a project for a binary, not a library.<br><br>Now we should have the following structure:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;application/json&quot;,&quot;theme&quot;:&quot;mbo&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;JSON&quot;,&quot;language&quot;:&quot;JSON&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;json&quot;}">[py_rust]
├── python
│   └── main.py
└── rust
    ├── Cargo.lock
    ├── Cargo.toml
    ├── src
    │   └── main.rs
    └── target</pre></div>


<hr class="wp-block-separator has-text-color has-header-gradient-color has-css-opacity has-header-gradient-background-color has-background"/>



<p class="wp-block-paragraph">As Rust is a comparably young language, you might find tools and IDE integrations don&#8217;t work as well as say for Python, C++, Java. I am using VSCodium (VSCode) and it does have a good enough integration. Our goal here is to see two languages, Python and Rust, side-by-side, and be able to just use a keyboard shortcut to run both.</p>



<p class="wp-block-paragraph">Please make sure that you add Python and Rust project folders into our code editor one-by-one, and not the whole <code>py_rust</code> folder. This is because the Rust cargo system&nbsp;will be looking for a project source in a root directory, so if our root directory is <code>py_rust</code>, this will fail. Adding each folder separately will make sure that both, Python and Rust are executed <strong>not</strong> from <code>py_rust</code> but from their individual workspace. More details on that in my video version of this article.</p>



<p class="wp-block-paragraph">In the article <a rel="noreferrer noopener" href="https://rebelscience.club/2020/04/lets-set-up-a-code-editor-for-python-and-bioinformatics/" target="_blank">here</a>, we set up a <a href="http://vscodium.com" target="_blank" rel="noreferrer noopener">VSCodium</a> editor to be able to run Python as easy as possible, using just two plugins. If you have this configured already, you should, in theory, be able to run Rust exactly the same way. Make sure you have these two extensions installed:</p>



<figure class="wp-block-image size-full"><a href="https://rebelscience.club/wp-content/uploads/2020/05/816-795-max.png"><img decoding="async" width="816" height="795" src="https://rebelscience.club/wp-content/uploads/2020/05/816-795-max.png" alt="Rust Bioinformatics" class="wp-image-488" srcset="https://rebelscience.club/wp-content/uploads/2020/05/816-795-max.png 816w, https://rebelscience.club/wp-content/uploads/2020/05/816-795-max-296x288.png 296w, https://rebelscience.club/wp-content/uploads/2020/05/816-795-max-739x720.png 739w, https://rebelscience.club/wp-content/uploads/2020/05/816-795-max-768x748.png 768w" sizes="(max-width: 816px) 100vw, 816px" /></a><figcaption class="wp-element-caption">Click to enlarge/download</figcaption></figure>



<p class="wp-block-paragraph">The only thing we need to add is a <code>code runner</code> configuration for Rust.</p>



<p class="wp-block-paragraph">1. While you have <a href="http://vscodium.com" target="_blank" rel="noreferrer noopener">VSCodium</a> opened, hit <code>F1</code> and type <code>settings</code> and choose the first option:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://rebelscience.club/wp-content/uploads/2020/05/Screenshot_20200519_101928.png"><img decoding="async" width="776" height="103" src="https://rebelscience.club/wp-content/uploads/2020/05/Screenshot_20200519_101928.png" alt="" class="wp-image-487" srcset="https://rebelscience.club/wp-content/uploads/2020/05/Screenshot_20200519_101928.png 776w, https://rebelscience.club/wp-content/uploads/2020/05/Screenshot_20200519_101928-512x68.png 512w, https://rebelscience.club/wp-content/uploads/2020/05/Screenshot_20200519_101928-768x102.png 768w" sizes="(max-width: 776px) 100vw, 776px" /></a><figcaption class="wp-element-caption">Click to enlarge/download</figcaption></figure>
</div>


<p class="wp-block-paragraph">2. Add this line to your <code>settings.json</code> file:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;application/json&quot;,&quot;theme&quot;:&quot;mbo&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;wide&quot;,&quot;fileName&quot;:&quot;JSON&quot;,&quot;language&quot;:&quot;JSON&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;json&quot;}">&quot;code-runner.executorMap&quot;: {
	&quot;rust&quot;: &quot;if [ $(basename $dir) = 'examples' ]; then cargo run -q --example $fileNameWithoutExt; else cargo run -q; fi&quot;
}</pre></div>


<p class="wp-block-paragraph">Now we should be able to run both, Python and Rust with just a key combination (Ctrl + Alt + N).</p>



<hr class="wp-block-separator has-text-color has-header-gradient-color has-css-opacity has-header-gradient-background-color has-background"/>



<p class="wp-block-paragraph">Let&#8217;s add our first line of code and execute it:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">print(&quot;Hello, I am Python!&quot;)</pre></div>

<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;rust&quot;,&quot;mime&quot;:&quot;text/x-rustsrc&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Rust&quot;,&quot;language&quot;:&quot;Rust&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;rust&quot;}">fn main() {
    println!(&quot;Hello, I am Rust and I am fast&quot;);
}</pre></div>


<p class="wp-block-paragraph">And now we can try executing the code and if everything is configured correctly, we should see this:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://rebelscience.club/wp-content/uploads/2020/05/image.png"><img decoding="async" width="659" height="194" src="https://rebelscience.club/wp-content/uploads/2020/05/image.png" alt="" class="wp-image-494" srcset="https://rebelscience.club/wp-content/uploads/2020/05/image.png 659w, https://rebelscience.club/wp-content/uploads/2020/05/image-512x151.png 512w" sizes="(max-width: 659px) 100vw, 659px" /></a><figcaption class="wp-element-caption">Rust output.</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://rebelscience.club/wp-content/uploads/2020/05/image-1.png"><img decoding="async" width="659" height="194" src="https://rebelscience.club/wp-content/uploads/2020/05/image-1.png" alt="" class="wp-image-495" srcset="https://rebelscience.club/wp-content/uploads/2020/05/image-1.png 659w, https://rebelscience.club/wp-content/uploads/2020/05/image-1-512x151.png 512w" sizes="(max-width: 659px) 100vw, 659px" /></a><figcaption class="wp-element-caption">Python output.</figcaption></figure>
</div>


<p class="wp-block-paragraph">And here is how our setup should look like:</p>



<figure class="wp-block-image alignwide size-full"><a href="https://rebelscience.club/wp-content/uploads/2020/05/image-2.png"><img decoding="async" width="1860" height="1057" src="https://rebelscience.club/wp-content/uploads/2020/05/image-2.png" alt="Rust Bioinformatics" class="wp-image-496" srcset="https://rebelscience.club/wp-content/uploads/2020/05/image-2.png 1860w, https://rebelscience.club/wp-content/uploads/2020/05/image-2-507x288.png 507w, https://rebelscience.club/wp-content/uploads/2020/05/image-2-1267x720.png 1267w, https://rebelscience.club/wp-content/uploads/2020/05/image-2-768x436.png 768w, https://rebelscience.club/wp-content/uploads/2020/05/image-2-1536x873.png 1536w" sizes="(max-width: 1860px) 100vw, 1860px" /></a><figcaption class="wp-element-caption">Project view. We can click-in to opened Python or Rust files and use (Ctrl + Alt + N) to execute each.</figcaption></figure>



<hr class="wp-block-separator has-text-color has-header-gradient-color has-css-opacity has-header-gradient-background-color has-background"/>



<p class="wp-block-paragraph">Now let&#8217;s start looking at the actual code.</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">x = 100.0
y = 1.0

print(f&quot;x: {x}, and y: {y}&quot;)
y = y * -0.3145
print(f&quot;x: {x}, and y: {y}&quot;)</pre></div>

<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;rust&quot;,&quot;mime&quot;:&quot;text/x-rustsrc&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Rust&quot;,&quot;language&quot;:&quot;Rust&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;rust&quot;}">let x = 100.0;
let mut y = 1.0;

println!(&quot;x: {}, and y: {}&quot;, x, y);
y = y * -0.3145;
println!(&quot;x: {}, and y: {}&quot;, x, y);</pre></div>


<p class="wp-block-paragraph"><strong>Note</strong>: for Rust, we need to remember that all the code is wrapped in the <code>main() {}</code> function.</p>



<p class="wp-block-paragraph">We can see, that the code is very similar. Print statements differ just a bit, but easy to get used to. In Python, we use f-strings. But the difference, of course, is in how we declare variables. This is why Rust is statically typed. We need to tell it what type our variable is before we use it. The concept of mutable/immutable is present in both languages, but in Rust we need to specify <code>mut</code> if we plan to change the value of the variable somewhere in the code. So on the line <code>5</code> we modify the variable <code>y</code> and that is why we need it to be mutable. If we don&#8217;t declare it as <code>let mut</code> and try executing the code, we will see the beauty of Rust&#8217;s compiler:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;rust&quot;,&quot;mime&quot;:&quot;text/x-rustsrc&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;wide&quot;,&quot;fileName&quot;:&quot;Rust&quot;,&quot;language&quot;:&quot;Rust&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;rust&quot;}">error[E0384]: cannot assign twice to immutable variable `y`
 --&gt; src/main.rs:8:5
  |
5 |     let y = 1.0;
  |         -
  |         |
  |         first assignment to `y`
  |         help: make this binding mutable: `mut y`
...
8 |     y = y * -0.3145;
  |     ^^^^^^^^^^^^^^^ cannot assign twice to immutable variable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0384`.
error: could not compile `rust`.

To learn more, run the command again with --verbose.</pre></div>


<p class="wp-block-paragraph">We can see, that Rust compiler tells us&nbsp;<strong>exactly&nbsp;</strong>what is wrong. I wish all languages had such a helpful output.</p>



<hr class="wp-block-separator has-text-color has-header-gradient-color has-css-opacity has-header-gradient-background-color has-background"/>



<p class="wp-block-paragraph">Let’s see how&nbsp;<code>if</code>&nbsp;and&nbsp;<code>else</code>&nbsp;look like in both:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">if y &lt; x:
    print(f&quot;The difference is: {x-y}&quot;)
elif y == x:
    print(f&quot;The difference is: {x-y}&quot;)
else:
    print(f&quot;The difference is: {y-x}&quot;)</pre></div>

<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;rust&quot;,&quot;mime&quot;:&quot;text/x-rustsrc&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Rust&quot;,&quot;language&quot;:&quot;Rust&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;rust&quot;}">if y &lt; x {
    println!(&quot;The difference is: {}&quot;, (x - y));
} else if y == x {
    println!(&quot;The difference is: {}&quot;, (y - x));
} else {
    println!(&quot;The difference is: {}&quot;, (y - x));
}</pre></div>


<p class="wp-block-paragraph">We can see that the code is almost identical. C-style language programmers probably will be tempted to wrap <code>y &lt; x</code> in <code>(y &lt; x)</code>. Let&#8217;s try doing that and see what Rust will tell us.</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;rust&quot;,&quot;mime&quot;:&quot;text/x-rustsrc&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;wide&quot;,&quot;fileName&quot;:&quot;Rust&quot;,&quot;language&quot;:&quot;Rust&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;rust&quot;}">warning: unnecessary parentheses around `if` condition
  --&gt; src/main.rs:10:8
   |
10 |     if (y &lt; x) {
   |        ^^^^^^^ help: remove these parentheses
   |
   = note: `#[warn(unused_parens)]` on by default</pre></div>


<p class="wp-block-paragraph">Well, what can I say? Beautiful! Learning Rust with such amazing help from the compiler is pure joy.</p>



<hr class="wp-block-separator has-text-color has-header-gradient-color has-css-opacity has-header-gradient-background-color has-background"/>



<p class="wp-block-paragraph">Loops will look very similar:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">x = 5
while x &gt; 0:
    print(f&quot;x is {x}&quot;)
    x -= 1

for i in range(3, 7):
    print(f&quot;i is {i}&quot;)</pre></div>

<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;rust&quot;,&quot;mime&quot;:&quot;text/x-rustsrc&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Rust&quot;,&quot;language&quot;:&quot;Rust&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;rust&quot;}">let mut x = 5;
while x &gt; 0 {
    println!(&quot;x is {}&quot;, x);
    x -= 1;
}

// Version 1 (Very Pythony):
for i in 3..7 {
    println!(&quot;i is {}&quot;, i);
}

// Version 2 (If you need to create a range on the fly):
let num_range = 3..7;
for i in num_range {
    println!(&quot;i is {}&quot;, i);
}</pre></div>


<p class="wp-block-paragraph">While/For loops and range usage are almost identical. The only difference is if you want to re-use the <code>x</code> variable in Python, you just overwrite it. This is where we can see that Python is <strong>not</strong>, and Rust <strong>is</strong> type-safe and does not allow you to just re-use a variable and requires something called <code>variable shadowing</code> on the line <code>1</code>. This is needed to protect <strong>you</strong> from writing broken code. Originally, we declared <code>x = 100.0</code>, and Rust interpreted it as a <code>float</code>. Now, if we just try overwriting it as <code>x = 5;</code> Rust will tell us that the original variable was <code>float</code> and now you are trying to assign an <code>integer</code> to it. Try doing that yourself and see what Rust compiler will tell you.</p>



<hr class="wp-block-separator has-text-color has-header-gradient-color has-css-opacity has-header-gradient-background-color has-background"/>



<p class="wp-block-paragraph">Let&#8217;s take a look at a List/Vector and how we can get an iterator and enumerator out of both:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">tmp_lst = ['DNA', 'RNA', 'mRNA']

for value in tmp_lst:
    print(f&quot;value is {value}&quot;)</pre></div>

<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;rust&quot;,&quot;mime&quot;:&quot;text/x-rustsrc&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Rust&quot;,&quot;language&quot;:&quot;Rust&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;rust&quot;}">let tmp_vec = vec![&quot;DNA&quot;, &quot;RNA&quot;, &quot;mRNA&quot;];

for value in tmp_vec {
    println!(&quot;value is {}&quot;, value);
}</pre></div>


<p class="wp-block-paragraph">This is easy. What in Python is a List, is a Vector in Rust. We should see this output from both:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;monokai&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">value is DNA
value is RNA
value is mRNA</pre></div>


<p class="wp-block-paragraph">Now, if Python will allow accessing/modifying our List after we access it in a For loop, Rust will not allow us to do the same to its Vector. After both loops, try acceding/printing just the first element of a List/Vector: like this:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;rust&quot;,&quot;mime&quot;:&quot;text/x-rustsrc&quot;,&quot;theme&quot;:&quot;monokai&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Rust&quot;,&quot;language&quot;:&quot;Rust&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;rust&quot;}"># Python:
print(tmp_lst[0])

// Rust:
println!(&quot;{}&quot;, tmp_vec[0]);</pre></div>


<p class="wp-block-paragraph">And now Rust will introduce us to yet another concept of <a rel="noreferrer noopener" href="https://dev.to/itnext/rust-ownership-and-borrowing-i2n" target="_blank">Ownership and Borrowing</a>, also (<a rel="noreferrer noopener" href="https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html" target="_blank">here</a>)</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;rust&quot;,&quot;mime&quot;:&quot;text/x-rustsrc&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;wide&quot;,&quot;fileName&quot;:&quot;Rust&quot;,&quot;language&quot;:&quot;Rust&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;rust&quot;}">error[E0382]: borrow of moved value: `tmp_vec`
  --&gt; src/main.rs:34:20
   |
26 |     let tmp_vec = vec![&quot;DNA&quot;, &quot;RNA&quot;, &quot;mRNA&quot;];
   |         ------- move occurs because `tmp_vec` has type `std::vec::Vec&lt;&amp;str&gt;`,
   | 	                                 which does not implement the `Copy` trait
27 |     for value in tmp_vec {
   |                  -------
   |                  |
   |                  value moved here
   |                  help: consider borrowing to avoid moving into the for loop: `&amp;tmp_vec`
...
34 |     println!(&quot;{}&quot;, tmp_vec[0]);
   |                    ^^^^^^^ value borrowed here after move</pre></div>


<p class="wp-block-paragraph">If you never heard about this Rust concept, you should invest your time in reading about it. Basically, after we provided our <code>tmp_vec</code> Vector to a For loop in Rust, we gave the <code>ownership</code> of our <code>tmp_vec</code> Vector to that For Loop. For loop did what we asked it to do, which is to print the contents of that Vector, and after that, <code>tmp_vec</code> was discarded as our For loop <code>owned</code> it. After the For loop was done and out of scope, everything in the scope of that For loop is now gone too.</p>



<p class="wp-block-paragraph">This is easily &#8216;fixed&#8217; by &#8216;asking&#8217; our <code>tmp_vec</code> to return just the iterator, and not itself: <code>tmp_vec.iter()</code>, and now line 7 will not produce an error:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;rust&quot;,&quot;mime&quot;:&quot;text/x-rustsrc&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Rust&quot;,&quot;language&quot;:&quot;Rust&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;rust&quot;}">let tmp_vec = vec![&quot;DNA&quot;, &quot;RNA&quot;, &quot;mRNA&quot;];

for value in tmp_vec.iter() {
    println!(&quot;value is {}&quot;, value);
}

println!(&quot;{}&quot;, tmp_vec[0]);</pre></div>


<hr class="wp-block-separator has-text-color has-header-gradient-color has-css-opacity has-header-gradient-background-color has-background"/>



<p class="wp-block-paragraph">And now let&#8217;s add an enumerator to our For loop:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">tmp_lst = ['DNA', 'RNA', 'mRNA']

for pos, value in enumerate(tmp_lst):
    print(f&quot;value at pos {pos} is {value}&quot;)</pre></div>

<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;rust&quot;,&quot;mime&quot;:&quot;text/x-rustsrc&quot;,&quot;theme&quot;:&quot;gruvbox-dark&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Rust&quot;,&quot;language&quot;:&quot;Rust&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;rust&quot;}">let tmp_vec = vec![&quot;DNA&quot;, &quot;RNA&quot;, &quot;mRNA&quot;];

for (pos, value) in tmp_vec.iter().enumerate() {
    println!(&quot;value at pos {} is {}&quot;, pos, value);
}</pre></div>


<p class="wp-block-paragraph">Almost identical. Alright. This is it for this article. See you in the next one soon.</p>



<p class="wp-block-paragraph">All the code is available her: <a href="https://github.com/rebelC0der/From_Python_To_Rust">https://github.com/rebelC0der/From_Python_To_Rust</a></p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="From Python to Rust. Part 1" width="640" height="360" src="https://www.youtube.com/embed/Ygk0IMbu2nY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div><figcaption class="wp-element-caption">Video version of this article.</figcaption></figure>
]]></content:encoded></item></channel></rss>