Live data from Hacker News

Ask HN: What are some good Rust code samples?

news.ycombinator.com

11–20 of 25 posts

Re: Ask HN: What are some good Rust code samples?

#11
Rather than giving always the same crate or author names, it's probably more interesting to learn to recognize repositories which have a higher probability of being "good rust code", so that you can search yourself (starting with repos of well used and well known libraries and programs).

IMO good rust code often

- has most types, functions, macro definitions, etc. correctly commented (don't bother reading code when there are pages without comments)

- has a lot of code snippets in function comments

- has some test units

- has no unjustified `unwrap` and `expect`

- has understandable error management

- has "bench", "tests" and "examples" directories (they're not always necessary but if you see them, the author knows the tools and made some efforts in building a better crate)

- has a proper readme and lib level comments (if it's a lib): people who take pride in a readable documentation often take pride in a good code

I'd also suggest to first look at code which is neither very old (people learnt since and new features were added to the language) nor so recent it didn't mature

Re: Ask HN: What are some good Rust code samples?

#12
(I'm going to write about Rust learning resources in general, so this may not fit the question, but believe the concepts are still worth considering)

Rust is a very hard language to learn, not only because of the inherent difficulty, but also because of the lack of a structured way to approach the learning itself.

Reading codebases is a very good practice in programming, but I don't think it benefits below the intermediate (if we put it this way) level, as you may not even notice many of the design choices.

You're going to need a lot of time to become fluent, so it's crucial to clarify what's your target. If you just want to play with the language, anything will do; if you're serious, it's important to follow a path that it's stimulating, otherwise, you will just get bored and stop.

I've tried Rustlings as a beginner, but it wasn't very stimulating. Gjengset's videos are extremely slow - they could be cut to a third, and still keep the same content; again, not very stimulating.

For beginners, a possible path is to study the reference book (there's no escape from that), then optionally find a "strongly guided" projects-based book, then find some projects to work on (which may include "weakly guided" project-based books).

A project-based book I suggest is "Rust Programming By Example" (Packt). The code quality is not very good, but it has several different projects to play with.

Another book that may be interesting is (I haven't read it yet) is "Hands-on Rust- Effective Learning Through 2D Game development" (Pragmatic Bookshelf).

After you're past the strongly guided projects phase, you may start to work on weakly guided ones. Two very good books (by the same author) are "Mazes for Programmers" (Pragmatic Bookshelf) and "The Ray Tracer Challenge" (Pragmatic Bookshelf).

After that, sky's the limit, as you'll be surely ready to join established projects. The "Rust weekly" newsletter publishes projects who welcome beginners.

You may want to read, at some point, "Programming Rust" (O'Reilly). It's tedious to read it after the reference book, but it has a different, slightly more in depth, view on the Rust language.

Re: Ask HN: What are some good Rust code samples?

#14
I’ve written a book “Rust From the Ground Up” which teaches you to write a real Rust program from scratch in each chapter. I used the BSD sources to rewrite classic Unix utilities (head, wc, cat, etc) from C into idiomatic Rust. Rust has an incredibly steep learning curve and I make it easier by avoiding more advanced topics like lifetimes. I’ve published the first 3 chapters with the fourth coming in January.

https://rftgu.rs/

Re: Ask HN: What are some good Rust code samples?

#15
I really liked Programming Rust (got the 2nd edition). It's been a few months since I read it, so be warned, that I might misremember some parts of it

https://www.oreilly.com/library/view/programming-rust-2nd/97...

The book gives good visual explanations for how each of the code examples and it spends a good amount of time showing the relationship between the code examples and the borrow checker, OS, etc.

As most language books it spends a lot of time going through all the types, which I've always found incredibly boring - the same thing is the case with this book.

I'd recommend reading about strings and slices before moving on to the examples, but spread out the rest of those chapters in between the more interesting ones

Re: Ask HN: What are some good Rust code samples?

#16
post #2

Good code by BurntSushi (a.k.a. Andrew Gallant) including ripgrep, walkdir, quickcheck, xsv, bstr, etc. https://github.com/BurntSushi

ripgrep is great! Also, I’ve learned a lot by simply looking at his advent of code repository which contains a lot of small challenges that will help you learn some best practices on idiomatic Rust and at the same time will not get you overwhelmed by the complexity of a big project.

Re: Ask HN: What are some good Rust code samples?

#19
I did advent of code this year in rust (not finished yet, but such is christmas). I found it quite a good way to quickly get a "functional" appreciation for the language. I would preface that by saying that you should read the first few chapters of the rust book. Especially you need to read the introductory chapter about borrowing (chapter 4 I think?) , otherwise you will find it borderline impossible to work with it.
Post reply on HN