3 ms·
I'm a massive Nimrod fan, but those safety guarantees are void once you use manual memory management. What Rust gives you is safety with memory management. Nimr
by girvo 13y ago
I'm a massive Nimrod fan, but those safety guarantees are void once you use manual memory management. What Rust gives you is safety with memory management. Nimrods GC and type system are both really powerful, but Araq and I were chatting about Rusts type system on irc for a reason...
- TylerE 13y agoI'm probably in the minority here, but I think the answer is "so use a GC". The number of people on earth that I would trust with my life to correctly manage memory in a complex application is very small.
- pcwalton 13y ago> The number of people on earth that I would trust with my life to correctly manage memory in a complex application is very small. The Rust compiler checks it for you, so you don't have to trust anyone. :)
- tel 13y agoWhat kind of guarantees does the type system provide? Can I say that no terminating/productive Rust program leaks memory, full stop?
- kibwen 13y agoRust's type system statically guarantees that memory will never be accessed after it is freed, so no segfaults, no use-after-frees, no double-frees, no iterator invalidation, etc. It also statically eliminates data races between concurrent tasks. In the past Rust has attempted to use the type system to prevent memory leaks in certain cases, but the features that attempted to do so were deemed overly restrictive to use for practical purposes. Nowadays I'm sure it's possible to leak memory if you try. Honestly I've never heard of a Turing-complete language whose type system can provide such a guarantee.
- pcwalton 13y ago> Honestly I've never heard of a Turing-complete language whose type system can provide such a guarantee. SPARK (a dialect of Ada) can, I believe. But it does so by forbidding allocation :)
- haberman 13y agoDoesn't it make a stronger guarantee, that you cannot cause an invalid dereference? In addition to what you mentioned, this would also cover bounds-checking, trying to dereference a pointer that was never allocated, etc. Also does it enforce that memory is consistently used as a single type? Can you allocate a byte array and then cast it to an appropriately sized array of integers?
- kibwen 13y ago> Doesn't it make a stronger guarantee, that you cannot > cause an invalid dereference? I'm not knowledgeable enough to answer that question precisely. However, I can tell you that Rust's type system is not strong enough to obviate bounds checking. I hear you'd need something like Idris' dependent types for that. Rust bounds checks arrays dynamically (there are `unsafe` functions available to index an array without bounds checks), and avoids bounds checking on arithmetic by guaranteeing that fixed-sized integers will wrap on overflow (which is gross, but might be changed to fail-on-overflow if it doesn't hurt performance too much). > Can you allocate a byte array and then cast it to an > appropriately sized array of integers? You can't do this in safe code, but you can in `unsafe` code via the `std::cast::transmute` function, which does still enforce that both types are the same size.
- haberman 13y ago> However, I can tell you that Rust's type system is not strong enough to obviate bounds checking. That's a bummer. It seems doable, but maybe it is too complex. > avoids bounds checking on arithmetic by guaranteeing that fixed-sized integers will wrap on overflow (which is gross, but might be changed to fail-on-overflow if it doesn't hurt performance too much). That would be nice as a default, but I'd be afraid it would hurt performance too much for numerical code. You'd definitely want a way to express arithmetic should be allowed to overflow (ie. that omits the check). On a related note, one thing that is sorely missing in C and C++ is a way to test whether a value will overflow when converted to a different type (I wrote a blog article about this point: http://blog.reverberate.org/2012/12/testing-for-integer-overflow-in-c-and-c.html http://blog.reverberate.org/2012/12/testing-for-integer-over...)
- bzbarsky 13y agoYou can't even say that about languages _with_ a GC. At least not as long as you use the practical definition of "leaks memory", which is that the memory remains alive until the application shuts down. Here's a simple example of a memory leak in JS in that sense (modulo nontrivial manual cleanup, obviously): window[Math.random()] = new Array(100000); A much more interesting question, in some ways, is what guarantees you have about not accessing no-longer-alive objects. That's where Rust has some serious advantages over C++, say.
- tel 13y agoI think you could in raw linear logic—you wouldn't be allowed to introduce a type unless you eliminated it later.
- dbaupp 13y agoYou don't need to trust people using Rust, you just trust the type system. Admittedly, the memory safety features of the type system haven't been formally verified, but this is a goal, and there is a rather large piece of in-source documentation: http://static.rust-lang.org/doc/master/rustc/middle/borrowck/doc/index.html http://static.rust-lang.org/doc/master/rustc/middle/borrowck... (I haven't read it, so I have no idea if it will make any sense to someone who doesn't know Rust.)
- saosebastiao 13y agoI think the answer is "don't use a language that you don't want to use". If Nimrod works for you, that isn't a reason to bash Rust.
- Xorlev 13y agoDifferent use cases, Rust isn't for you it seems. The whole point of Rust is to make memory management safer.
- haberman 13y agoThere are many problems where a GC is too intrusive (one notable one is: writing a GC). Until you acknowledge this, Rust will make little sense to you (nor will C or C++, for that matter). The whole problem Rust is trying to solve is that a programmer can do manual memory management without anyone needing to trust that they have gotten it right. The compiler can automatically check correctness (except for unsafe blocks, which are kept to a minimum).
- scott_s 13y agoSo why not let those small number of people have access to a language which makes it much easier to correctly manually manage memory?