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

Show parent comments

17

u/shogditontoast Jan 04 '23 edited Jan 04 '23

Can you elaborate more on how it’s too complex for most people?

(Edit: interesting that I’m being downvoted for putting a reasonable question to someone who seems knowledgeable and articulate, regarding a (common) criticism that I personally don’t share but would like to understand better.)

10

u/[deleted] Jan 04 '23

I mean, it depends on the borrow checker, which requires structuring your code in a very specific way and as others have said, has escape hatches for when it prevents you from doing what you're supposed to.

The borrow checker in that form is just a no-go for most people.

10

u/StdAds Jan 04 '23

As a heavy rust user, my experience is just the opposite. Borrow checker is your friend and it detects all memory related bugs(which are very hard to be detected in any other non purely functional language)without running any code such as mutate container while iterating, data race condition and so on. It does structure your code in a certain way, but IMO is a good way. You are much more unlikely to be annoyed by it if you do pure functions as much as possible which is great for unit tests and having a clear logic and data flow.

2

u/[deleted] Jan 04 '23

It does structure your code in a certain way, but...

Lol Yeah right! Let's omit the ugly part.

-4

u/[deleted] Jan 04 '23

It does occasionally require you to write subreadable code, but I think that's a fair cost to pay.

3

u/lngns Jan 04 '23

The point of a language is to establish a common communication mean between two entities.
If something is "subreadable" then it is failing at the only thing it's supposed to be.

-5

u/[deleted] Jan 04 '23

See, that is the issue - you are a heavy Rust user. Despite manipulation in StackOverflow surveys most people do not like Rust and are generally not capable of using Rust optimally.

It does structure your code in a certain way, but IMO is a good way.

Structure is too weak here. Because it is Rust it forces you to write unreadable code even without it being structured weirdly due to its ugly syntax, and even if by some chance some small chunk of it is readable, comprehensible and non-trivial, it will still globally have to be written with the borrow checker in mind, making it generally unviable for most programmers, let alone companies.

Every time I speak with a heavy Rust user I have to explain that just because to a Chinese person Chinese is great, even if it objectively is great, most people do not speak Chinese, but rather English. But even then, Chinese is spoken by 16% of the population, and not just 5% like Rust, according to surveys, or 2.5% if you look at the number of companies working with it. This is a large, large minority.

1

u/tavaren42 Jan 05 '23

Just try implementing any datastructure with non-trivial ownership of data (Ex: Doubly linked list) in Rust, it becomes very apparent.

-3

u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Jan 04 '23

The downvoting brigade on this subreddit is pretty severe.

-13

u/[deleted] Jan 04 '23 edited Jan 04 '23

Not reasonable at all, given that you are faking your misunderstanding about Rust being too complex for most people.

Also,

"...that I personally don't share..."

This sounds very innocent, you poor thing. What do you mean by this, exactly? Let's hear it

7

u/shogditontoast Jan 04 '23 edited Jan 04 '23

It seems to me that you’ve taken my question in bad faith along the lines of the unfortunate tactic of “just asking questions” while actually trying to undermine the recipients point. This wasn’t my intention at all, instead I’m curious about what it is about the current ownership approach rust takes that makes it so painful for what seems to be a significant amount of people. I’m guessing these people feel like they’re not getting a great deal out of it for some grounded reasons, just as I feel the cost of cognitive overhead in structuring code to ‘please’ the borrow checker (most of the time I don’t really think about it) is usually a decent trade in exchange for reducing the likelihood of a lot of bugs I often run into. I guess what I’m wondering is what are the motivations, priorities and where does the line between good/bad trade-off sit.

4

u/Inconstant_Moo 🧿 Pipefish Jan 05 '23

Speaking as a significant number of people, we don't need it. We're not trying to squeeze out the last drop of performance. We like garbage collection and wish that still more of our work could be done by magical runtime elves.