4 ms·
This claim is too strong, because Rust can't reduce heap allocations to stack allocations. Java, Swift, and others can optimize apparent heap allocations into s
by ridiculous_fish 8y ago
This claim is too strong, because Rust can't reduce heap allocations to stack allocations. Java, Swift, and others can optimize apparent heap allocations into stack allocations. But (in my understanding) Box, Rc, etc. can never allocate on the stack.
- kibwen 8y agoI'm confused; in Rust, you use Box when you want a value to escape the function. That's the whole point of Box! Likewise I can't think of any situation where you'd ever want to construct a value in an Rc without that value escaping somehow, because in a single scope you're better off just handing out shared references (I think you may be confusing Rc with RefCell, since they are often used together and RefCell can be useful within a single scope--but note that RefCell never allocates). You have no need to cleverly promote these things to the stack because nobody is using them for data that they don't want to live on the heap; you have to go out of your way to use them! :)
- pcwalton 8y agoThey could allocate on the stack; we just don't implement that optimization yet. That's because, as kibwen said, it would almost never kick in in practice.