avatarAnkit Tanna

Summary

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

1362

Abstract

lt;<span class="hljs-type">i32</span>> = <span class="hljs-built_in">vec!</span>[<span class="hljs-number">2001</span>, <span class="hljs-number">2002</span>, <span class="hljs-number">2003</span>, <span class="hljs-number">2004</span>]; <span class="hljs-keyword">let</span> <span class="hljs-variable">slice</span> = &nums[<span class="hljs-number">0</span>..<span class="hljs-number">2</span>]; }</pre></div><p id="a718">The only difference between the metadata of a Vector and a Slice is slice does not have <code>capacity</code>. Slices can be a really nice way of referencing same piece of memory from heap and creating a new memory in the stack. We can make slices of Vectors or even strings if you a subset of chars. Slice does not own the elements, it just references them:</p><figure id="58a6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*6xs-HS4rSFutd2VNE8nHoQ.png"><figcaption>Slice references the memory in heap</figcaption></figure><p id="3e4f">The type of years is <code>Vec<i32></code> where as the type of slice is <code>&[i32]</code>. You can slice the strings as well.</p><div id="9b21"><pre><span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">years</span>: <span class="hljs-type">Vec</span>&lt

Options

;<span class="hljs-type">i32</span>> = <span class="hljs-built_in">vec!</span>[<span class="hljs-number">2001</span>, <span class="hljs-number">2002</span>, <span class="hljs-number">2003</span>, <span class="hljs-number">2004</span>]; <span class="hljs-keyword">let</span> <span class="hljs-variable">slice</span> = &nums[<span class="hljs-number">0</span>..<span class="hljs-number">2</span>]; <span class="hljs-keyword">let</span> <span class="hljs-variable">name</span> = <span class="hljs-string">"hello world!"</span>; <span class="hljs-keyword">let</span> <span class="hljs-variable">name_slice</span>: &<span class="hljs-type">str</span> = name[<span class="hljs-number">0</span>..<span class="hljs-number">2</span>]; }</pre></div><p id="d241">You can also get all the elements of a Vector or a string as a slice by using <code>.as_slice()</code> or <code>.as_str()</code> method.</p><p id="a8cd">I hope you enjoyed this article about the Slices on Vectors and Strings in Rust.</p><p id="ca37">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 — References and Borrowing — Slices

Grab a slice for yourself!

You can define a vector quite easily in Rust. But what if you want to reference just a portion of the vector without having to create a new copy of it? Slices come to the rescue. Slices are nothing but the references of a portion of a vector. Just like VectMetadata {} we get StructMetadata {}.

struct VectMetadata {
  first_elem_index: usize,
  length: usize,
  capacity: usize,
}

struct SliceMetadata {
  first_elem_index: usize,
  length: usize,
}
fn main() {
    let years: Vec<i32> = vec![2001, 2002, 2003, 2004];
    let slice = &nums[0..2];
}

The only difference between the metadata of a Vector and a Slice is slice does not have capacity. Slices can be a really nice way of referencing same piece of memory from heap and creating a new memory in the stack. We can make slices of Vectors or even strings if you a subset of chars. Slice does not own the elements, it just references them:

Slice references the memory in heap

The type of years is Vec<i32> where as the type of slice is &[i32]. You can slice the strings as well.

fn main() {
    let years: Vec<i32> = vec![2001, 2002, 2003, 2004];
    let slice = &nums[0..2];
    let name = "hello world!";
    let name_slice: &str = name[0..2];
}

You can also get all the elements of a Vector or a string as a slice by using .as_slice() or .as_str() method.

I hope you enjoyed this article about the Slices on Vectors and Strings in Rust.

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
Programming
Recommended from ReadMedium