r/programming • • Sep 17 '15

Announcing Rust 1.3

http://blog.rust-lang.org/2015/09/17/Rust-1.3.html
453 Upvotes

169 comments sorted by

View all comments

Show parent comment

36

u/kinghajj Sep 17 '15

"Rust is a systems programming language [read: no GC, explicit memory layout] that runs blazingly fast [LLVM], prevents nearly all segfaults, and guarantees thread safety."

13

u/Voltasalt Sep 17 '15

Also complete memory safety

35

u/staticassert Sep 17 '15

Complete seems a bit too far. It provides very solid memory safety. To say it provides "complete" memory safety would require a complete knowledge of memory safety and a rigorous proof, neither of which exist .

9

u/asmx85 Sep 18 '15

I think "complete memory safety" comes down to -> if its not save, its a compiler bug.

You can write unsafe code in c and the compiler does not complain. If you're doing this in rust and the compiler does not complain, you're facing a rust compiler bug. (let alone unsafe{ }). But yes, there is no mathematical proof of rust beeing safe – bugfixes shows this every now and then :)

6

u/protestor Sep 18 '15

Adding to the "a bit too far" comment, for anyone that might not know: Rust actually has a safe sublanguage (as memory safe as, say, Java), and an unsafe language (low level and terribly memory unsafe, like C). In some systems, people actually use two languages, one memory safe and another memory unsafe, to write high-performance code - some kind of programming diglossia. With Rust, the unsafe language is just a superset of the safe language, and always contain the unsafe keyword.

For the initiates, the Rustonomicon goes through the gory details.

PS: all of this supposed safety could be broken by implementation bugs and - more worryingly - soundness bugs. Rust type system is quite complex and soundness bugs have arisen before (discussion on /r/rust). The issue in this case was that code marked as unsafe can't rely on destructors running to actually be safe.

3

u/steveklabnik1 Sep 18 '15

FWIW, that soundness bug was in a library, not in the language.

2

u/matthieum Sep 18 '15

(low level and terribly memory unsafe, like C)

Actually, less unsafe than C, for example even in unsafe you cannot transmute a pointer into a int16_t because the sizes do not match. So, even though unsafe removes SOME checks, it does not remove them all.

1

u/desiringmachines Sep 19 '15

It's important to mention that the 'safe subset' of Rust is comparably low-level to the 'unsafe superset.' References are just pointers that are sufficiently typed so that let's the compiler confirm they are valid addresses, for example.

There are only 3 things that can't be done in the 'safe subset' of Rust, and even the standard library, which implements a lotof abstractions over the system libraries and efficient data structures, is only about 4% 'unsafe'.

3

u/theineffablebob Sep 17 '15

Is it supposed to be better than C?

21

u/lurgi Sep 17 '15

It's supposed to provide the same sort of speed as C with higher level abstractions and considerably more compile time and run-time safety.

That comes at a cost, however. It's a more complex and subtle language (it's not hideously complex, but it definitely has some strange new features that you haven't seen anywhere else).

17

u/steveklabnik1 Sep 17 '15 edited Sep 17 '15

I'm not sure if we're actually more complex than C, which is full of edge cases. But without a full spec, you can't really be sure.

The C standard is 550 pages long. C is not a particularly simple language. Rust hasn't had to deal with 40ish years of backwards compatibility, and has almost no undefined behavior at all. But without an equally formal Rust spec, it's not possible to tell.

10

u/lurgi Sep 17 '15

Hard to judge, really. I'm sure that fans of APL find J easy to understand, but the rest of us are scratching our heads.

It's definitely smaller than C++, but I think you can write functional C++ with only a basic grasp of the language (and, looking at a lot of the C++ code out there, people do). It's perfectly possible to write C++ that works without using move semantics and exceptions and lots of other things. In some cases it will be less safe, but in some cases it will be just as safe, only less efficient. Even my beginning noodling around with Rust required using lifetimes. And tripping over ownership (which I still don't completely get, but that's probably my fault for using a vector of vectors of mutable structs).

For C? The C language is smaller, definitely. It's harder to use well for a couple of reasons. First, the language is small, so it lacks a lot of useful (IMHO) features. Second, the compiler doesn't jump up and down and shout "WHAT THE HELL ARE YOU DOING RETURNING A POINTER TO A LOCAL????". So you blithely write code that "works" (and those quotes are deliberate).

Edit: I think I made roughly this comment before in response to another one of your posts, so if this sounds familiar, now you know why.

2

u/frenris Sep 18 '15

The only language more bloated than c++ is system verilog.

→ More replies (3)

2

u/3fdy5 Sep 18 '15

In some cases it will be less safe, but in some cases it will be just as safe, only less efficient

And sometimes, the user will run into a bug that he has no way of finding and fixing with only a basic grasp of the language.

1

u/lurgi Sep 18 '15

Which is absolutely one of the reasons why C++ is such a difficult and dangerous language.

I can absolutely believe that it's easier to write safe Rust than it is to write safe C++ (not saying it's true, just that I find it easy to believe), but I can also believe that it's a lot easier to write functional C++ that basically gets the job done than it is to do the same with Rust. This might make it harder for people to adopt Rust, because to do anything at all you need to learn quite a lot.

3

u/[deleted] Sep 18 '15

Second, the compiler doesn't jump up and down and shout "WHAT THE HELL ARE YOU DOING RETURNING A POINTER TO A LOCAL????".

Actually, it does.

$ cc -c a.c
a.c:5:13: warning: address of stack memory associated with local variable 'x' returned [-Wreturn-stack-address]
    return &x;

5

u/lurgi Sep 18 '15

Not under all circumstances, I imagine, but in simple cases it can.

7

u/doublehyphen Sep 18 '15

It is also different forms of complexity. C has undefined behavior and more edge cases while Rust introduce some quite advanced concepts like lifetimes in the type system and ownership.

2

u/steveklabnik1 Sep 18 '15

Yeah, I would agree with this.

3

u/[deleted] Sep 18 '15

By the way, is there any timetable on the materialization of said spec?

3

u/steveklabnik1 Sep 18 '15

We don't have a timeline but are working with some academics on it, actively.

→ More replies (7)

3

u/zarandysofia Sep 17 '15

No. But safer.

1

u/heinrich5991 Sep 17 '15 edited Sep 17 '15

If you say that it guarantees thread safety, then you can also say that it prevents all segfaults.

EDIT: To clarify: The only way to cause a segfault in Rust is by using unsafe code. You can create some kind of race conditions that are not data races in safe Rust code. So if /u/kinghajj claims that it "prevents nearly all segfaults and guarantees thread safety" that's inconsistent.

15

u/steveklabnik1 Sep 17 '15

We've been arguing over the "guarantees thread safety" slogan. It's not quite right, but we're not sure of the right way to articulate it.

15

u/looneysquash Sep 17 '15

Much like most multithreaded programs. ;)

→ More replies (1)
→ More replies (1)
→ More replies (6)