#References question

15 messages · Page 1 of 1 (latest)

north osprey
#

&String is a reference to a pointer to text data on the heap.
&std is a reference to text data.

tacit zealot
#

&String
is

& ---> String
       ptr ----> actual data
       len
       cap

where &str is

&str
 ptr ---> actual data
 len
hasty path
#

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.

hasty path
tacit zealot
#

(note that &str doesnt necessarily mean its pointing to the heap)

hasty path
onyx wing
#

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.

tacit zealot
hasty path
onyx wing
#

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"

hasty path
onyx wing
#

The book calls "wrappers around pointers with Deref implementations" smart pointers, but I don't personally see that terminology used much.

tacit zealot
#

to be pedantic, String is just a wrapper to Vec<u8>. you can actually check the source code for this

hasty path