Live data from Hacker News

Learning Rust via Advent of Code

forrestthewoods.com

21–30 of 85 posts

Re: Learning Rust via Advent of Code

#21

I do appreciate the rule set that Rust forces the programmer into, however I keep coming back to it felt to me that I was being asked to conform to the language. Maybe I'm looking at it all wrong. When I was also learning other languages it felt to me some of them conformed to me more than others, and therefore were easier to write logic towards. Is low level safe systems programming the main/only use case or is ther…

Rust is a bit like Haskell: a very strong but also complicated type system that enables you to encode a lot of the logical restrictions into the API.

This is most notable immediately around the Result and Option types with their many modifiers (map, map_err, and_then, ...) and how strings work.

This can feel pretty awkward and cumbersome compared to other languages and that could be what you are describing.

It's not that way just for fun though: Most languages hide the underlying complexity from you and 'punish' you at runtime if you violate invariants by eg throwing exceptions. Rust forces you to deal with the invariants at compile time.

This makes you write safer code and prevents many errors, even "business logic" type errors if the API is right.

This is why "move fast and break things" aka prototyping is hard with Rust. (Note you can also circumvent many of these restrictions by using "unwrap()" et al a lot)

On the other hand, it gives you a high degree of confidence for writing and refactoring code: if it compiles, it probably works. (Assuming you were writing idiomatic code)

Rust is certainly not a good fit for every domain: the manual memory management is often overkill, and in very business logic heavy code like your typical CRUD apps, where most errors happen in logic that can't really be encoded in the type system, the value is limited.

Re: Learning Rust via Advent of Code

#22
I used AoC to try and learn a bit of Nim as I liked the idea of writting simple scripts that could run many times faster than Python. I somewhat regret it though.

I actually considered Rust given its popularity. I program C++ for a living, but I'd love to find some use for Rust.

Rust will probably my language of choice for this year's challenge.

Re: Learning Rust via Advent of Code

#23

I do appreciate the rule set that Rust forces the programmer into, however I keep coming back to it felt to me that I was being asked to conform to the language. Maybe I'm looking at it all wrong. When I was also learning other languages it felt to me some of them conformed to me more than others, and therefore were easier to write logic towards. Is low level safe systems programming the main/only use case or is ther…

It is true, you have to conform to Rust. Rust looks like an imperative and OO language, but it isn't fully either of these, so some of your intuition you may have from other languages won't be applicable.

• Rust requires more rigor around ownership and borrowing. You can't juggle pointers as freely as you'd in C, or reference everything everywhere like in GC languages.

• Rust doesn't have inheritance, so many design patterns common in C++ or Java need rethinking in Rust.

If you really insist on doing things exactly the C way, or exactly the OOP way, then some things may work, some can be fudged, but it's going to be awkward.

However, if you adopt Rust's style, which is mostly functional, with clearly thought out ownership, without too many cyclic dependencies, then everything nicely falls into place, and Rust becomes easy and productive.

Re: Learning Rust via Advent of Code

#24
post #2

What is HN-users view of learning a programming language through this kind of problem-solving? Is it limiting? Should this be used as a complement to a larger toy project?

A long, long time ago, I used Haskell to reimplement the examples from Software Tools (https://crsr.net/Programming_Languages/SoftwareTools/). It was how I got over the How-do-I-write-real-code-in-Haskell hump.

I strongly recommend it.

Re: Learning Rust via Advent of Code

#25

I used AoC to try and learn a bit of Nim as I liked the idea of writting simple scripts that could run many times faster than Python. I somewhat regret it though. I actually considered Rust given its popularity. I program C++ for a living, but I'd love to find some use for Rust. Rust will probably my language of choice for this year's challenge.

what kind of regrets do you have?

Re: Learning Rust via Advent of Code

#26
post #19

If you decide to learn Rust using AoC 2018, I highly recommend comparing your work with that of BurntSushi, who is known to write high quality code. https://github.com/BurntSushi/advent-of-code You develop Rust skills by writing your own and then hone your skills by learning from idiomatic Rust.

I learned Go first and then went to Rust, which was convenient because it appears sushi went the same route.

Re: Learning Rust via Advent of Code

#27

I do appreciate the rule set that Rust forces the programmer into, however I keep coming back to it felt to me that I was being asked to conform to the language. Maybe I'm looking at it all wrong. When I was also learning other languages it felt to me some of them conformed to me more than others, and therefore were easier to write logic towards. Is low level safe systems programming the main/only use case or is ther…

Rust is a bit like Haskell: a very strong but also complicated type system that enables you to encode a lot of the logical restrictions into the API. This is most notable immediately around the Result and Option types with their many modifiers (map, map_err, and_then, ...) and how strings work. This can feel pretty awkward and cumbersome compared to other languages and that could be what you are describing. It's not…

“If it compiles, it probably works.”

This has been so true in my experience. I find that it does take longer overall to write something in Rust, but I almost never have to root out strange errors from my code. In shorter files, I almost never have to use ‘cargo run --debug’ twice.

Re: Learning Rust via Advent of Code

#28
This is slightly off-topic, but someone mentioned that it would be a good idea to check out BurntSushi's solutions after trying it yourself which seems like a fantastic idea.

It would be cool if there was some resource that linked well-written, idiomatic solutions for other languages as well. Anyone know if this has been done?

Re: Learning Rust via Advent of Code

#29
post #18

Hey, I did the same thing this year! At least I tried to do AoC in Rust until I reached the problem which required using doubly-linked lists... Turns out it's not quite as easy as a Rust newbie would think. Regarding input parsing, I pretty quickly converged to using regexes and never looked back. I guess it's an acquired taste, but rubular.com is my best friend when it comes to it.

In my sibling post on performance I go into details on the doubly-linked list problem. https://www.forrestthewoods.com/blog/solving-advent-of-code-...

You're right that Rust doesn't make doubly-linked lists easy. For that problem I somewhat skirted the issue. For a later problem I built an Octree. I used an Rc>. That feels pretty gross, but I'm still not sure what the idiomatic pattern is. :(

Re: Learning Rust via Advent of Code

#30
post #19

If you decide to learn Rust using AoC 2018, I highly recommend comparing your work with that of BurntSushi, who is known to write high quality code. https://github.com/BurntSushi/advent-of-code You develop Rust skills by writing your own and then hone your skills by learning from idiomatic Rust.

I learned Go first and then went to Rust, which was convenient because it appears sushi went the same route.

I suspect his current route includes both Rust and Go. :)
Post reply on HN