How did the String Type in Rust define?
There are two type of string types in Rust. Once is &str and other one is “String”.
String is heap allocated, grow able and not null termiated type.
&str is a slice(&[u8] ) that always points to a UTF8 valid sequence.
learn more about type usage here String types
But we are trying to focus on “String” type and how do we search Rust source code to see how this type is implemented.
String type - Heap allocation
Heap allocated memory implies that there will be pointer behind the definition of this type.
Let’s see,
We can get the rust source from below link.
https://doc.rust-lang.org/std/string/struct.String.html
click on the src button, as highlighted in the link.
it will directly take you to the surprising fact that String is simply a vector ( vec ).
String is a Vec<u8>
We know that Vec is also a heap allocated memory. In order to find how “vec” is defined in the source ,we need to search keyword “Vec” in the source code.
click on “src”
Interestingly, you will see the code.
pub struct Vec<T> {
buf: RawVec<T>,
len: usize,
}
I haven’t heard about RawVec earlier, but this is coming from : crate::raw_vec::RawVec;
raw-vec is implemeted for handling contiguous heap allocations.
Rawvec is implemented as below using a Unique pointer.
As guessed , we come to know that String type is actually using a pointer.
But what is unique pointer?
It comes from use core::ptr::Unique;
Unique is wapper around a raw non nullable ‘*mut T’.
interesting …
NonZero is wrapper type for raw pointers and integers that never becomes zero or null .