Why raw pointer doesn't own type parameter T for dropcheck ?
This is a classic "confusion" with pointers. If I have a pointer (*mut T
in Rust or T *
in C++), do I own the data or do I just have a pointer to it? Since the drop checker is conservative, Rust assumes that you don't own the data and won't drop it.
This is safe; if raw pointers owned, the String
would drop twice (double free).
let s = "hi".to_string();
let r: *const String = &s;
Because it is designed not to own. If you need an owning pointer, use Box
.
This is like asking "why there is no chocolate mousse in this burger?". Chocolate mousse is delicious, but it does not belong in a burger. If you want chocolate mousse, then that's fine and you can choose to eat it instead of a burger. But at other times you may want a burger instead.
For another physical analogy: Dropping a raw pointer doesn't drop the pointee in the same way that getting rid of a domain name doesn't require going in and take a hammer to the web server. Someone else might still want to use it.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.