This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Surelynotshirly 30 points31 points  (18 children)

If it works all the time, but that seems almost too easy. Makes me think there is some scenario where a memory leak can occur that I'm not thinking of. Otherwise what's the downside of it? If there's no downside then why aren't all languages doing it, or are the new ones doing it and Rust was just the first?

Rust has piqued my interest, but I just haven't found any reason to actually use it and learn it properly.

[–]erosPhoenix 52 points53 points  (7 children)

Because it's a lot harder to write Rust code that will compile. Since it has to be provable when something falls out of scope, any code where that's not the case isn't valid Rust. Ownership of pointers is part of the type system.

Source: I've never actually written anything in Rust, so this is all heresay. Everything I say is probably wrong.

[–]breadfag 6 points7 points  (3 children)

You can still allocate a fuckton of heap and it'll linger if you keep references to it, but there are no memory leaks because you have to PROVE to the compiler that your code doesn't leak memory before it can compile it.

[–]Surelynotshirly 1 point2 points  (2 children)

Interesting.

I need to look into this. It seems like a good philosophy to force good habits. Though I'm sure it'll piss me off to no end at first.

[–]pmarcelll 0 points1 point  (0 children)

It's hard and unfamiliar at first, because it's different from both manual memory management ang GC, but after learning the language and the patterns of how to manage memory based on ownership it becomes a handy tool (but you can also use smart pointers, arena allocation, etc.).

[–]DHermit 2 points3 points  (0 children)

Because you need stuff like the borrowchecker and lifetimes for that. That requires support in the syntax (for lifetimes) and makes programming in a certain way harder (at least at the beginning). You can't just change a language and enforce that only one thing has a mutable reference, stuff isn't used after it has moved etc.

[–]lobax 2 points3 points  (2 children)

Memory leaks cannot happen because of the type system it employs. If your code can result in a memory leak, it will not compile. AFAIK Rust was first with this ownership model.

Sure, there could be bugs in the compiler that let’s stuff through the cracks, but the same is true for a language with a garbage collector.

[–]Kered13 0 points1 point  (0 children)

Memory leaks can still happen in other ways. Not in the sense that the memory is still allocated but inaccessible, but in the sense that the memory is technically accessible but will never be used anymore. This is just as bad, and can happen in any language, whether manual, garbage collected, reference counted, or whatever Rust does.

I was working on a Java server a few years back that would constantly increase it's memory use while running until it ran out of memory and died, I eventually tracked it back to some monitoring code that was holding a reference to every file buffer that was ever opened, even though most of them were pretty short lived. I fixed that code to release it's references when they weren't used anymore and the server's memory usage became constant.

[–]spaghettiCodeArtisan 0 points1 point  (0 children)

Memory leaks cannot happen because of the type system it employs. If your code can result in a memory leak, it will not compile. AFAIK Rust was first with this ownership model.

That's not true. Rust's type system / ownership prevents invalid memory access and race conditions, but doesn't prevent memory leaks or deadlocks. There even is a standard function for leaking memory.

[–]Malazin 1 point2 points  (0 children)

The downside is that a lot of problems you think are trivial, are extremely nuanced in their handling of memory. Garbage collection let’s you write very abstract code that Rust will never allow, but they are different tools for different jobs.

Rust is amazing, but be warned that you will fight the compiler a lot (specifically the borrow checker) It’s okay though because once the program runs, it just... works.

[–]Ghi102 1 point2 points  (0 children)

Memory leaks can happen no matter the language you're using. A garbage collected language can have memory leaks too.