7 ms·
Small exercises to get you used to reading and writing Rust code
- adrianN 11y agoThis 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.
- danbolt 11y agoI 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!
- on_and_off 11y agoAwesome ! I learned Kotlin by glancing at the doc and then completing the kotlin koans. I look forward to doing the same with Rust.
- reacweb 11y agoexcellent. Do you have the same for javascript ?
- softinio 11y agoThis is an excellent idea. Every language should have something like this.
- speg 11y agoWhat 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.
- vbezhenar 11y agoYou should handle errors, not unwrap them.
- danieldk 11y agoHandle errors, or use try! when you want to propagate errors to the caller: https://doc.rust-lang.org/std/macro.try!.html https://doc.rust-lang.org/std/macro.try!.html
- burntsushi 11y agoThe book has a section on error handling, starting with the basics: https://doc.rust-lang.org/book/error-handling.html https://doc.rust-lang.org/book/error-handling.html
- sanderjd 11y agoThe 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#poisoning https://doc.rust-lang.org/std/sync/struct.Mutex.html#poisoni...
- lhnz 11y agoOften 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.
- pj_mukh 11y agoIs there something like this for objective-C or swift? Still getting used to the syntax
- carols10cents 11y agoHi! 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
- cgh 11y agoThank 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.
- carols10cents 11y agoI will try!!
- MuEta 11y ago> yinz So a Pittsburgher with a passion for rust? Color me surprised.
- dikaiosune 11y agoIsn't it in the rust belt?
- carols10cents 11y agoIndeed it is!
- solipsism 11y agoThat's... the joke...
- carols10cents 11y ago> So a Pittsburgher with a passion for rust? Color me surprised. Guilty as charged :)
- josep2 11y agoExactly what I needed.
- shmerl 11y agoThese 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".
- carols10cents 11y agoThose would be great additions, I'll try to get to them soon!
- elcapitan 11y agoThis 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.
- andrey-g 11y agoWhen you're a little bit more comfortable with Rust, Exercism (http://exercism.io/languages/rust http://exercism.io/languages/rust) has a lot of cool toy problems to solve.
- kbenson 11y agoThat'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).