Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: What are some good Rust code samples?
78 points by djangovm on Dec 29, 2021 | hide | past | favorite | 23 comments
Inspired by the python thread: https://news.ycombinator.com/item?id=29698198

What are some good code to read/channels to watch on twitch to learn basic to advanced rust?




I’d recommend working through rustlings rather than watching videos for basic rust understanding: https://github.com/rust-lang/rustlings. It is essentially a set of quick exercises to get you familiar with the language and how to fix common build errors.

For advanced Rust “Jon Gjengset” has posted a number of long-form videos on YouTube that are very good. Search for it.

For code examples, find a crate in an area that you are interested in and clone the repo. There are so many to choose from that it would be unfair to single anything out. Variation is good, not bad. If you see lots of generics and macros then skip it for early learning purposes.

Instead of writing something from scratch I’d recommend rewriting something you are familiar with in but in Rust. For example, if you did advent of code (if that is your thing) in python then write it in Rust and find a repo of someone who solved the same problems in Rust.

The Rust book is excellent. https://doc.rust-lang.org/stable/book/


> For advanced Rust “Jon Gjengset” has posted a number of long-form videos on YouTube that are very good. Search for it.

Jon has also released a book called "Rust for Rustaceans", aimed at intermediate ("experienced") developers, to provide them with both idiomatic and ergonomic rust solutions, as well as deeper understanding of the more advanced concerns of the language (macros, async, unsafe, ...).


Absolutely no... The book is not user friendly for new learners. It's good, but not for beginners. BTW It's 600 pages!


Strongly disagree. User friendly doesn't mean "short", it means "accessible for newcomers" in this context. And the rust book very much is that: it starts out extremely basic/simple and builds from there. Anecdotally and for many others it's been the best way to develop a solid understanding of the language.

Rust is somewhat "shortcut resistant" for new learners: you can pick it up by just starting coding after reading a few blog-post-length tutorials and backfilling understanding, the way many people do with other languages, but this typically takes much longer and incurs more frustration with Rust than, say, Python. That's because Rust takes longer to understand than most languages (I say this as someone who enjoys the language and expects it to become quite widely used, not as a criticism). Hence the book: it's long, but quite friendly. It's just not what you'd expect if you compare it to other learn-a-language-in-x-hours experiences.


I disagree - I found the book to be very valuable when learning Rust. I agree it would not be good if Rust is your first programming language. But I think many beginners to Rust are people who are already experienced programmers and basically just want to know some mental models for how Rust's memory model works, some useful idioms and patters, "how to do X in Rust", and how to get started.


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.


going through his past advent of code solutions is good too


For learning async Rust, mini-redis repo is hard to surpass: https://github.com/tokio-rs/mini-redis

The code is simple enough for beginners to follow, but also complex enough to demonstrate async Rust in the wild. And best of all, the code is heavily commented!

You can follow the official Tokio tutorial to implement mini-redis incrementally: https://tokio.rs/tokio/tutorial/setup


(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.


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/


Here is a peer-reviewed collection - https://github.com/mre/idiomatic-rust


I know it's not what you are looking for, but I am going to share that I have my eyes on the book "Hands-on Rust: Effective Learning through 2D Game Development and Play" [0]. I will start working through it by the mid-end of 2022.

I am always bored out by most programming language books because most of them are listing for the language's API. This is accessible through the docs. Instead, I like books, where I get to learn something new by actually doing something. That's why this book.

I also like books that teach you what the language can do, how it makes your quality of life better, or maybe ups my level by a lot. Two such books that come to mind are: 1)SICP 2)Programming in Haskell by Hutton.

I hope to learn not only Rust with it, but also 2D game programming- or at least a little of it.

I started with the Rust book earlier and made some headway, but quickly lost interest because I was far away from doing something with it, and also my work is far removed from Rust.

[0]: https://pragprog.com/titles/hwrust/hands-on-rust/


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


I haven't done much Rust but I started out here, thought it was a good general introduction; https://jesselawson.org/rust/getting-started-with-rust-by-bu...


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


I was just reading lua implemented in rust today. the codebase was pretty easy to read. felt like sharing

https://github.com/eatonphil/lust



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.


A friend of mine who is very much into learning rust is following https://twitter.com/RustRepos on twitter and says its a great way to see what's upcoming and learn from it.


Along the lines of "What's upcoming and learn from it", I've found the https://this-week-in-rust.org/ weekly newsletter valuable for this.


If you already know the language, try the standard library. it's very good to read.


It's often hard to follow though. It's probably easier for a new rustacian to read smaller repositories.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: