Hacker News new | past | comments | ask | show | jobs | submit login
Small exercises to get you used to reading and writing Rust code (github.com/carols10cents)
198 points by jupp0r on Feb 2, 2016 | hide | past | favorite | 36 comments



Hi! I'm the maintainer, and I'm glad yinz are enjoying rustlings :) I don't have any plans to do this for other languages, but please feel free to start your own!

If you have any particular Rust concepts that you'd like exercises for, please let me know here, file an issue, or even contribute an exercise! Errors that you've hit that took you out of your flow of working on something else are great candidates. I haven't been adding new exercises recently, but I would like to continue expanding.

Thanks! <3


This looks pretty neat! Would you recommend this for someone wanting to learn rust from scratch who is proficient in other languages?


That's basically my target audience :) It's not comprehensive yet, but the idea is to give you practice with Rust syntax and compiler errors.


> yinz

So a Pittsburgher with a passion for rust? Color me surprised.


Isn't it in the rust belt?


Indeed it is!


That's... the joke...


> So a Pittsburgher with a passion for rust? Color me surprised.

Guilty as charged :)


holy cow...just went looking for the origins of "yinz" and found out about "western pennsylvania english." My wife does the "needs/likes/wants + past participle" thing listed in the wikipedia article[1], and I never knew it was commonplace in some parts of the U.S.

1: https://en.wikipedia.org/wiki/Western_Pennsylvania_English


Hmmm... It is a common usage in Appalachian East Tennessee also. I suspect it's more generically Appalachian than just Pennsylvania.


Wikipedia says only outsiders say "Pittsburghese", but I've only ever heard it, and never "Western Pennsylvania English."

See also http://www.pittsburghese.com/


Thank you for doing this. Is there any chance we could see exercises involving lifetimes? I've found this is one of the trickiest Rust-only concepts to really understand.


I will try!!


Thanks for putting in the effort to produce this. I'm excited to learn Rust and to port a few projects over to it, but I haven't gotten traction to get past the first few steps. Your packaging-up of the examples is a great way to start.


For everyone looking for exercises in other languages, there is actually a project to curate exercises for programming languages called exercism.io

Maybe the OP could contribute their exercises there, since their Rust exercises were not very good when I tried them.


There's also Rosetta Code, which has tons of problems and language implementations. It's tons of fun to read through.


Yeah, I love exercism! There's definitely conceptual overlap, but IIRC the exercises in exercism were more of the variety where you're given a problem and you start from scratch, then others give feedback on your solution. I'm not sure if the intentionally-broken starting places of rustlings would fit in?


This looks great. Bookmarked for when I get around to learning Rust.


This is great. It took me from occasionally scrolling through the Rust book ("Rust looks cool, I'll use it eventually") and never writing a line of code to actually trying the language.


I feel the same way! Especially since this makes me a little more comfortable with the language, I might be more inclined to use it later too!


This is absolutely fantastic, well done.

One point that makes this particularly nice is the fact that for many languages of the compiled c++/Rust/etc kind there is a certain level of barrier for people coming from dynamic languages, and that is the non-existence of a repl where you can just try out basic idioms and constructs without having to manually set up a context. This kind of interactive environment kind of replaces that and therefore opens the language for much more people.


When you're a little bit more comfortable with Rust, Exercism (http://exercism.io/languages/rust) has a lot of cool toy problems to solve.


That's really cool. I like the whole idea behind this site, and apparently I missed it the times it's been on HN in the past. I'm totally going to use this for a few languages I've been meaning to pick up (including rust).


What about all the `unwraps`? I have nightmares from last time I tried to get into Rust and it was unwrapping everything all the way down.


The book has a section on error handling, starting with the basics: https://doc.rust-lang.org/book/error-handling.html


Handle errors, or use try! when you want to propagate errors to the caller:

https://doc.rust-lang.org/std/macro.try!.html


The only violator I found in these exercises is the one that uses mutexes. The suggested answer involves `val.lock().unwrap()`, which is (anecdotally) common in real Rust code. An error result of a mutex unlock implies that another thread panicked while holding the lock[0], so propagating that panic is likely the right answer in this case.

[0]: https://doc.rust-lang.org/std/sync/struct.Mutex.html#poisoni...


Often the quickest replacement for an `unwrap` is an `unwrap_or` with a sensible default.

Other times where error-handling is important use a `match` there and then. And don't nest the arms of the match instead use the return of the match expression.

On the other hand, I found `try!` pretty awkward. Especially since quite often you need to create custom errors alongside it and can go down a rabbit hole in which you have to read confusing compiler messages from within macros about traits not being implemented correctly, and lifetimes missing, etc.


You should handle errors, not unwrap them.


This is an excellent idea. Every language should have something like this.


Awesome !

I learned Kotlin by glancing at the doc and then completing the kotlin koans.

I look forward to doing the same with Rust.


These are very useful! Do you plan to add anything about object oriented approach of Rust (structs, traits, trait objects)? This topic (OOP approach) isn't documented in a focused fashion, especially since Rust lacks common inheritance (favoring composition).

I.e. some examples of how to do OOP "the Rust way".


Those would be great additions, I'll try to get to them soon!


Is there something like this for objective-C or swift? Still getting used to the syntax


Exactly what I needed.


excellent. Do you have the same for javascript ?




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

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

Search: