Coming from C++, thought that understanding of pointers would be an easy one. Trying to understand the ownership concept using String type with the following code snippet. Irrespective of the deliberate ownership change from "s" -> "w" the pointer value has to be pointing to the memory address of "H". Accordingly, the as_prt() method is returning the raw pointer to "H" but the same is not true when s_ptr and w_ptr are used. I'm assuming that the s_ptr and w_ptr are the pointers of "s" and "w" strings data (pointer, length, and capacity) respectively located on the stack. Correct me if I'm wrong, else please let me know why the pointers (s_ptr and w_ptr) are different.
fn main(){
let s = String::from("Hello, world!");
let s_ptr:&String = &s;
println!("Pointer of {} is {:p}", s, s_ptr);
println!("Pointer of {} is {:p}", s, s.as_ptr());
println!("Size = {}", get_string_length(s));
}
fn get_string_length(w: String) -> usize{
let w_ptr:&String = &w;
println!("Pointer of {} is {:p}", w, w_ptr);
println!("Pointer of {} is {:p}", w, w.as_ptr());
w.len()
Here, the local variable s is moved into the w parameter of get_string_length; that is, a new variable is created. However, moving steals content from the source String; the same heap memory gets pointed to by the new variable. Since you are from C++, this might help: (I used std::vector rather than std::string, because the latter has small string optimization)
#include <cstddef>
#include <iostream>
#include <utility>
#include <vector>
std::size_t get_length(std::vector<int> w) {
auto p = &w;
std::cout << p << '\n';
std::cout << w.data() << '\n';
return w.size();
}
int main() {
std::vector<int> s{1, 2, 3};
auto p = &s;
std::cout << p << '\n';
std::cout << s.data() << '\n';
get_length(std::move(s));
}
If the variable s is defined as, for instance, let s = String::from("foo"); then &s is the address of the variable s itself, i.e. it points to s, whereas s.as_ptr() points to the beginning of the underlying, dynamically-allocated buffer of bytes. These are of course different addresses — Strings do not store their content inline.