#References question
15 messages · Page 1 of 1 (latest)
&String
is
& ---> String
ptr ----> actual data
len
cap
where &str is
&str
ptr ---> actual data
len
ok that makes sense. so println!("{:p}", &q) is giving me the address of the String object (is that the word for it?). That itself contains the ptr and metadata.
while &str is the address of the str data on the heap.
(note that &str doesnt necessarily mean its pointing to the heap)
you mean in the case of a static str?
Nope, &str is the address of str data somewhere. In the case of a literal it will be in static memory, in the case of an ArrayString or std::str::from_utf8 it could be on the stack, or in the case of String::as_str it would be on the heap.
there's also the case of &str pointing to the stack
ok nice I will read into stack allocated str. Btw is it correct to call String, the thing that contains the ptr + other data as an object? Or does it have a more technical name in Rust
Usually you can just talk about types and values in Rust. "object" isn't a term used much.
so "String::new() returns a value of type String"
Is it correct to see String as like a wrapper or container over a pointer to the heap and some other data? I vaguely remember that terminology from somewhere.
The book calls "wrappers around pointers with Deref implementations" smart pointers, but I don't personally see that terminology used much.
to be pedantic, String is just a wrapper to Vec<u8>. you can actually check the source code for this
that makes sense since vec has a ptr, cap, len.