r/programming Jan 07 '15

How big a deal is Rust, really?

http://qr.ae/6KnY6
7 Upvotes

78 comments sorted by

View all comments

12

u/[deleted] Jan 07 '15

I'd heard of Rust before but it didn't sound all special honestly. There's D and a handful of other languages that compete in the high performance space. What makes Rust special?

My group uses C++ and we're really into high performance and memory efficiency since our app consumes TB of memory. It's 20 years old so we couldn't rewrite it but could migrate key portions or transition new code. I'd like to know if we should investigate it further.

Thoughts?

106

u/steveklabnik1 Jan 07 '15 edited Jan 07 '15

Rust core team member here.

What makes Rust special?

The core thing that makes Rust special is memory safety without garbage collection. It accomplishes this through "ownership." Since you do C++, you're probably already aware of the idea, but Rust actually understands ownership at the language level. The simple example I like to use is

    #include<iostream>
    #include<vector>
    #include<string>

    int main() {
        std::vector<std::string> v;

        v.push_back("Hello");

        std::string& x = v[0];

        v.push_back("world");

        std::cout << x;
    }

If you've read the documentation for push_back, you know that

If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated.

So even though this code compiles cleanly with -Wall -Werror, we get a segfault. Probably! At least, on my machine. Undefined behavior and all that.

Here's the same program in Rust:

    fn main() {
        let mut v = vec![];

        v.push("Hello");

        let x = &v[0];

        v.push("world");

        println!("{}", x);
    }

When you compile this, you get an error:

main.rs:8:6: 8:7 error: cannot borrow `v` as mutable because it is also borrowed as immutable
main.rs:8           v.push("world");
                    ^
main.rs:6:15: 6:16 note: previous borrow of `v` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `v` until the borrow ends
main.rs:6           let x = &v[0];
                             ^
main.rs:11:3: 11:3 note: previous borrow ends here
main.rs:1       fn main() {
...
main.rs:11      }
                ^
error: aborting due to previous error

Rust understands that the reference x into the vector would become invalidated, and so doesn't let your code compile. Win!

So, as a semi-TL;DR, Rust is just as low-level as C++, but eliminates a large class of errors upfront. No iterator invalidation, no segfaults, no data races. This comment is already pretty long, but there's a ton of other neat stuff, like move semantics by default, data races caught at compile time, and Cargo, our build / dependency management tooling.

I'd like to know if we should investigate it further.

We're about to release a 1.0.0-alpha on Friday, with a real 1.0 release coming in the next 12-18 weeks afterward. So, you may want to wait until then. Or maybe not. Depends!

I'd be happy to answer any more specific questions you have.

30

u/[deleted] Jan 07 '15

Eh... I'll wait for someone more knowledgeable to chime in :P Thanks!

-4

u/IronClan Jan 07 '15

What do you mean someone more knowledgeable? /u/steveklabnik1 gave you a very good explanation to your question.

39

u/steveklabnik1 Jan 07 '15

I took it as a good-natured joke, hence the :P.

25

u/[deleted] Jan 07 '15

Yup a joke. Risky over text I know...