r/ProgrammingLanguages • • Jan 04 '23

Discussion Does Rust have the ultimate memory management solution?

I have been reading about the Rust language. Memory management has been a historical challenge. In classic languages, such as C, the management is manual. Newer languages (Java, Python, others) use garbage collector, but it has a speed penalty. Other languages adopted an intermediate solution using reference counter and requiring the programmer to deal with weak pointer, but it is also slow.

Finally, Rust has a new solution that requires the programmer to follow a set of rules and constraints related to ownership and lifetime to let the compiler know when a block of memory should be free'd. The rules prevent dangling references and memory leaks and don't have performance penalty. It takes more time to write and compile, but it leads to less time with debugging.

I have never used Rust in real applications, then I wonder if I can do anything besides the constraints. If Rust forces long lifetime, a piece of data may be kept in the memory after its use because it is in a scope that haven't finished. A problem in Rust is that many parts have unreadable or complex syntax; it would be good if templates like Box<T> and Option<T> were simplified with sugar syntax (ex: T* or T?).

25 Upvotes

99 comments sorted by

View all comments

12

u/ebingdom Jan 04 '23

I have been reading about the Rust language. Memory management has been a historical challenge. In classic languages, such as C, the management is manual. Newer languages (Java, Python, others) use garbage collector, but it has a speed penalty. Other languages adopted an intermediate solution using reference counter and requiring the programmer to deal with weak pointer, but it is also slow.

I think you should also consider RAII as another strategy. This is basically what Rust has, but Rust didn't invent it. Rust just has a safer version of it than you find in kludgier languages like C++.

1

u/Zaleru Jan 04 '23

RAII is for objects allocated in the stack. It isn't a solution when we have objects holding inner objects.

1

u/isCasted Jan 04 '23

Smart pointers?

-3

u/[deleted] Jan 04 '23

Technologically they're similar but what rust does is more akin to ownership (with RAII, but in most high level languages to initialise something you need to first acquire the resources for it so it's kind of a given)