News for hackers. Stuff that matters.
Re:Explore the ownership system in Rust by Animats on Monday January 19, 2015 @09:23PM (#8915158)
If you've written some Rust, and you're more confused after reading that, it's not your fault.

Here's how to think about ownership in Rust. First, realize that Rust has basically the same memory model as C/C++. C/C++ has three big sources of memory trouble: "how big is it", "who owns it", and "who locks it". C/C++ provides little help in dealing with those issue. Rust locks down all those problem. Mostly through compile time checks.

Ownership in Rust starts out simple. There's single-ownership, and multiple-ownership with reference counting. The latter comes in two flavors, with and without concurrency locking. Reference counting works roughly the way you think it should.

In addition to ownership, there's "borrowing". This usually means creating a local reference to something. The local reference must have a scope that doesn't outlive the thing being borrowed. That makes borrowing safe. Borrowing is a compile-time thing with no run-time representation. Borrowed objects with reference counts don't need reference count updates on the borrowed reference, which speeds things up a lot. Borrowing in Rust is cheap and easy, and should be done frequently.

When a reference to something is passed into a function, the compiler needs to know if it's being borrowed, or whether ownership is being handed off to the function. The default is to borrow; for more complex situations, there's special lifetime syntax. A similar issue appears when a function returns a reference. Returning a reference is complicated - is it a new object, or a borrowed reference to an input object? Creating new objects in functions and returning them should be avoided if possible. It's a Rust idiom to create the object in the caller and pass it into a function to be modified.

Single ownership can be handed off to another owner. This, after some controversy, is the default action for the "=" operator. (Using "<-" for ownership transfer probably would have been better.) Such a handoff invalidates the variable that gave up ownership, and that variable can't be used again. For some types, though, you get a copy instead. For other types, you have to explicitly ask for a copy. This part of the language is kind of ugly.

Rust has the concept of "mutability". This is just the inverse of "const". Since immutability is the default, you write "mut" in a lot of places.

Programs in Rust need more design effort than in some other languages. You need to plan out who's going to own what, and who gets to change what. "Agile" types may find this troublesome. The payoff is that once the program has compiled, whole classes of errors have been eliminated.

Re:Explore the ownership system in Rust by sanxiyn on Tuesday January 20, 2015 @12:07AM (#8915566)
Historically, Rust used to have separate operators for copy and move (= vs <-). It was an ergonomic disaster. You may find the current state ugly, but I assure you that if you actually used it, you would have found what you suggested even uglier.

This is one example of Rust failing initial design because developers followed intuition. They thought the same "it would be better to use a separate operator for ownership transfer!" and ignored existing research in the area. The fact is every single language having linear types do ownership transfer by default, based on type.

The experience was reported here for posterity: http://smallcultfollowing.com/babysteps/blog/2012/10/01/move...

Re:Explore the ownership system in Rust by smosher_ on Tuesday January 20, 2015 @05:07AM (#8916210)
> but I assure you that if you actually used it, you would have found what you suggested even uglier.

It's not fair to make that judgment for someone, particularly because people using Rust at the time weren't complaining much about how ugly it was. At the time it was questioned whether the change was desirable and that very blog post braces for opposition under Wait, doesn’t this make it hard to know what your program does?

Fwiw, that's one instance where unnecessary information/features were removed I would rather had been preserved.

Re:Explore the ownership system in Rust by sanxiyn on Tuesday January 20, 2015 @05:43AM (#8916321)
I exaggerated for effect. I am sorry, and I apologize.

Being a long time Rust user, it is annoying to read "why doesn't Rust do X instead of Y?", insinuating X is clearly better than Y and they can't believe why Rust developers haven't considered the obvious alternative. Because it is very frequently (estimate: >50%) the case that Rust did do X, and changed to Y only after lots of actual experience and deliberation. For most design decisions people complain about Rust, Rust tried them both ways. In a few cases, changes were reverted after trying both ways, but usually changes stuck, because they were made carefully.

Re:Explore the ownership system in Rust by dan00 on Tuesday January 20, 2015 @06:11AM (#8916393)
> Fwiw, that's one instance where unnecessary information/features were removed I would rather had been preserved.

If the information isn't necessary to reason about your program, to write a correct and performant program, then you've just more information to consume, and instead of the unnecessary information you could consume other, necessary information.

Re:Explore the ownership system in Rust by smosher_ on Tuesday January 20, 2015 @04:28PM (#8919733)
The information isn't necessary for the compiler to reason about the program. The information is, in my opinion, useful for humans to reason about the program. The fact is I would reason about programs quicker if this was explicit.

There isn't any more information to consume. The information is exactly the same. It's just different, which is why I think it should look different.

Re:Explore the ownership system in Rust by drapper on Tuesday January 20, 2015 @03:04AM (#8915919)
> Creating new objects in functions and returning them should be avoided if possible. It's a Rust idiom to create the object in the caller and pass it into a function to be modified.

> immutability is the default

Haven't yet written any Rust code, but aren't those two things contradicting each other? I'd have thought that with immutability the most natural way to operate is to do pure functions (for uninitiated: pure function doesn't modify anything, can only produce new things).

Re:Explore the ownership system in Rust by dan00 on Tuesday January 20, 2015 @06:21AM (#8916426)
> Haven't yet written any Rust code, but aren't those two things contradicting each other?

I don't think so.

One way to ensure that data isn't mutated by different parts of a program, is to have immutable data structures.

The other way - the Rust way - is to seriously track who holds mutable references to the same data and just not allow it.

So you can have more optimized mutable data structures, but can be sure, that if you're holding a mutable reference, that no other can modify the data during the lifetime/scope of your mutable reference.

Re:Explore the ownership system in Rust by MichaelMoser123 on Tuesday January 20, 2015 @02:40PM (#8918980)
Similar to C++11 - here they added some smart pointers

  std::shared_ptr - 'owner' smart point with reference counter 

  std::weak_ptr - borrowing thing (non owning smart pointer)

  std::unique_ptr - exlusive ownership (transferable)
well reference counting has its problems: like circular reference chains that will cause leaks, now one is supposed to break these cycles with borrowing smart pointers (it still sucks); also in shared_ptr's the reference count is an atomic operations, even if you only use it from one thread; no way to change that (i think that's an oversight)

However what really sucks in C++ are the default constructor/copy constructor, etc operation; This was done so that C structures behave like classes - most of the C++ goodness is due to these backward compatibility with C requirements.

Re:Explore the ownership system in Rust by azth on Tuesday January 20, 2015 @03:14AM (#8915945)
> Creating new objects in functions and returning them should be avoided if possible.

Isn't that how "constructors" in Rust work (i.e. `Foo::new()` calls)? Plus, with return value optimization, it should be pretty performant.

Re:Explore the ownership system in Rust by lohankin on Monday January 19, 2015 @10:13PM (#8915287)
Noisy, unreadable code, obsessed with ownership, may give rise to whole new classes of errors that otherwise won't be there. Good that someone experimented with building a language around ownership, but for me it all demonstrates that the idea is not viable. I tried to read 10 tutorials already, each leaving me more confused than ever before.
Re:Explore the ownership system in Rust by Animats on Monday January 19, 2015 @11:20PM (#8915468)
I tried to read 10 tutorials already, each leaving me more confused than ever before.

I can understand that. The language has been changing so fast that most of the stuff on the Web is outdated or wrong. I just found a place in the official reference manual that's out of sync with the compiler, and sent in a note. (The lambda/closure syntax is still in a state of flux.)

Rust roughly follows C/C++/Java syntax, except that, like Go, it's a "name: type" language. Talking about ownership in code is new, as is the syntax for that. The ownership syntax is a bolt-on, and it shows. The lambda syntax is kind of weird; the designers went for conciseness over clarity. Some of this will take getting used to. The syntax is no worse than C++, and in some areas, better.

The syntax isn't the hard part, anyway. It's living within the ownership restrictions that's hard. It's hard because that's a design issue. There's a Rust port of Doom, and it has far too much unsafe code, because Doom's data structures were not designed with Rust in mind. It's not yet clear how big an issue this will be. The remark that 10% of Servo, the web page renderer, is unsafe code is a bad sign.

I don't have a personal opinion on this yet. I haven't written enough Rust code. I'm writing an RSS feed parser to get a sense of what it's like to deal with a complex tree in Rust. So far, it's going well, with no need for unsafe code.

If Rust can eliminate buffer overflows, dangling pointers, and memory leaks, that's enough to justify using it in place of C/C++.

Re:Explore the ownership system in Rust by dbaupp on Tuesday January 20, 2015 @02:40AM (#8915858)
Where did the 10% number come from?

FWIW, I just looked at the code in the mozilla/servo repository: more than half (1291) of all mentions of 'unsafe' (2150) are inside autogenerated bindings to CEF (Chromium Embedding Framework), which doesn't seem at all like the interesting/relevant part of the application (it's providing an optional "minimal working" browser chrome for the rendering engine). A lot of the other uses are similarly for other FFI tasks, interfacing with libraries like FreeType, HarfBuzz and Spidermonkey. Obviously it would be nice for those libraries to be in Rust and thus avoid the FFI-is-always-unsafe , but the ecosystem is still young and those libraries are seriously non-trivial, Mozilla and the Servo team presumably want to be able to do the interesting parts of their experimentation rather than spending years porting all the low-level libs to Rust first.

For some "success stories", Servo's CSS parser was now pure Rust, and has a single `unsafe` block: https://github.com/servo/rust-cssparser/blob/38877bc0a50a64a... . Similarly, the pure Rust HTML parser written for Servo doesn't have much unsafe (certainly very little compulsory unsafe, most exists in an alternate DOM implementation, or in the C interface the library exposes): https://github.com/servo/html5ever

That said, Rust is definitely not perfect in this regard, three of the major failings with Rust that force Servo to have a unsafe code are insufficient data-parallel libraries, lack of inheritance-like features to allow the DOM to be CPU and memory efficient, and lack of garbage collection hooks in the compiler.

> The ownership syntax is a bolt-on, and it shows.

Could you go into more detail about what you mean by this?

Re:Explore the ownership system in Rust by wycats on Tuesday January 20, 2015 @12:05AM (#8915560)
For what it's worth, I've written two fairly large pieces of code in Rust: Skylight and much of Cargo. In both cases, the amount of unsafe code was very small (tiny) and restricted to FFI boundaries, which are "unsafe" even in high level languages like Ruby.

Servo has been under active development for far longer, and sometimes used unsafe code to work around historical issues that no longer require unsafe code. I find the idea of an idiomatic codebase (written today), that is 10% unsafe code (outside of FFI boundaries) to be difficult to imagine.

Re:Explore the ownership system in Rust by eddyb on Tuesday January 20, 2015 @01:25AM (#8915706)
Mentioning Go for `name: type` may not be inaccurate, but it is an odd choice (we're seeing more comparisons of Rust and Go than Rust and any language it is closely related to). I believe the origin for Rust's choice is actually the ML family. EDIT: I just checked more carefully and Go doesn't even have the colon ([this blog post](http://blog.golang.org/gos-declaration-syntax) used it in pseudocode).
Re:Explore the ownership system in Rust by Animats on Tuesday January 20, 2015 @01:54AM (#8915759)
Sorry. "name : type" comes from Pascal, and the Modula/Ada/Oberon family of languages continue it. Now that type expressions have become so complex, it's better to have a syntax where you know a type expression is expected. C/C++ struggle with this; they have to know which names are types just to parse. This is a pain for tools which try to parse C/C++ without reading all the include files. C originally had only built-in type names and "struct". "typedef" came later, and made the syntax context-dependent.
Re:Explore the ownership system in Rust by adamnemecek on Monday January 19, 2015 @10:24PM (#8915322)
> obsessed with ownership, may give rise to whole new classes of errors that otherwise won't be there

How does baking the ability to statically determine resource ownership into a language lead to a class of errors? What sort of errors are these?

Re:Explore the ownership system in Rust by lohankin on Monday January 19, 2015 @10:40PM (#8915363)
By taking away the ability of programmer to read and understand the code written by somebody else, or by himself 1 week ago
Re:Explore the ownership system in Rust by wycats on Tuesday January 20, 2015 @12:06AM (#8915565)
As someone who has written many thousands of lines of production Rust code, this doesn't ring true to me at all. Ownership semantics have dramatically improved my ability to reason about my code, not the opposite.
Re:Explore the ownership system in Rust by adamnemecek on Monday January 19, 2015 @10:44PM (#8915379)
I have no clue what you are talking about and I don't think that you have either so I'll just leave this discussion now.
Re:Explore the ownership system in Rust by Retra on Monday January 19, 2015 @10:36PM (#8915353)
Have you written any code?