let temp = String::from("The black cat");
println!("p: {:p} ptr: {:?} ", &temp, temp.as_ptr());
Do I understand correctly that in the above code {:p} will print the address of the variable and as_ptr() returns the address of the payload, the actual content of this variable?
fn main() {
let temp = String::from("The black cat");
let x = &temp;
let addr_of_temp = std::ptr::addr_of!(temp);
let y = temp.as_ptr();
let x_deref = &**x as *const str;
println!("p: {x:p} addr_of_temp: {addr_of_temp:?} ptr: {y:?} x_deref: {x_deref:?}");
}
String is just a vector with valid utf-8 bytes. The bytes are stored on the heap in some contiguous slice of memory. The String instance is a fat pointer consisting of a pointer to the bytes, together with the length and capacity. When we call String::as_ptr, we get the address of the bytes on the heap. When we print the address of a reference to the String instance, we get the address of the fat pointer.
Did you put in the love or do you use a tool to create your ASCII diagrams? Also, if anyone has an idea how boats makes the diagrams in their blog, I'd love to know, they are super fancy.
Well yes, one could use a graphical editor for that, but I can't. They make me anxious . I was hoping for something like mermaid.js or tikz, some sort of declarative markup language with which I can define the graph that is rendered automatically as an ASCII diagram.