Live data from Hacker News

Learning Rust via Advent of Code

forrestthewoods.com

31–40 of 85 posts

Re: Learning Rust via Advent of Code

#31

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?

I haven't used Nim for anything else since then. If I had to give a simplified critique, the language in itself is still springing to life and the user base and documentation are small.

I currently don't have the time to sink into something that could end up being a gimmick only, while there is some hardcore optimization needed in one of our services that could be approached through a better parallelization using Rust.

While from my point of view Nim is only interesting, Rust on the other hand seems promising. (I don't mean to compare different purpose languages, the problem stands on my spare time to study and the company needs, if that makes sense.)

Re: Learning Rust via Advent of Code

#32
For sorting sequential fields you can chain comparisons with .then():

    struct Date {
        year: u32,
        month: u32,
        day: u32,
    }
    
    let mut vec: Vec = Vec::new();
    
    vec.sort_by(|a,b| {
       a.year.cmp(&b.year)
        .then(a.month.cmp(&b.month))
        .then(a.day.cmp(&b.day))
    });
Alternatively you can derive PartialEq, PartialOrd, Eq and Ord for your struct, which will produce a lexicographic ordering based on the top-to-bottom declaration order of the struct's members:

    #[derive(PartialEq, PartialOrd, Eq, Ord)]
    struct Date {
        year: u32,
        month: u32,
        day: u32,
    }

Re: Learning Rust via Advent of Code

#33
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.

Fellow Gopher here interested in learning Rust. Any recommendations?

Re: Learning Rust via Advent of Code

#34
I used AoC to brush up on my python skills. Which was quite good - as soon I am participating in a programming competition and my team is using Python.

In general AoC is just great for exploring new langs or brushing up on ones you haven't used for some time.

Re: Learning Rust via Advent of Code

#35

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…

To add a bit to what others have said, at some point, once you internalize what Rust asks of you, it no longer feels like conformance; you just write stuff in a Rust-y way, and things get a lot easier and more fluid. This is sort of true in all languages, ("you can write Java in any language"), but Rust doesn't let you get away with writing some types of code as much as other languages do.

Like many things, it's a tradeoff: is the cost of getting up to speed worth the benefits Rust gives? Like any tradeoff, some will say yes, some will say no.

Re: Learning Rust via Advent of Code

#36
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. :(

In practice, most data structures like that in Rust written to be efficient for real-world make judicious use of unsafe. For something like Advent of Code, `Rc` is probably the correct way to go.

Re: Learning Rust via Advent of Code

#37
Slightly OT: this seems like a good way to learn to program. My wife has been trying to learn to program with python but all the tutorials even programming 101 tutorials these days actually seem more advanced than I would expect. For example they assume someone gets what a string is and how it works with surprisingly very little explanation, then overload the student by comparing the difference between string objects in Python 2 vs Python 3 and Unicode va non-Unicode! Woah object, Unicode, what?

Are there any good resources for progressively learning Python by doing something like AOC but starting from nothing and as a tutorial and explaining the basics of what a string is etc as it goes?

Re: Learning Rust via Advent of Code

#38
post #37

Slightly OT: this seems like a good way to learn to program. My wife has been trying to learn to program with python but all the tutorials even programming 101 tutorials these days actually seem more advanced than I would expect. For example they assume someone gets what a string is and how it works with surprisingly very little explanation, then overload the student by comparing the difference between string objects…

codingbat.com, the Python exercises. They have all the background information needed in nearby links (e.g. I have an exercise with strings how do I use those), and build experience nice and easy

Re: Learning Rust via Advent of Code

#39
post #20

" Learning a new language via Advent of Code turned out to be a great idea. I highly recommend it. " I'll second this: Using a set of progressive problems is an excellent way to get used to actually writing code in a language. " My first stumbling block was parsing. Almost every AoC problem starts with parsing lines of text from an input file. " Scheme. Guile. The PEG parsing module. (There's no kill like overkill.)

> I'll second this: Using a set of progressive problems is an excellent way to get used to actually writing code in a language.

IME AoC isn't really progressive though, the problems ramp up and down pretty dramatically.

> Scheme. Guile. The PEG parsing module. (There's no kill like overkill.)

Rust has several pretty good parsing libraries e.g. nom, lalrpop, pest, …

Re: Learning Rust via Advent of Code

#40

For sorting sequential fields you can chain comparisons with .then(): struct Date { year: u32, month: u32, day: u32, } let mut vec: Vec = Vec::new(); vec.sort_by(|a,b| { a.year.cmp(&b.year) .then(a.month.cmp(&b.month)) .then(a.day.cmp(&b.day)) }); Alternatively you can derive PartialEq, PartialOrd, Eq and Ord for your struct, which will produce a lexicographic ordering based on the top-to-bottom declaration order of…

Or you can use sort_by_key and extract the relevant sorting key as a tuple (or any other Ord structure) e.g.

    vec.sort_by_key(|d| (d.year, d.month, d.day))
sort_by is more flexible as it works fine with borrows, but when sorting on a series of integer values or references sort_by_key is great.

> Alternatively you can derive PartialCmp for your struct, which will produce a lexicographic ordering based on the top-to-bottom declaration order of the struct's members:

Do you mean PartialOrd? partial_cmp is the method. And `sort` requires absolute ordering (Ord) not just partial.

Post reply on HN