News for hackers. Stuff that matters.
Re:Announcing Rust 1.14 by libria on Thursday December 22, 2016 @02:53PM (#13239844)
> experimental support for WebAssembly as a target, wasm32-unknown-emscripten [...] check out Tim’s example of the classic TodoMVC project.

On one hand this is really cool and potential for client-side Rust. On the other hand:

    let id = node.parent().unwrap().parent().unwrap().data_get("id").unwrap().parse::<usize>().unwrap();
Eh, no thanks. :) If I could get a macro or shorthand for that, I'd welcome all the other benefits of a large scale client-side platform with strong typing.
Re:Announcing Rust 1.14 by bbatha on Thursday December 22, 2016 @02:57PM (#13239879)
When the carrier trait lands this becomes:

    let id = node.parent()?
                 .parent()?
                 .data_get("id")?
                 .parse::<usize>()
                 .ok()?;
I'd also expect that someone will write higher level libraries so that you aren't doing raw DOM manipulation. A rust vdom wasm library would probably be blazing fast.
Re:Announcing Rust 1.14 by killercup on Thursday December 22, 2016 @03:28PM (#13240106)
> A rust vdom wasm library would probably be blazing fast.

Even regardless of performance, it would be quite nice to have! I've been meaning[1] to build a small prototype to do something clever[2] with it, but didn't have the time yet. Feel free to steal these ideas :)

[1]: https://scribbles.pascalhertleif.de/impl-virtual-dom-cli-lib... [2]: https://scribbles.pascalhertleif.de/rich-diffs-of-rendered-h...

Re:Announcing Rust 1.14 by topspin on Thursday December 22, 2016 @03:33PM (#13240146)
Even without the ? sugar I don't see what's so offensive about this; the least little formatting effort produces:

    let id = node.parent().unwrap()
                 .parent().unwrap()
                 .data_get("id").unwrap()
                 .parse::<usize>().unwrap();
If there is something horrible about that I don't see it.
Re:Announcing Rust 1.14 by stymaar on Thursday December 22, 2016 @05:48PM (#13241098)
Unwrap will panic if no parent is found, whereas the `?` operator allows for a clean handling of this situation.

Dealing with this case properly without using a single `?`, gets really ugly with a lot of nested

    if let Some(parent) = node.parent() {
Re:Announcing Rust 1.14 by ballpointcarrot on Thursday December 22, 2016 @03:51PM (#13240283)
Some people prefer their languages to be more concise, and the safety of the the option objects (and the requirement of the unwrap() calls in the function chain) feels unwieldy. Moreover, as the unwrap() calls could panic if there's no attribute, the more complete handling of it is even longer (match calls and following the successful path).

This could be a function to isolate just the attribute you need, but seeing the example as it is presented can be off-putting from people used to scripting languages like ruby/python, which are more concise.

Re:Announcing Rust 1.14 by 102030485868 on Thursday December 22, 2016 @03:42PM (#13240213)
It hurts to type sometimes. Typing at work isn't really an issue since I have an ergonomic keyboard, but big side projects can sometimes hurt.

? is far easier and faster to type than .unwrap().

So no, there's nothing horrible that I see, there is something horrible that I feel though.

Re:Announcing Rust 1.14 by oblio on Thursday December 22, 2016 @03:49PM (#13240268)
> If there is something horrible about that I don't see it.

Isn't unwrap():

a) boilerplate

b) repetitive?

Re:Announcing Rust 1.14 by steveklabnik on Thursday December 22, 2016 @03:52PM (#13240295)
It's boilerplate in a certain sense. Those are all points where what you've written might not work. Rust makes you deal with possible problems _somehow_. Specifically, this is a replacement for null. If you legitimately don't care, then it can feel boilerplate-y, but it also lets you start by not having to care, and then searching for unwrap and replacing it with better handling.

In other words, it's boilerplate here because this particular operation has many possible failure modes, and Rust is pointing those out to you.

It's repetitive in this particular instance, but doesn't have to be in general.

Re:Announcing Rust 1.14 by stymaar on Thursday December 22, 2016 @05:57PM (#13241158)
c) a bad practice since it will make your program crash is a node has no parent.

if the node always having a parent is a invariant of your application, you should use `expect` with a clear error message when the invariant is broken.

if your node can have no parents then you want to handle this case explicitly with a `if let Some(parent) = node.parent() {` pattern.

Re:Announcing Rust 1.14 by leeoniya on Thursday December 22, 2016 @03:05PM (#13239933)
> A rust vdom wasm library would probably be blazing fast.

Most of the cost of vdom is the resulting DOM ops and reflow/restyle the browser needs to do. It may be faster than the fastest today, but you're unlikely to see any kind of performance leap or even anything noticeable.

Re:Announcing Rust 1.14 by lobster_johnson on Thursday December 22, 2016 @03:20PM (#13240041)
The "?" operator looks like a more specialized, error/success-specific version of monads. Has there been any discussion about generalizing everything into monad-style chaining operators?

I see that there is some discussion here [1], including a suggestion to add a Haskell-style "do" block. But that's just one guy's blog post.

[1] https://m4rw3r.github.io/rust-questionmark-operator.

Re:Announcing Rust 1.14 by steveklabnik on Thursday December 22, 2016 @03:23PM (#13240075)
Yes, that was endlessly discussed as part of all of this. The TL;DR is: it's not simple. ? is very straightforward and helps people out a lot today. So if and when (and it is a big if) Rust ever gets do notation, this might be a little redundant, but blocking one of people's largest pain points today is worth the (possible) little bit of redundancy.
Re:Announcing Rust 1.14 by lobster_johnson on Thursday December 22, 2016 @04:36PM (#13240597)
Thanks, that makes perfect sense. I haven't followed the internal discussion at all.

It sounds like there are some that would like to move Rust in the direction of becoming more Haskell/ML-like (which is to say more purely functional) rather than the current balance between imperative and functional? Is there any internal tension in that regard?

Re:Announcing Rust 1.14 by steveklabnik on Thursday December 22, 2016 @05:03PM (#13240775)
It's not even a philosophical difference, really, there are technical objections that would have to be addressed somehow before it would even be possible, let alone figure out if it's a feature that's desired. These involve things like "it's still unclear if HKT is going to end up in Rust or not, and how", which is a pre-requirement, as just one example.
Re:Announcing Rust 1.14 by mathw on Friday December 23, 2016 @03:32AM (#13243678)
I believe one of the biggest obstacles to a general monad system in Rust is that the type system can't actually handle it at the moment - it requires higher-kinded types, which may or may not ever actually be added to the language.
Re:Announcing Rust 1.14 by GolDDranks on Friday December 23, 2016 @05:31AM (#13244069)
There's another problem too: a monad trait would unify types behind a monadic interface, but many things that in Haskell are types, are traits in Rust. This is because Rust requires a single type to have uniform memory representation and to be statically dispatchable (save for trait objects). So in Rust, there is not just an "iterator type", but multiple types that have the Iterator trait. The same goes for Future trait.

This leads to a situation where, if there were to be a monad trait, that trait should abstract over not only types, but another traits generically.

Not to speak about some other hurdles, like lifetimes. The design space is inherently more convoluted than in languages with boxed, possibly dynamically dispatched types. It may be possible, but it's not easy, and we can't copy the monads straightforwardly from Haskell or Scala.

Re:Announcing Rust 1.14 by mathw on Friday December 23, 2016 @06:06AM (#13244181)
Good point! A whole other set of things also need to be hoisted to make this work.
Re:Announcing Rust 1.14 by tatterdemalion on Thursday December 22, 2016 @03:34PM (#13240148)
The semantics of ? are different from monadic bind and fit better to Rust's language context of strictness, imperativeness, and mutability than Monad (which is a problematic type class in Rust for some separate reasons).
Re:Announcing Rust 1.14 by libria on Thursday December 22, 2016 @03:04PM (#13239928)
Swell! I was hoping that existed in this language. The rest of the code looks concise FWIW, https://github.com/tcr/rust-todomvc/blob/master/src/main.rs
Re:Announcing Rust 1.14 by steveklabnik on Thursday December 22, 2016 @03:06PM (#13239948)
Small note, we don't _technically have to stabilize Carrier to land this, just implement it for Option. (yay standard library types) However, ? for Option is a bit controversial, so we'll see...
Re:Announcing Rust 1.14 by masklinn on Thursday December 22, 2016 @04:52PM (#13240694)
> However, ? for Option is a bit controversial

Because people wouldn't expect it to return due to behaviour of existing languages yet that's what it does in Rust?

Re:Announcing Rust 1.14 by Rusky on Thursday December 22, 2016 @04:59PM (#13240747)
More because Option is not really intended to handle errors while Result and ? are.
Re:Announcing Rust 1.14 by steveklabnik on Thursday December 22, 2016 @05:07PM (#13240803)
Yes, that's correct. If we make ? go to more than just result, you could implement it for any type, and it's not totally clear that changing it from "error handling" to "general control flow" is a good idea.

Many are in favor of doing it, though. I'm personally against it.

Re:Announcing Rust 1.14 by int_19h on Thursday December 22, 2016 @08:16PM (#13242018)
Whether it's ? or something else, having a very concise form of syntactic sugar to unwrap option types is desirable. Most languages that don't have it, eventually acquire it due to demand.
Re:Announcing Rust 1.14 by echelon on Thursday December 22, 2016 @02:57PM (#13239882)
This is NOT idiomatic Rust. If you're new to Rust, please don't let this give you a taste for the language. Clean Rust code looks very elegant.
Re:Announcing Rust 1.14 by netcraft on Thursday December 22, 2016 @03:35PM (#13240157)
how would this be written idiomatically today?
Re:Announcing Rust 1.14 by echelon on Thursday December 22, 2016 @04:06PM (#13240393)
There are so many nice options here.

For the cleanest code, I would use `?` and a `From` impl for the error transformation.

If I didn't want to use `From`, I'd use `map_err`, `and_then`, etc. Both the Result and Option types have wonderful interfaces for chaining.

Re:Announcing Rust 1.14 by lisivka on Thursday December 22, 2016 @03:56PM (#13240318)
With proper error handling instead of unwrap(), which means "I don't care about errors here".
Re:Announcing Rust 1.14 by Dylan16807 on Thursday December 22, 2016 @04:16PM (#13240461)
What does proper error handling look like when I want to use the same "invalid node" error for most of those unwrap calls?
Re:Announcing Rust 1.14 by staticassertion on Thursday December 22, 2016 @07:28PM (#13241782)
You can use map or and_then, you can write a macro, you can use try!, you can use if let, etc. There are lots of ways to handle errors in rust.
Re:Announcing Rust 1.14 by EugeneOZ on Thursday December 22, 2016 @04:52PM (#13240696)
Use "try" or "?" if possible, pattern matching otherwise.
Re:Announcing Rust 1.14 by cgag on Thursday December 22, 2016 @03:17PM (#13240019)
I wish it were u and e rather than unwrap/expect. Or just "yolo" or "!" or something.

Is there a way to rename / alias methods like that?

edit: I see ? is going to be a trait you can implement to do that, so that'll be nice (probably).

Re:Announcing Rust 1.14 by killercup on Thursday December 22, 2016 @03:23PM (#13240078)
There's a crate for that™: https://crates.io/crates/yolo
Re:Announcing Rust 1.14 by openasocket on Thursday December 22, 2016 @03:21PM (#13240048)
There is: make a trait called SimplerUnwrap<T> or something with the method u(self) -> T. Implement it for Option<T> and you are good to go!
Re:Announcing Rust 1.14 by lisivka on Thursday December 22, 2016 @03:58PM (#13240337)
Why not use C instead? I see no benefit to use complicated language which cares about errors and then completely ignore that care.
Re:Announcing Rust 1.14 by openasocket on Thursday December 22, 2016 @05:06PM (#13240796)
You're not ignoring errors with unwrap(): if there is an error, the program will print out a stack trace and exit. If you have some operation that could potentially fail but you know it won't (like if you are sure that node has a parent) what do you do? If the operation actually does return an error, it means what you thought to be true was wrong, and in some cases that means something has gone seriously wrong, and your program should immediately stop so you can debug it. You can try to handle the error yourself, using try! or the ? operator, but unless you plan to be able to recover from that error, or you want to output a more end-user-friendly error message than a stack trace, what's the point?
Re:Announcing Rust 1.14 by Rusky on Thursday December 22, 2016 @04:19PM (#13240480)
Because while you may not care at first, you will later, and Rust will help you out then.
Re:Announcing Rust 1.14 by lisivka on Thursday December 22, 2016 @03:54PM (#13240299)
Every unwrap() is the point, where you need to check return value for an error, unless you are writing one-time code.

If you don't care about errors, don't use Rust. Use C instead. Simple solution.

Re:Announcing Rust 1.14 by openasocket on Thursday December 22, 2016 @03:23PM (#13240080)
I'd like to see some sort of macro for DOM navigation that takes a node and an XPATH expression or something, and the macro expands it to the above. Never played around with macros in Rust, so I don't know how involved that would be, or even if it would be possible.
Re:Announcing Rust 1.14 by steveklabnik on Thursday December 22, 2016 @03:26PM (#13240100)
That sounds very possible.
Re:Announcing Rust 1.14 by jcurbo on Thursday December 22, 2016 @04:46PM (#13240661)
So, I see tons of unwrap() in Rust code I've looked at in the wild. I understand the concept of unwrap() and Option types. What would this type of code look like with proper error handling? It seems like it'd devolve into a ton of if-then statements like in C.
Re:Announcing Rust 1.14 by akiselev on Thursday December 22, 2016 @05:31PM (#13240978)
First, you'd use match statements instead of if-then so the worst case scenario already looks much better than in C, especially since Rust has enum decomposition and makes expressions first class (you can do let y = if (...) { y } else { z }). There are a lot of ways of combining control flow and assignment operators to decompose union types like Option and Result (which is Option with error E instead of None). For example, you can do if let, while let, or let x = match(y) { ... } that allow you to handle errors without jumping through hoops like you would with C returns or C++ exceptions. For a library consumer this covers a large number of cases.

Second, most of the time you can use the ? operator to greatly simplify error handling. Any code path that shares the same error type can just pass through errors as if they were an exception. The ? operator is also aware of Into/From trait implementations so in many (or even majority of) cases you can use the ? operator even if the function you're calling has a different error type! If I remember correctly, you can even exploit the module system to provide a per-file Into/From implementation for your custom error type if it doesn't map cleanly across use cases (they just have to be in the same module as the type). Rust's zero cost abstractions and trait system make it really easy to reduce boilerplate without compromising on readibility or discoverability.

Third, the Result type has a number of functional operators for responding to and combining results. You can do Result.and_then(...) with a closure or function call to do something when there is no error and Result.or_else(...) to do something when there is an error. You can use the and or or operators to combine multiple results and there are many other utility functions that are universal to Rust error handling that make the entire experience very pleasant, composable, and readable with a little experience.

Re:Announcing Rust 1.14 by sidlls on Thursday December 22, 2016 @06:17PM (#13241319)
It does turn into if-then statements (some implicit, e.g. via try! or ?), if done properly. But I don't understand the objection or calling it "devolving". Errors have to be handled. This way is as efficient as any other. And, in my view, is cleaner than exception handling systems.
Re:Announcing Rust 1.14 by barrkel on Thursday December 22, 2016 @07:10PM (#13241684)
Errors have to be handled

This is a highly contentious assertion, and I don't think it's true for most programs, at least not in the way you're suggesting.

Most of the point of exceptions is that errors don't have to be handled; or rather, in most application software, almost all errors should be handled high up in the stack, near the event loop or server request / response dispatcher.

Re:Announcing Rust 1.14 by int_19h on Thursday December 22, 2016 @08:19PM (#13242032)
There are errors, and there are errors.

The kind of error that you'd handle high up the stack is, say, a general I/O error reading a file.

But when you look up an item in a collection (or a node in a DOM tree), and it is not found, the only place where you can sensibly handle that is right where the lookup happens, usually.

In a language that doesn't follow this philosophy, what you get instead are random NullPointerExceptions etc. If you go and look at a random C# or Java project on GitHub and check its tracker, you will see that - surprise, surprise! - that is exactly the type of error that shows up most.

Re:Announcing Rust 1.14 by barrkel on Friday December 23, 2016 @09:28AM (#13244847)
Null pointer exceptions are programming errors. They're usually better handled up the stack too.

An item not found in a collection or tree is not an error.

Re:Announcing Rust 1.14 by int_19h on Saturday December 24, 2016 @12:06AM (#13248668)
How exactly do you handle a null pointer exception?
Re:Announcing Rust 1.14 by barrkel on Sunday December 25, 2016 @01:22PM (#13254923)
Programming errors in production are usually handled by logging and then either exiting the process or going back around the request / response loop again (presuming global state hasn't been polluted, which is unusual in most modern application servers since they're usually stateless).

Null pointer exceptions are little different.

Re:Announcing Rust 1.14 by staticassertion on Thursday December 22, 2016 @07:30PM (#13241789)
This is the same in rust. Panics end at thread boundaries. So you'd have some event loop with threads / coroutines doing stuff and if they panic you handle it at the top level.