Focus mode

Rust Fundamentals

Fundamental Types:Slices

Since having a memory management feature in Rust, there are 2 separate string types, one of which takes the letters as a vector and the other as a whole. In this way, we have the chance to manage the modifiable and non-modifiable partitions.

  • String is stored as a vector of bytes, it is always valid UTF-8 sequence. A String's size can be known or unknown at compilation time during initialization, but it can expand as the String's length exceeds its maximum. Strings have ownership. 
  • Slices allow you to refer to a continuous sequence of elements in a collection instead of the entire collection. Because a slice is a kind of reference, it does not have ownership. 

In performance-oriented languages, memory usage is minimized by reducing unnecessary storage. Data types such as String cause unnecessary memory usage as they freely use the Heap structure in all languages. For this reason, it is preferred to keep the events in the stack in rust and Heap is used when really necessary.

We can use &str to point to a specific part of an existing String instead of creating new String structures standing in the Heap. In this way, it does not require any allocation at runtime, and fixed-length &str cannot be resized.

You can find detailed further information from Rust Official documentation:

https://doc.rust-lang.org/book/ch04-03-slices.html 

Resources:

https://www.tutorialspoint.com/rust/rust_slices.htm 

https://www.educative.io/answers/what-is-the-slice-type-in-rust 

https://blog.logrocket.com/understanding-rust-string-str/ 

https://www.becomebetterprogrammer.com/rust-string-vs-str/

left-disk

Programs to Accelerate Your Progress in a Software Career

Join our 4-8 month intensive Patika+ bootcamps, start with the fundamentals and gain comprehensive knowledge to kickstart your software career!

right-cube

Test

Comments

You need to enroll in the course to be able to comment!