avatarAnkit Tanna

Summary

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2168

Abstract

.<span class="hljs-title function_ invoke__">push</span>(<span class="hljs-number">2006</span>); <span class="hljs-built_in">println!</span>(<span class="hljs-string">"The number of years are {}"</span>, years.<span class="hljs-title function_ invoke__">len</span>()); years.<span class="hljs-title function_ invoke__">push</span>(<span class="hljs-number">2007</span>); <span class="hljs-built_in">println!</span>(<span class="hljs-string">"The number of years are {}"</span>, years.<span class="hljs-title function_ invoke__">len</span>());

<span class="hljs-keyword">let</span> <span class="hljs-variable">number_of_years</span>: <span class="hljs-type">usize</span> = years.<span class="hljs-title function_ invoke__">len</span>(); <span class="hljs-comment">// usize = u32 or u64 depending on the system</span>

<span class="hljs-keyword">let</span> <span class="hljs-variable">first_year_index</span>: <span class="hljs-type">usize</span> = <span class="hljs-number">0</span>;
<span class="hljs-keyword">let</span> <span class="hljs-variable">first_year</span>: <span class="hljs-type">i32</span> = years[first_year_index];
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"The first year is {}"</span>, first_year);

<span class="hljs-keyword">let</span> <span class="hljs-keyword">mut </span><span class="hljs-variable">nums</span>: [<span class="hljs-type">u32</span>; <span class="hljs-number">3</span>] = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];
<span class="hljs-keyword">let</span> <span class="hljs-keyword">mut </span><span class="hljs-variable">nums_vec</span>: <span class="hljs-type">Vec</span>&lt;<span class="hljs-type">u32</span>&gt; = <span class="hljs-built_in">vec!</span>[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];

<span class="hljs-comment">// nums.push(4); &lt;-- this will panic</span>

<span class="hljs-keyword">for</span> <span class="hljs-variable">year</span> <span class="hljs-keyword">in</span> years.<span class="hljs-title fu

Options

nction_ invoke__">iter</span>() { <span class="hljs-built_in">println!</span>(<span class="hljs-string">"The year is {}"</span>, year); } }</pre></div><h2 id="d59d">Observations:</h2><ol><li>Rust has a type called <code>Vec</code>. <code>Vec</code> stands for Vector.</li><li>Vec uses a <code>type parameter <i32></code>. This indicates that all the elements inside the vector would be of type <code>i32</code>.</li><li>In order to create a vector array, we use a macro <code>vec![]</code>. Some macros are initiated using parenthisis <code>()</code> indicating that they are a function call. e.g. <code>println!()</code>, <code>format!()</code> and <code>panic!()</code>. Vectors are initiated using square brackets <code>[]</code>. e.g. <code>vec![]</code> to indicate that they are an array.</li><li>You can do operations like <code>.push()</code>, <code>.pop()</code> and <code>.len()</code> on a vector which does just as what it describes. But for doing <code>.push()</code> and <code>.pop()</code> the vector must be mutable. Hence, we’ve added <code>mut</code> keyword.</li><li>Lust like Arrays, Vectors can be iterated over using <code>.iter()</code> keyword.</li></ol><p id="b3bf">We might have question in our mind that why not use vectors always rather than array? But it is important to understand how these two are fundamentally different in the usage of memory. Arrays use the stack memory because they are of “finite”or “known” length. Vectors, on the other hand, are not of “finite” or “known” length. Hence, they cannot use the stack memory. They need to use something called heap memory.</p><p id="d812">We’ll discuss about the stack memory vs heap memory in the next article. I hope you liked this article about Rust Vectors. Please share your feedback in the comments section.</p><p id="a1f3">You can subscribe to my newsletter about <b>The Rust Programming Language <a href="https://tinyletter.com/ankittanna"></a></b><a href="https://tinyletter.com/ankittanna">here</a>. You can read about all the articles in this series <a href="https://readmedium.com/the-rust-programming-language-4b22bc717ecc">here</a>.</p></article></body>

The Rust Programming Language

The Rust Programming Language — Vectors — Vectors

Arrays of Rust on Steroids!

We’ve already learnt about arrays in Rust here. Arrays in Rust can be declared like below:

fn main() {
    let mut years: [i32; 3] = [2021, 2022, 2023];
}

These arrays are a fixed length arrays, even though we’ve added mut keyword. mut keyword allows you to modify the values inside the array i.e. 2021, 2022 and 2023 but does not allow you to .push() or .pop() any elements from the array. The length of the array remains the same throughout the program’s lifecycle.

But, just as other programming languages, we do have need of flexible size arrays. Vector comes to the rescue. Vectors are array but with flexibility of size during program’s runtime.

Let’s have a look at below code snippet:

fn main() {
    let mut years: Vec<i32> = vec![2001, 2002, 2003, 2004, 2005];
    years.push(2006);
    println!("The number of years are {}", years.len());
    years.push(2007);
    println!("The number of years are {}", years.len());

    let number_of_years: usize = years.len(); // usize = u32 or u64 depending on the system
    
    let first_year_index: usize = 0;
    let first_year: i32 = years[first_year_index];
    println!("The first year is {}", first_year);

    let mut nums: [u32; 3] = [1, 2, 3];
    let mut nums_vec: Vec<u32> = vec![1, 2, 3];

    // nums.push(4); <-- this will panic

    for year in years.iter() {
        println!("The year is {}", year);
    }
}

Observations:

  1. Rust has a type called Vec. Vec stands for Vector.
  2. Vec uses a type parameter <i32>. This indicates that all the elements inside the vector would be of type i32.
  3. In order to create a vector array, we use a macro vec![]. Some macros are initiated using parenthisis () indicating that they are a function call. e.g. println!(), format!() and panic!(). Vectors are initiated using square brackets []. e.g. vec![] to indicate that they are an array.
  4. You can do operations like .push(), .pop() and .len() on a vector which does just as what it describes. But for doing .push() and .pop() the vector must be mutable. Hence, we’ve added mut keyword.
  5. Lust like Arrays, Vectors can be iterated over using .iter() keyword.

We might have question in our mind that why not use vectors always rather than array? But it is important to understand how these two are fundamentally different in the usage of memory. Arrays use the stack memory because they are of “finite”or “known” length. Vectors, on the other hand, are not of “finite” or “known” length. Hence, they cannot use the stack memory. They need to use something called heap memory.

We’ll discuss about the stack memory vs heap memory in the next article. I hope you liked this article about Rust Vectors. Please share your feedback in the comments section.

You can subscribe to my newsletter about The Rust Programming Language here. You can read about all the articles in this series here.

Rust
Rust Programming Language
Webassembly
Performance
Web Development
Recommended from ReadMedium