News for hackers. Stuff that matters.
Re:Four Years of Rust by jnordwick on Wednesday May 15, 2019 @11:56AM (#19920665)
I really want to like Rust, and I've made a few attempts in the past to get involved - I wrote a database driver for a time series database (KDB) in pure Rust among a few other things. I've even done a couple deep dives into rustc to figure out the cause of some iterator slowness (it wasn't giving proper hinting in the LLVM IM).

However, I keep having to step back. And in this short time Rust has progressed a lot (and that isn't entirely a good thing either).

Rust might be the most liked language, but that is mostly a measure of how vocal its supporters are. It is also very underused for how liked it is. It is become a C-like Haskell in that people like it a lot, but don't use it for much important. Like Haskell, it might turn out to be a great research language, but not be used much in practice. I was hoping to see a language that was much more practical.

I'm about to take another shot at Rust, and I decided this time I should just ignore all the dislike of `unsafe` and just use it as much as I need to. I think I was making life more difficult on myself by trying to avoid it so much, but when you have a lot of pointers that have multiple refereces between then, you kind of have to. Doulble linked and mutually linked structures - where any pointer chance can lead to a mutation - are Rust's achillies heel. Unfortunately, you see that a lot in system-level programming. I write a lot of high performance code and low-latency networking. My main reason to use Rust is for performance reasons and a language with fewer footguns than C++., but something that doesn't completely remove my ability to produce good code.

However, I think Rust has moved too fast in the language department. It has changed and continues to change at a pace that prevent people how mainly use it as a tool for other things from becoming an idiomatic writer of the language. The idioms change too quickly.

The networking stack for efficient non-blocking event driven code still isn't where it needs to be. In Java for example, the NIO abstractions are much better. C++ just turns you over to the C system API. Rust is a disaster in the middle with kind of having a socket abstraction and telling you to just use libc for the rest. This has been an issue for years. Tokio is the wrong abstraction on the wrong level for me, and mio underlying library is kind of a mess with its token architecture, how it handles user defined events. Hopefully it has improved in the last couple years. It used to be slower than it needed to be.

"Slower than it needs to be" can describe a number of things in Rust, also the default hash implementation. Why would you use a secure hash for your default? If you really need that, you know you do, and 99.999% of the uses cases don't need that. I often need small key performance, which is basically the trade off they decided against.

C++ has blown its complexity budget a 100x over. Rust: hold my beer. In just a few year, Rust has kept up to c++ in that measurement (again, not a good thing). I'd like the language to stabilize a bit and work on what is currently there instead of running off to add support for yet another programming paradigm (async and userland threads - feel like a trip back in time to 20 years ago).

Side note on docs formatting: Reading the documentation is still incredibly difficult for me. It doesn't flow well, the top of the page has no index - you basically just have to scroll down. The page width is fixed to 960px so there are interior scroll bars on the code segments. No use of background color is mostly white so it is a big mess of black on while with some small indentation differences. Look at cppref and cpluplus: both have essentially broken down contents at the top making it easier to find what you are looking for, they both make use of color effectively. And everything on Rust Docs starts off fully expanded: you don't actually expand what you want to see instead of have to fold what you don't want to see (so first thing on every page, I have to immediately fold everything, then unfold the often too long context section). Seems backwards to me. And navigation on ther two sites is much better once you get deep on the docs. You can't go to a rust page doc and skim down a page or two looking for the call you want (e,g.: I need to find the mutating calls on a tree). On the cpp docs on both sites very eary, on rust it is tough. There is no "here are all the lookup methods" and "here at all the methods to look things up"

Re:Four Years of Rust by steveklabnik on Wednesday May 15, 2019 @12:19PM (#19920925)
There's a lot here, and I don't have a ton of time, and the important thing is that this is about your experience, so I won't say you're wrong. But to add some context on a few things:

> I'm about to take another shot at Rust, and I decided this time I should just ignore all the dislike of `unsafe` and just use it as much as I need to.

What it sounds like to me is that you struggled to learn Rust, and instead tried to write C or C++ in Rust. While they're similar languages, they're also very different. While it's true that these structures are used often in C and C++, they're used way less in Rust, and arguably, those kinds of structures are often bad for performance, not good. As always, it depends. It can be tough to communicate idiom, and to learn the "Rustic way" of doing things. This is true of every language, of course :)

> However, I think Rust has moved too fast in the language department. ... I'd like the language to stabilize a bit and work on what is currently there

This is actually the major theme of this year! https://blog.rust-lang.org/2019/04/23/roadmap.html

I disagree about overall complexity, but again, that's fairly subjective.

> Side note on docs formatting:

It is really, really hard to please everyone here. Some people love it, some people hate it. Sorry you hate it :/

Re:Four Years of Rust by est31 on Wednesday May 15, 2019 @02:53PM (#19922532)
> > However, I think Rust has moved too fast in the language department. ... I'd like the language to stabilize a bit and work on what is currently there

> This is actually the major theme of this year! https://blog.rust-lang.org/2019/04/23/roadmap.html

Major theme of the year, but not major theme of the language. One year of stability followed by one year of working on the next breaking idiom change followed by one year where that change is executed doesn't give you a stable language.

Also consider the pace of compiler updates. With C++, I can be very productive with my distro provided compiler. With Rust, the ecosystem is jumping onto new compiler versions so fast that you basically have to use rustup or a rolling release distro when you have a project with a medium amount of crates you depend on (hundreds). Cargo further has no way of recognizing the MSRV of crates and it's updating the index without asking so you can't even safely add new dependencies or update dependencies without increasing the MSRV of your Cargo.lock.

All of this can change, and maybe the rate of modifications will slow down. But right now it rather points towards the opposite direction, with the recent introduction of editions.

Re:Four Years of Rust by wtetzner on Wednesday May 15, 2019 @03:52PM (#19923074)
> Major theme of the year, but not major theme of the language. One year of stability followed by one year of working on the next breaking idiom change followed by one year where that change is executed doesn't give you a stable language.

Doesn't C++ have exactly this problem? It keeps adding new features, which change the language idioms. It actually seems much worse than Rust in this area, as Rust is mostly re-enforcing the existing idioms, while C++ is almost like two different languages now (well, probably even more than two).

Re:Four Years of Rust by dpc_pw on Wednesday May 15, 2019 @04:03PM (#19923167)
> C++ is almost like two different languages now (well, probably even more than two).

C++ is a different language in every company I worked for. Ha. Even a different language per team/project sometimes.

Tab vs spaces, different capitalization, different indentation style, exceptions vs no-exceptions, 0 is failure vs 1 is failure and then we like featureA, we don't like featureB, and so on.

Re:Four Years of Rust by est31 on Wednesday May 15, 2019 @04:29PM (#19923417)
The C++ approach of adding new idioms by making the language fatter is of course not as nice as the Rust approach of having breaking changes and disallowing the old idioms.

But then again, the people behind Rust have made usage of the try! macro harder for a questionably useful keyword that could have been named differently as well. And now it's part of the 2018 edition and you need two more characters to use the try! macro even though there are still cases where it's much better than ? and nothing is being done about it.

Re:Four Years of Rust by lzutao on Thursday May 16, 2019 @07:22AM (#19927946)
What are the cases when `try!` macro bettern than `?` operator? I'm a beginner to Rust so I don't know about that.
Re:Four Years of Rust by est31 on Thursday May 16, 2019 @12:37PM (#19930501)
Often when you are getting error messages you would like to find the place they originate. Failure provides backtraces but not everyone uses failure and tbh it's quite a heavy dependency, using serde and so on. With try!, you can easily shadow the macro to e.g. emit a panic. This can be done to basically any codebase using try!, even if it's not yours. Migrating it to use failure is much harder :).

It would be cool if you just had a compiler flag that invokes an user settable handler function every time Err() gets constructed or something like that.

To give concrete examples, I was developing a library to generate certificates [1]. Often you generate a wrong one though and the parser library you use for testing gives you a Err(BadDer). If that's everything you are getting it can be a bit tough to find out the origins. As it was using ? I had to grep for all the places returning a BadDer error and replace them with a panic. Shadowing try! would have been much easier.

[1]: https://github.com/est31/rcgen

Re:Four Years of Rust by lzutao on Thursday May 16, 2019 @11:16PM (#19935915)
Thanks through I still don't get it. I only know that you can get backtraces of errors by setting `RUST_BACKTRACE=1`.
Re:Four Years of Rust by est31 on Friday May 17, 2019 @06:04AM (#19938009)
That only gives you backtraces for panics, but not for errors based on the Result<,> type.
Re:Four Years of Rust by lzutao on Friday May 17, 2019 @08:31AM (#19938857)
I am not serious. Often in these cases I would use printf debugging to know where the errors came.

(I am still a Rust beginner).

Re:Four Years of Rust by pcwalton on Wednesday May 15, 2019 @01:19PM (#19921629)
Well, async I/O is important to get early. Otherwise we're going to have an entire ecosystem of synchronous networking code, and there's no going back from that. You can look at languages like Ruby for what happens if you don't get an async I/O story before the package ecosystem develops.
Re:Four Years of Rust by zbentley on Thursday May 16, 2019 @12:06AM (#19926051)
Java seems to have done pretty well, and it didn't have broad stdlib support for asynchronous I/O until a few years ago.
Re:Four Years of Rust by pjmlp on Thursday May 16, 2019 @01:01AM (#19926271)
NIO was introduced in Java 1.3.
Re:Four Years of Rust by jnordwick on Thursday May 16, 2019 @01:52AM (#19926497)
That's non blocking, not async as in the async that rust is developing.
Re:Four Years of Rust by pjmlp on Thursday May 16, 2019 @02:36AM (#19926692)
Non blocking code is asynchronous by definition, regardless of the syntax sugar.
Re:Four Years of Rust by ay on Wednesday May 15, 2019 @12:28PM (#19921030)
On practicality: I have written a ~30KLOC system for provisioning, configuration and management of a medium-size (500 switches) event network, with an ASP.net-like web frontend GUI, all entirely in Rust with PostgreSQL backend. The system was used for the past two years at an event for 20K people.

So it is very practical stuff for me :)

The biggest difficulty was absence of something analogous to ASP.net, so I had to invent my own... but even there it proved an excellent and practical choice, the macro system is very expressive and the strong typing gives a lot of confidence when refactoring.

Re:Four Years of Rust by sriram_malhar on Wednesday May 15, 2019 @12:36PM (#19921116)
You speak of low latency code and doubly linked lists in the same breath.

Personally, I have moved from linked data structures to more cache friendly data structures, which means contiguous spaces. No jumping around. It's very liberating.

Re:Four Years of Rust by davemp on Wednesday May 15, 2019 @01:34PM (#19921778)
Doubly linked lists are used all over for low level programming. For low access, large dynamic data they're a pretty good solution AFAIK.
Re:Four Years of Rust by DblPlusUngood on Wednesday May 15, 2019 @04:55PM (#19923681)
You are absolutely right. They are used everywhere in the Linux, OpenBSD, and FreeBSD kernels, and likely many other kernels.

Replacing large linked lists with arrays is rarely an actual win. With an array, insertion and deletion become far more expensive, virtual memory is more likely to become fragmented or grow monotonically, and the cache misses avoided are almost certainly irrelevant to total performance.

Re:Four Years of Rust by sriram_malhar on Thursday May 16, 2019 @01:11PM (#19930814)
I wasn't speaking of low-level programming. I was referring to very low-latency code.

Highly performant code tends to get complex. One way to ensure high performance and reduce complexity is to deal with values, not pointers; it is good for concurrency (no aliasing), excellent for cache locality, and the value is right there. Multi-versioned stable values work excellently, like RCU-locks in the Linux kernel.

Re:Four Years of Rust by foldr on Wednesday May 15, 2019 @12:59PM (#19921395)
Doubly linked lists are perhaps not the best example, but there are lots of practical data structures that aren't compatible with the single ownership restriction. It's possible to implement them in safe Rust using various techniques, but it's not ideal.

Then there is the fact that mutation makes things even harder. So for example, you can easily implement a tree using Box to hold pointers to child nodes, but you'll then need a little bit of unsafe code to write a mutable iterator over the tree. (Even mutable iterators for slices require unsafe code under the hood.)

Re:Four Years of Rust by estebank on Wednesday May 15, 2019 @01:15PM (#19921592)
And that should be fine. unsafe is not something to be forbidden, it is a marker for "here-be-dragons, as the compiler can't check that all invariants are correct when you dereference a raw pointer or modify a mutable static variable". In reality I've found the need for unsafe to be minimal, either when doing something that is actually forbidden for good reason or interacting with C libraries.
Re:Four Years of Rust by foldr on Wednesday May 15, 2019 @01:23PM (#19921669)
Yes, the OP said explicitly that he decided to use unsafe to deal with these cases.

>I decided this time I should just ignore all the dislike of `unsafe` and just use it as much as I need to. I think I was making life more difficult on myself by trying to avoid it so much, but when you have a lot of pointers that have multiple refereces between then, you kind of have to. Doulble linked and mutually linked structures - where any pointer chance can lead to a mutation - are Rust's achillies heel.

Sriram_malhar pointed out that doubly linked lists are usually not a good data structure to use anyway. I was pointing out that there are lots of more useful data structures that raise similar issues in Rust.

I don't think that using unsafe is "fine", necessarily. It is much better to implement a data structure on top of safe Rust if at all possible.