DEV Community

Cover image for Ownership in Rust

Ownership in Rust

Francesco Ciulla on February 20, 2024

Ownership in Rust Rust has a concept of ownership that is unique among programming languages. It is a key feature of the language that a...
Collapse
 
rdrpenguin04 profile image
Ray Redondo

There is a lot wrong with this as written.

  • Stack is not a type in Rust. The stack is an abstract place, but describing it as a type that can be used is unhelpful.
  • Likewise with Heap; calling it a type is misleading.
  • Copy objects can be put on the stack or the heap. For example, an array of 1024 u16 values can technically be put on the stack, but if you get too many of those, it'll overflow, so it's wiser to use the heap.
  • Move is not a trait! Moving is what happens when Copy is not implemented, but moving doesn't have a trait corresponding to it. What would such an implementation even look like? Or, what would happen if Move and Copy were implemented?
Collapse
 
francescoxx profile image
Francesco Ciulla

Hi Ray, and thanks for addressing these things. here is my answers to all of them:

  • Stack is definitively not a type, I couldn't find where I mention that Stack is a type in the article. Can you please point it out?
  • Same for the heap
  • you are correct, but this is an introductory article, and it's meant for beginners to understand the difference between the stack and the heap. I didn't mention Box, Vec in this article as it's a more advanced one.
  • you are right about calling "Move" a trait; it's more like a feature of the language
Collapse
 
rdrpenguin04 profile image
Ray Redondo

The main thing I noticed is that Stack and Heap are given styling as if they're types. The first thing I noticed was actually this quote:

It has some methods to manage the data, like push and pop, and it's very fast.

In reality, the stack is abstract and does not have methods. For someone who only knows Rust, saying it has methods is probably a bit confusing, but as someone who was taught Java first (which actually does have a Stack type) and as someone who knows Rust's VecDeque (something that has push and pop as I recall), the way the abstract stack was described seemed extremely close to describing an actual type with no clarification that it isn't.

Collapse
 
get_pieces profile image
Pieces 🌟

I learn more about Rust with each article, thank you. 😊

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome, @get_pieces