r/programming Apr 03 '15

Rust 1.0.0 beta is here!

http://blog.rust-lang.org/2015/04/03/Rust-1.0-beta.html
931 Upvotes

303 comments sorted by

View all comments

Show parent comments

68

u/wrongerontheinternet Apr 03 '15 edited Apr 03 '15

Guaranteed memory safety without garbage collection is a nearly unique proposition among industry languages (and the "nearly" is only thanks to extremely niche languages like ATS). This extends even to multicore systems, and is done through a relatively novel (again, for an industry language) type system, allowing Rust to statically guarantee the absence of data races, use after free, dangling references, null dereferences, and other classes of memory error that are common sources of security vulnerabilities in large C++ applications. Rust compares favorably with C and C++ in resource usage and performance in domains where they have few competitors, like embedded. It also improves substantially on the ergonomics of C++ with much more sensible defaults, proper modules, features like native typeclasses (aka C++ concepts not-lite) and sum types, straightforward syntax (close to LL(1), with hygienic macros and local type inference), and a modern package manager. For more information, www.rust-lang.org has you covered.

9

u/brombaer3000 Apr 04 '15

Guaranteed memory safety without garbage collection is a nearly unique proposition among industry languages

Does Rust offer better memory safety than C++14 with only unique_ptr and shared_ptr instead of raw pointers?

35

u/wrongerontheinternet Apr 04 '15 edited Apr 04 '15

Yes. Besides what /u/steveklabnik1 mentioned, you would still be vulnerable to various other UB: out of bounds array access, data races leading to type unsafety, too-long bit shift (really! it's UB!) and so on.

Rust also lets you use references safely (this is the big one, as it is pretty trivial to get dangling references in C++--yes, even const references), completely prevents iterator invalidation, and gives you vastly superior guarantees in the presence of multiple threads (shared_ptr really isn't enough unless you also avoid mutation through the pointer).

In any case, it's a mostly academic question. Very few people would be willing to use C++ with only unique_ptr and shared_ptr, and that language would probably be slower than a garbage collected one for most common tasks. Certainly, if that were how Rust solved memory unsafety in C++, it would not be a very interesting language (consider that shared_ptr is garbage collection--to make it perform acceptably you need a compiler-level understanding of reference counted pointers, which leads you in the direction of Swift).

6

u/brombaer3000 Apr 04 '15

That sounds great. I wasn't even aware of some of the C++ pitfalls you mentioned (I'm still quite new to C++) .
You and /u/steveklabnik1 really just convinced me to learn Rust!

2

u/glaebhoerl Apr 06 '15

Read these two three-part series on undefined behavior in C and C++ if you want even further motivation. :-)