|
|
|
|
|
by pornel
1577 days ago
|
|
Rust is strict about ownership, which doesn't have equivalent in other popular languages. Users coming from GC languages are surprised Rust can't make things "live long enough". Users coming from OOP languages are surprised that Rust has on-stack objects without a layer of indirection. Users coming from C are surprised that references aren't like pointers. So I think it would be interesting to teach users Rust's point of view first, before they learn the "misconceptions" from other languages. But in practice something like JS is better, because students can make something engaging appear on screen before they lose patience. |
|
If you really need a general 'long enough' strategy and can't just pick a sensible place to drop the object statically, that's what Rc<> and Arc<> provide - built from the ground up via refcounting.
> Users coming from C are surprised that references aren't like pointers.
For good reason. General "pointers" don't have a simple, compositional semantics that preserves modularity (Yes, I know about separation logic; that's not practically reasonable in a language like Rust - or C/C++ for that matter - that's not built around expressing complex logical invariants in code); references do. A piece of code that uses pointers in non-reference-like ways must be understood as a unit. Which is why Rust strives to reduce such code to a minimum, marked with a scary "unsafe" keyword.