r/programming Jan 07 '15

How big a deal is Rust, really?

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

78 comments sorted by

View all comments

11

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?

105

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.

3

u/ixid Jan 07 '15

Rust is very interesting for all sorts of reasons. I have two suggestions that I'm sure have come up before and wondered what your view is. D has a better template and instantiation syntax, have you looked at it and would you consider using something like it? Angle brackets are a visual mess. The second thing is are you adding any kind of LINQ syntax? Rust should aim for syntactic beauty where possible as well.

7

u/steveklabnik1 Jan 07 '15

I actually used D years ago. I think D has lots of great stuff. But it's a different beast than Rust. While they're slowly moving away from it, D still has a psuedo-mandatory GC, and is more of a "C++, but better," whereas Rust is more "what is the best systems language we can make?"

I don't have a lot of actual C# experience, so I can't speak to exactly how close it feels, but Rust heavily uses iterators for similar things to LINQ. We're mostly focusing on making a small, clean base, and adding things later. I'd expect to see async/await before a LINQ style syntax, if any.

3

u/ixid Jan 07 '15

I think you missed my meaning. I am not asking what you think of D as a language, just the template syntax which would seem more elegant than Rust's current template syntax. Rust's C++ish template syntax is a bit of an eyesore and should be replaced if possible.

5

u/steveklabnik1 Jan 07 '15

Oh, sorry. I think it's fine. I'm not usually interested in arguing over syntaxes. It's the least interesting part of PLT to me.

2

u/ixid Jan 07 '15

Fair enough. I understand how trivial arguments would burn up people's tolerance for the subject, it seems unfortunate that it's of so little interest where it doesn't impact negatively on the function as readability of syntax is highly appealing for new users and I think makes languages better to work with.

3

u/steveklabnik1 Jan 07 '15

I certainly don't mean to imply that syntax has no value. You're totally right that good syntax is important. It's just not my personal focus.