News for hackers. Stuff that matters.
Re:The struggle with Rust by trishume on Tuesday January 24, 2017 @08:05AM (#13470975)
Agreed. Rust is different from other languages in that it is HARD to learn. It took me 3 projects and thousands of lines of code before I finally felt like I'm okay at Rust. Each project involved tons of compiler fighting that I now know how to avoid or solve.

The problem is that instead of a general technique for solving borrow checker issues, every issue has a different specific technique. Hash map matches (like the article) require the entry API. Multiple mutable pointers into a buffer require either using a typed Vec or Vec#split_off. Cyclic data structures require an Arena or Rc<Refcell<>>. String casting issues sometimes require .map(|x| &x). Global state requires lazy_static. Nice error handling requires error_chain. When you run into problems with "Send" you need a Mutex. The list goes on...

It's like pure lazy functional programming in Haskell in that unlike other languages you can't just pick it up based on previous knowledge and patterns. You need an entirely new toolbox of pure algorithms and data structures, or in this case borrow-safe algorithms and data structures.

Re:The struggle with Rust by wyldfire on Tuesday January 24, 2017 @10:43AM (#13472211)
> Agreed. Rust is different from other languages in that it is HARD to learn.

Don't you think C/C++ are hard to learn too? C++ has a vast surface area and I think it's unlikely that most people who use it understand much of it. When I code in C, I feel like I have to be paranoid because it's so easy to forget a path that doesn't recover allocated resources. But if I'm not paranoid, I just leak the resources and don't care about it (ever, or until many months or weeks later when the problem's discovered). So the easiness is illusory IMO. It seems to me that all of those other resource leaks are just leaking too slowly for us to discover yet.

> Each project involved tons of compiler fighting that I now know how to avoid or solve.

As frustrating as it is, it's easier than struggling with heisenbugs. Those can easily eat up days trying to reproduce issues.

Re:The struggle with Rust by adrianN on Tuesday January 24, 2017 @11:04AM (#13472425)
I think it's easier to get started with C++ because it contains simpler languages. You don't have to start with template metaprogramming, you can be productive with a small subset of C++.
Re:The struggle with Rust by flukus on Tuesday January 24, 2017 @11:35PM (#13478555)
C++ is hard, but C is about the simplest language in modern use.
Re:The struggle with Rust by ajmurmann on Tuesday January 24, 2017 @10:18AM (#13471960)
The issues and solutions you list sound super valuable. Do you know if there is a write up about those issues somewhere or did you just learn through blood and sweat?
Re:The struggle with Rust by nebopolis on Tuesday January 24, 2017 @10:51AM (#13472297)
Learning Rust With Entirely Too Many Linked Lists (http://cglab.ca/~abeinges/blah/too-many-lists/book/) is the best resource I've found for learning how to create data structures in Rust. It goes over a lot of the pain points of interacting with cyclic data structures.
Re:The struggle with Rust by petersp on Friday January 27, 2017 @02:22PM (#13501895)
Thank you for this pointer!
Re:The struggle with Rust by lhnz on Tuesday January 24, 2017 @10:34AM (#13472120)

  > Cyclic data structures require an Arena or Rc<Refcell<>>.
Is there a way of creating a cyclic data structure that doesn't involve these types?

I ask because I used the latter recently, after struggling to create lifetimes that were acceptable to the compiler. I only found it when I searched for 'cyclic data structures rust' on Google.

It would have been much nicer if the compiler could have noticed that I was trying to create a cyclic data structure and then have informed me on the different ways of doing so. Perhaps a misunderstanding on my behalf, but the error messages that I previously received felt like they were sending me on a wild goose chase.

I still don't think writing Rust code is hard, but there are definitely improvements which could be made to the compiler error messages.

Re:The struggle with Rust by readittwice on Tuesday January 24, 2017 @11:02AM (#13472402)
You could use id/vec indices, instead of directly pointing to your e.g. sucessors: "struct Node { successors: Vec<usize> }". This has the obvious disadvantage that deleting nodes is quite hard though.
Re:The struggle with Rust by mtanski on Tuesday January 24, 2017 @11:24AM (#13472681)
> This has the obvious disadvantage that deleting nodes is quite hard though

It's hard because now you're in charge of building a malloc(3) implementation. Depending how important this data structure is you might need to worry about 1. about fragmentation 2. free space managment 3. adjacency and cache effects

So you've traded on hard problem for another hard problem. Not something I'd call a win.

Re:The struggle with Rust by pcwalton on Tuesday January 24, 2017 @11:27AM (#13472717)
Just use petgraph. It manages all of this for you.
Re:The struggle with Rust by dman on Tuesday January 24, 2017 @01:31PM (#13474084)
I work on databases and I ran into similar problems implementing index data structures. While I understand petgraph solves some/all of these problems completely - it would be great if the barrier to writing trees / graphs from scratch in Rust would be lower than it is currently.
Re:The struggle with Rust by mtanski on Tuesday January 24, 2017 @02:11PM (#13474482)
Same problem. Working on an OLAP database toolkit (tried to in Rust)... writing typical database data structures is excruciating painful in Rust.

Trees, indexes (including bitmaps), buffer allocators and even just keeping state in streaming cursors (Volcano model). Are all pain in the ass in to implement.

In fact implementing a Cursor trait for like 10 different Cursor (SrcView, Join, Project, Compute, ...) became painful because each cursor struct has it's own complex lifetime params and those lifetimes were trying to leak into the main Cursor trait.

Right now I can't do that without resolving to runtime using RefCell. In my case the plans could be entirely materialized at compile time, so not really a win.

The situation won't get better until there's some kind of abstraction over lifetimes (HKT?).

Re:The struggle with Rust by readittwice on Thursday January 26, 2017 @03:51AM (#13489781)
Sure, I wouldn't recommend that solution if you have to delete single nodes a lot. But it is quite convenient if you don't need to do that.
Re:The struggle with Rust by jononor on Tuesday January 24, 2017 @10:22AM (#13472007)
Are these strategies for solving borrow checker issues documented anywhere? Sounds like a case for which a best-practice reference would be golden.
Re:The struggle with Rust by Manishearth on Tuesday January 24, 2017 @01:51PM (#13474284)
I wrote http://manishearth.github.io/blog/2015/05/27/wrapper-types-i... which explains the primitives that can be composed together to do things like this.
Re:The struggle with Rust by alkonaut on Tuesday January 24, 2017 @09:59AM (#13471768)
> Agreed. Rust is different from other languages in that it is HARD to learn

But do you learn to code in a different paradigm where you don't struggle by iterating on a compilable and runnable piece of code, but rather struggle on iterating on a non-compilable piece of code and "once it compiles, it works"?

Re:The struggle with Rust by AsyncAwait on Tuesday January 24, 2017 @11:57AM (#13473083)
Definitely and I'd say the struggle is only there for the first couple of weeks.

This will get shorter as tooling gets better this year.