I have been learning about references in Rust and finding it hard to understand as on how exactly are references been stored in stack.
Consider the below program:
fn main() {
let name = String::from("Somu");
let name_ref = &name;
}
Now, How does memory is allocated in the stack frame for these 2 variables?
|--------------------|
| name | 0x1fcd |
|--------------------|
| name_ref | ? |
|--------------------|
So, since String are stored in heap, we have the address of heap where the string "Somu" is present as the value for the variable name
.
Now, name_ref
is a reference to name
. In Rust terms, name_ref
borrows the value pointed by name
.
So what get's stored in the stack frame as the value for name_ref
?
- Address to the heap memory containing the string?
- Address of
name
in stack?
Or something else?
Can someone please enrich me with the concept?
Thanks in advance