News for hackers. Stuff that matters.
Re:Learning Rust by amelius on Sunday November 02, 2014 @01:17PM (#8547415)
I'm interested in Rust also. But I'm wondering, does it have these features, and to what extent?

  * coroutines
  * pure functional programming (enforcing it in an isolated subset of the code)
  * laziness
  * (clean) meta-programming
  * introspection
  * garbage collection
Especially for stream-based programming (essential for internet applications), you'd need something like coroutines, to keep things manageable. I'm wondering if there is example code for a well-performing Rust-based web-server out there already.

Also, how does Rust compare to other modern languages, like Scala?

Re:Learning Rust by adrusi on Sunday November 02, 2014 @02:54PM (#8547845)
I've followed rust for a couple years, never done anything major with it.

It doesn't have coroutines, but they might be possible to implement using a macro that expands to an iterator implementation. It also has go-like channels and can be used with both N:M (green) threads and os-native 1:1 threads. It handles streams of data quite well.

All variables and references in Rust are immutable by default and accessing global mutable state can only be done in explicitly marked unsafe code. I think that all functions that have no "mut" or "unsafe" in their signature are pure unless they use an "unsafe" block. Exposing unsafe behavior in a safe interface is a contract violation and considered a bug.

There's no built in laziness, but because Rust allows for overloading the pointer dereference operator, it should be possible to implement thunks in a syntactically transparent way.

Rust has a full macro system. There are AST-based macros for when you need a serious amount of power, but most macros are implemented on a partially structured token stream and are implemented a lot like scheme's syntax-case. It's a lot cleaner than metaprogramming in dynamic languages like python and ruby IMO, and it has no runtime overhead.

Introspection? No, but I think doing something about that is on the post-1.0 roadmap.

Garbage collection. A reference-counted pointer like C++11's shared_ptr is available in the standard library. Its use is discouraged, though, in favor of structuring your code to work with Rust's lifetime system and borrow checker. Doing that, your memory is managed in a safe way with no runtime overhead, you don't have to worry about reference cycles, and the structure it enforces in your code tends to be easier to follow (although often harder to come up with).

Rust isn't really meant to be used in place of Scala, it's more of a "competitor" to C++. It keeps C++'s promise of "don't pay for what you don't use" and strongly favors 0-cost abstractions. It differentiates itself from C++ by using more modern abstractions and garaunteeing safety.

Re:Learning Rust by jeremyjh on Sunday November 02, 2014 @03:39PM (#8548011)
I'm not totally clear on the status of green threads but I'm not sure they should be encouraged presently. My understanding is they will be an external package and std::io will not support them.

[1]https://github.com/rust-lang/meeting-minutes/blob/master/wor...

Re:Learning Rust by adrusi on Sunday November 02, 2014 @04:55PM (#8548238)
I think that the external package is supposed to work by providing an alternate implementation of std::io and several other parts of the standard library. Green threads will continue to be fully supported, but they are second class.
Re:Learning Rust by wyager on Sunday November 02, 2014 @06:45PM (#8548645)
> coroutines

Would not be part of the core language. Quite feasible as a package or plugin, and I think under development.

> pure functional programming (enforcing it in an isolated subset of the code)

I don't believe Rust has a way to enforce purity of a function, but the type system could certainly help you out. Rust's type system doesn't have HKTs, so you can't do generic monads, but you could probably do an IO-specific monad.

>laziness

Already implemented in the loose sense with e.g. lazy iterators. Thunk-based evaluation could be implemented using operator overloading, lazy data structures, etc.

>(clean) meta-programming

I haven't used it a whole lot, but Rust's macro system seems clean and powerful.

>introspection

Could you expand a bit on what you mean by this? The kind of introspection I'm thinking of seems to run counter to the strict correctness and isolation principles espoused by Rust.

>garbage collection

This would be implemented using a custom wrapper type. I think it's in the works. However, the need for GC is mostly obviated by the statically checked lifetime system.

>Also, how does Rust compare to other modern languages, like Scala?

Rust, obviously, does not run on the JVM.

The type system is similarly rich, although (being HM-based) does not support subtyping (instead opting for the interface/typeclass/trait-only approach used by e.g. Haskell). The most obvious improvement over Scala is substantially improved type inference.

Scala absolutely requires heavy heap usage and GC. Rust requires no heap usage and absolutely no GC. All that stuff is determined at compile time, which is great for determinism and performance. The downside is that it requires some more effort from the programmer to think about lifetimes and such.

Rust has a substantially lower-level focus than languages like Scala or Haskell. It aims to be a C(++) replacement, not a Java replacement.

Re:Learning Rust by delluminatus on Sunday November 02, 2014 @01:40PM (#8547527)
I can comment on a few of those. I'm not a Rust expert but I've been following it some. One thing I don't know about are coroutines. I know Rust has iterators but I don't think those are quite the same?

Rust uses lazy iterators for everything (including functions like map, and datatypes like arrays).

Rust actually has a full hygenic macro system (metaprogramming). IMO this is one of the coolest things about Rust.

Rust does not use reference-counting GC. Instead, it has a fairly unique borrow checking system for garbage collection, which not only handles cleaning up memory but also cleaning open sockets, file handlers, etc. There is a generic type implementation of a reference counted variable you can use, though.

Although there are no "pure functional" (i.e. side-effect free) guarantees in Rust AFAIK, the borrow checker actually catches a lot of issues that pure functional programming is meant to fix (i.e. issues from unexpected state mutation). Also, variables in Rust are immutable by default, you have to use the "mut" keyword to make them mutable.

Re:Learning Rust by veddan on Sunday November 02, 2014 @02:49PM (#8547818)
>Rust does not use reference-counting GC. Instead, it has a fairly unique borrow checking system for garbage collection, which not only handles cleaning up memory but also cleaning open sockets, file handlers, etc. There is a generic type implementation of a reference counted variable you can use, though.

Rust does not have garbage collection, unless you count using types like Rc<T> garbage collection. It has destructors, just like C++, and uses them for cleaning up resources like file handles and memory. Lifetimes do not really interact with destructors. They're part of the type system and do not have any effect on the generated code.

Re:Learning Rust by myrryr on Sunday November 02, 2014 @03:42PM (#8548022)
I was given the impression that as soon as the lifeline was out of scope, the resources were cleaned.

So it acts like a GCed language in most ways, in that, most of the time you don't have to worry about cleaning up after yourself, and that it is hard to memory leak.

Re:Learning Rust by kaoD on Sunday November 02, 2014 @08:26PM (#8548911)
> I was given the impression that as soon as the lifeline was out of scope, the resources were cleaned.

And you are right.

Even though GP is almost right, lifetimes do interact with destructors and, in fact, dictate when values go out of scope and hence when drop (Rust destructor) is called.

> So it acts like a GCed language in most ways, in that, most of the time you don't have to worry about cleaning up after yourself

Nope. In Rust you almost always have to worry about cleaning up although not explicitly, but via lifetime and ownership management.

Sometimes you're lucky and those lifetimes can be inferred, but you still have to think about ownership a lot.

> and that it is hard to memory leak.

But not in the same way.

In Rust you can memory leak only when:

- Using GC like Arc or Rc with cycles.

- On blocks/fns marked as unsafe (which are mandatory for unsafe operations).

- Running into a Rust bug.

Re:Learning Rust by dbaupp on Sunday November 02, 2014 @08:30PM (#8548922)
> And you are right. Even though GP is almost right, lifetimes do interact with destructors and, in fact, dictate when those are called.

It's actually the other way around: scoping dictates when destructors are called and the length of the lifetimes of borrowed pointers etc.

That is, the main factor is scoping, and lifetimes just reify that into something the programmer and compiler can reason about, to ensure safety.

Re:Learning Rust by kaoD on Sunday November 02, 2014 @08:42PM (#8548939)
> That is, the main factor is scoping

Sorry, I meant (and I guess myrryr meant too) ownership instead of lifetimes. I was in the process of ninja-editing my comment when I saw your reply :P

Ownership dictates when a value goes out of scope and hence when the destructor is called.

Re:Learning Rust by Dewie on Sunday November 02, 2014 @04:16PM (#8548142)
If you'd like to call that a form of GC, I'd prefer to call it "compile-time GC" to avoid confusion. I rarely see "garbage collection" being used to refer to anything other than dynamic garbage collection.
Re:Learning Rust by mattdw on Sunday November 02, 2014 @05:23PM (#8548327)
I assume coroutines could be trivially implemented with sync_channel(0), which blocks the sender until the receiver takes the message. (And the receiver blocks until there is a message available.)

The 'generator' use of coroutines could be implemented with Receiver::iter, which is an iterator over a channel.

(As I understand it, coroutines are just a special case of channels.)

[Edit: see <http://doc.rust-lang.org/std/comm/> for channel docs]

Re:Learning Rust by amelius on Monday November 03, 2014 @07:15AM (#8550405)
Wouldn't that require the use of threads, and hence be very expensive?
Re:Learning Rust by ufo on Sunday November 02, 2014 @10:47PM (#8549256)
Your link is broken - there is an extra ">" at the end.