8 ms·
Exercises to Learn Rust
- mr-karan 2y agoThis is quite interesting! While we're on the topic, does anyone know of a similar set of exercises for learning Golang?
- hlfshell 2y agoGo by example is an established favorite. https://gobyexample.com/ https://gobyexample.com/
- __loam 2y agoI love go. Now to continue with modern systems languages, here's ziglings: https://codeberg.org/ziglings https://codeberg.org/ziglings
- ishaanbahal 2y agoHave been using go for 8 years now, and started with this. And to be quite honest, I still refer to this sometimes for quick lookup on certain things related to syntax, pools, waitgroups or channels. Beautifully done tutorial of Golang, can be done in a few hours and gets you a good base knowledge of the language. To the creator, a sincere thank you!
- sqkz 2y agoI would also highly recommend Go With Tests: https://quii.gitbook.io/learn-go-with-tests https://quii.gitbook.io/learn-go-with-tests
- kuxv 2y agohttps://exercism.org/ https://exercism.org/ offers exercises for multiple languages including Go.
- luyu_wu 2y agoTo anyone who has done this and rustlings, how do you think they compare? I've been very happy with rustlings, but I'm curious if it's worth it to finish these too!
- komali2 2y ago> To verify your solutions, we've provided a tool that will guide you through the course. It is the wr CLI (short for "workshop runner"). Install it with: > cargo install --locked workshop-runner I have no idea what this program / package manager is so I was going to throw my hands up and say great another mac-only tutorial, but apparently this is a package manager for rust, and also apparently it comes pre-installed on manjaro lol edit: I'm confused, on the home page I clicked "next" which takes me to this exercise: https://rust-exercises.com/01_intro/01_syntax https://rust-exercises.com/01_intro/01_syntax but on that ostensibly "first" exercise they write: > The previous task doesn't even qualify as an exercise, but it already exposed you to quite a bit of Rust syntax. Let's review the key bits! And keep referring to some prior exercise, that I can't find anywhere. Is there an "exercise 0" hiding somewhere?
- Jtsummers 2y ago> And keep referring to some prior exercise, that I can't find anywhere. Is there an "exercise 0" hiding somewhere? It's not hiding, they link to it. >> You can find the exercises in the companion GitHub repository. https://github.com/mainmatter/100-exercises-to-learn-rust https://github.com/mainmatter/100-exercises-to-learn-rust And then they tell you to complete the exercises for each section before continuing and tell you exactly where the first exercise is: > The exercise for this section is located in exercises/01_intro/00_welcome Read the section titled "Structure" for all of this information.
- deleted 2y ago[deleted]
- joshka 2y agoYou might find it helps to take one step back from the initial page and start at https://www.rust-lang.org/learn/get-started https://www.rust-lang.org/learn/get-started
- pelagicAustral 2y ago[flagged]
- AdamJacobMuller 2y agoI don't know rust, generally don't advocate writing things in rust but I find the people complaining about the people advocating for rust to be completely insufferable. People are talking about it because it's cool and new and shiny and they like it, you see it a lot because there's lots of people who find it cool and new and shiny.
- chainwax 2y agoBeing the first language to contest C++ in places like the kernal is a pretty big deal, no? It helps that the borrow checker is a new idea around being safe without gc.
- dehrmann 2y agoIt is, but I see people using it in places where Java would be good enough.
- drekipus 2y agoBut then they'd have to learn java
- AlotOfReading 2y agoJava is tremendously unfriendly to actually use. Sure, it's tolerable inside a corporate environment with established build/deployment systems, but there are few languages that make the "build a random repo off GitHub" hurdle higher than Java.
- brink 2y ago> I do not understand why there are so many zealots around the language. Neither did I. But then I took the time to learn the language, and I understood what all the hype was about. Maybe you should too, instead of just whining about it.
- veidr 2y agoWorth noting that this set of exercises was created by the author of "Zero to Production in Rust", the well-reviewed book with the crab-in-a-human-skull cover[1]. [1]: https://www.zero2prod.com https://www.zero2prod.com
- theonething 2y ago> crab-in-a-human-skull cover Anyone know the story behind this?
- chongli 2y agoRust community members are known Rustaceans which is a play on the word crustacean. Crabs are a well-known crustacean. Crab in a human skull is a visual metaphor for learning Rust.
- jjice 2y agoI got a Hamlet vibe from it when I picked the book up when it originally released. It could just be because it looks pretty sick though
- maxbond 2y agoAlas, poor Yorick; I knew him well, Ferris. (Slightly more seriously, the project to replace the borrow checker was called Polonius[1], so it wouldn't be the first Hamlet reference in Rust land.) [1] https://github.com/rust-lang/polonius https://github.com/rust-lang/polonius
- jonathankoren 2y agoTo follow up that movie reference with the response in a different movie where that line is misquoted, “Where’d you hear that, a renaissance festival?” I haven’t seen that movie a lot, but that scene (and really the entire movie) has been seared into my consciousness for 30 years. It was just the exact thing I wanted at that point in my life.
- 2y ago
- encody 2y agoI maintain a list of Rust tips and tricks for people who are looking to dig a bit deeper: https://geeklaunch.io/blog/rust-pro-tips-collection/ https://geeklaunch.io/blog/rust-pro-tips-collection/
- mgoetzke 2y agoGreat ! The first tip was already helpful, have to dig in.
- woile 2y agoSomething like this in "This week in Rust" would be fantastic
- xanathar 2y agoCool list! I learned a lot of new things, kudos. One suggestion: where you talk about using AsRef<[T]>, might be worth suggesting IntoIter, where it makes sense? IIRC that's what std uses for functions that aim at being generic over data structures and iterators (I might be remembering wrong, though). In any case let's not digress from the truth: amazing list.
- tialaramex 2y agoThere are a few of these conversion traits (for turning some kind of Thing into a Thing with a different purpose) You're thinking of IntoIterator, which is a trait for when you want an Iterator, maybe what you've got is already an iterator, but maybe it isn't yet and yet it could easily be transformed into one (like say an array), so in either case IntoIterator::into_iter() gets you an iterator instead of whatever you have now -- the thing you had before is consumed by this. Another you may find uses for is ToString, which says this obviously could be a string and I want the string. Unlike IntoIterator this does not consume the thing you wanted a string for. There are also a suite of generic traits for converting to another type, From, Into, TryFrom and TryInto. As the person consuming things, prefer TryInto if you're clear what should happen when you can't, or Into if you need it to always work. As the person who is implementing the traits, prefer to implement From or if you can't TryFrom. Rust automatically cascades the conversions in one direction, so if I implemented From<Goose> for Bird, and then you later write code which needs TryInto<Bird>, that'll work just fine with a Goose using the code I wrote, because Rust concludes how to use my conversion to make it work, automatically - Rust will even be able to see that your "What if I can't?" error handling code will never run, and optimise it out of production code since I provided From<Goose> so this cannot fail - its Error type is Infallible, an Empty type.
- sergiotapia 2y agoThis looks like a great resource! https://rust-exercises.com/03_ticket_v1/07_setters https://rust-exercises.com/03_ticket_v1/07_setters This shows why I wouldn't use Rust for anything. Seems like a lot of verbosity for gains that don't really apply whatsoever for the kind of work I do (web apps and APIs). You have to remember let/mut fine, but then ownership/borrowing, and finally this `into()` thing - yuck! Like washing your face with sandpaper.
- jamil7 2y agoThere’s a world outside of web development.
- sergiotapia 2y agointeresting!
- steveklabnik 2y agoAlso, just because these examples are verbose doesn't mean that all Rust code is this way. Accessors/mutators aren't used super often in my experience, and the builder pattern is used occasionally, but isn't pervasive. (I have historically been very skeptical of Rust on the web, but by now, I find it pretty pleasant. I of course know Rust very well already though.)
- littlestymaar 2y ago> I of course know Rust very well already though Understatement of the week ;)
- LukeMathWalker 2y agoAgreed. There's also another aspect at play: to build knowledge one step at a time with this learn-by-doing approach, you have to get learners to write code that's not perfectly idiomatic along the way. You then correct and refine those first drafts as you progress along. In the case of setters, we get rid of them at the end of that chapter by using the newtype pattern thus guaranteeing that field invariants can't be broken even if you can access them directly.
- _ank_it 2y agoIs there anything of this sort but for golang and python?
- knowaveragejoe 2y agoLook for "python koans". https://github.com/gregmalcolm/python_koans https://github.com/gregmalcolm/python_koans
- knowaveragejoe 2y agoExercises of this type are known as Koans, if I'm not mistaken. Look for "<language> koans" for others. I have to say, this seems like a far more robust set of Koans than I've seen elsewhere!
- anssip 2y agoI blogged about my approach of learn by doing: https://anssipiirainen.com/post/learning-rust/ https://anssipiirainen.com/post/learning-rust/
- mwigdahl 2y agoThanks for that article! One line jumped out at me: "When I learned about async programming, I realized that for my CLI-based project, I didn’t really need async. So, I removed all async code and the Tokio runtime, which significantly simplified the code." This implies that you used async before learning it. Does async come along for the ride with "standard" constructs in Rust, such that you have to make an active effort to avoid it? Forgive my ignorance about Rust if this is a dumb question.
- maxbond 2y agoNo, it's not like Go, you have to opt into async. (There was a time before 1.0 where it was, but that was removed.) My reading is that, since their technique was to learn something and then immediately apply it to their project, they began making it async before they realized it was better of without it. Seems like a good learning experience to me, I think this approach is good.
- anssip 2y agoYes, I had some feature in an earlier version that I wanted to add and the example code I was looking at was async and that made me think that I have to go to that route and also include an async runtime (Tokio). Once I was there, I ended up using async versions of some other dependencies as well. Later on I then realized that in my CLI app I’m not gaining anything from it as there is no need for any parallel prosessing. It was just making my code more complex.
- mwigdahl 2y agoThanks for the information!
- satvikpendem 2y agoSurprised no one has mentioned another great and similar resource called Rustlings [0] (yes very punny name). You are given some files with todo statements which you'll need to fix and make the code compile and pass all the tests. It's an interactive way to learn which is what got me through learning Rust a few years ago. [0] https://github.com/rust-lang/rustlings https://github.com/rust-lang/rustlings
- ergonaught 2y agoThe very first comment on this (I happened to have seen it) mentioned Rustlings.
- AnonC 2y agoThe difference is that Rustlings requires the person to already know Rust whereas the link on this post (100 Exercises to Learn Rust) starts teaching Rust from the basics (assuming that the person knows another language).
- archixe 2y agoI did Rustlings without any knowledge of Rust and I thought it worked great. There are links to guide & documentation pages for every exercise. It worked great to learn the basics through practice.
- LegionMammal978 2y agoThat doesn't match with my experience: I learned the language from scratch just fine with Rustlings. (In fact, I found it more approachable than the Rust Book.) The first few groups of exercises in it walk you through the basic syntax and semantics, to the point that I found it almost tedious to work through, but I ultimately came to appreciate it.
- sshine 2y agoIn the most recent Rust Copenhagen Hack Night, half a dozen people took their first steps with Rustlings. It was my experience that it worked really well. Some people were speed-running it, others took their time to read the book references that came up once in a while. These 100 exercises build on the same interactive project format as Rustlings, so I would assume they're both great.
- xlbuttplug2 2y agoWhat do you folks normally do after learning a language like this? (Assuming you don't use it for your day job.)
- harishnavnit 2y agoI never got a proper chance to experience Rust at work but always wanted to give it a go. So I went through the Build your own Redis challenge by https://codecrafters.io https://codecrafters.io. It was a a good excuse to get my hands dirty with all the different Rust concepts (with a bit of structure).
- cdperera 2y agoHighly specific, but in my operating systems course, we wrote a shell in C, it was very fun. I plan to write it again, but in Rust (mostly out of fun), extend it, make it my daily driver.
- tialaramex 2y agoIf it's coming towards the end of the year, solve Advent of Code in the new language you learned. AoC scales gently. On day 1 if you can't see how to do it that's going to be because you didn't really understand your new language. By day 25 it's very likely that you can't figure out how to solve the problem yet in any language, if you're up for it, maybe solve it in your existing favourite and then try the new one. Because lots of people do AoC, especially in popular languages like Rust or C++, you can see other people's solutions in the AoC Reddit solutions thread each day, which are often inspiring -- you learn new idioms, library features, or even whole CS concepts.
- bambax 2y agoI'm trying to write a Rust program to transform gamepad inputs to MIDI events -- mainly CC, so that one can control a filter with a joystick for example. But it should also be possible to play notes. (The field of MIDI controllers appears quite limited, and "pads" available on some keyboards are not fun to use at all.) I implemented this in a webapp, but it's really awkward to have to go through the browser back in the DAW. An executable would be more elegant. PS1: Some DAWs allow for gamepad input, but it's usually limited, difficult to configure, and buggy (Reaper for example has a bug that was first raised in... 2009 and never fixed). PS2: I had hoped LLMs would help... but boy do they not! ;-)
- jimsimmons 2y agoExperienced Rust users: how do you rate this? What level of proficiency would this leave you with
- wonrax 2y agoI'm not very experienced, but by skimming the headings I can see that it covers almost all of Rust's core features. I'd say you're between a beginner and an intermediate Rust user if you complete the exercises and can be comfortable dealing with lifetimes.
- YossarianFrPrez 2y agoThis certainly looks great. I hate to be stickler, but on the very first page ("Syntax"), right after explaining comments, the text says "In previous exercise, you saw the greeting function..." Unless I'm missing something, this is the very first intro to Rust, and so there are no prior exercises...
- Jtsummers 2y agoYou did. Read all of the first page, they tell you where to find the first exercise. > The exercise for this section is located in exercises/01_intro/00_welcome
- penguin_booze 2y agoIs it just I who get a 'site blocked due to security threat' message when visiting this site?
- rayxi271828 2y agoWonder if there's anyone who (1) is reasonably comfortable in some popular programming language(s) and (2) has gone through these exercises could/would share their take on how helpful/useful these are? Context: I've been doing Java professionally for 10+ years, Python, JS a few years ago, C++ way earlier in my career (early 2000s).
- dragonelite 2y agoI went through zigling variant i did about 90 of the 110 plus after reading the documentation of zig, it helped solidify some of the syntax and concepts. I think it does help reduce what might have been a 3 week process to a 1 week process during the evening learning.
- eknkc 2y agoI can’t get up to speed with rust using these kinds of material. Haven’t looked deeply at this one in particular but they tend to teach you patterns and approaches. That works for a lot of languages. Got into zig pretty fast for example. You can start writing go in 2 days and even if you write shit code at first, it will run. But I can’t get productive in rust and that kills my enthusiasm.
- hliyan 2y agoThis type of exercise doesn't quite help me. When I was learning Go, I couldn't find one that suited me, so I wrote one as I learned: https://github.com/hliyan/learn-golang/blob/master/day-01/hello.go https://github.com/hliyan/learn-golang/blob/master/day-01/he... For learning JS internals, I used this: https://johnresig.com/apps/learn/ https://johnresig.com/apps/learn/
- 3dworldrunner 2y agoSo, I have a couple CS degrees, but due to a series of unfortunate events, basically 0’d out of industry pretty quickly out of uni, now years ago. I just went through rustlings, and then this, and found both quite good. Rustlings is a little more gradual, perhaps, but basically just lists book sections to read for each problem set. This has a small outline of concepts for each exercise and is less reading. I’d recommend either or both…and I found them both to be pretty accessible. Rustling is a slightly broader survey of topics just due to the reading. This is maybe a little quicker to the meat of it. I don’t think either will give you 100% fluency in the borrow checker, but both will get you close enough to flesh out your understanding afterwards, and to make picking through readings more efficient afterwards.
- zerr 2y agoOne observation I have with Rust, looking at the code in wild, abstractions are usually (always?) leaky - implementation details are exposed/imposed due to the ownership feature of the language.
- pkolaczk 2y agoOwnership is not implementation. Ownership is contract. If you’re an owner of a house you can do more than if you’re only renting and if you’re renting you can do more than if you’re only looking at the house from the street. Ownership is a very useful concept that influences interfaces in real life so why wouldn’t it in programming?
- flohofwoe 2y agoThis reads like the Rust equivalent of the OOP 'animal/cat/dog' intros of yore. Real world comparisons are almost never all that helpful when it comes to computing concepts.
- pkolaczk 2y agoOk, point taken, indeed it reads like that. However, in programming and generally in engineering this is a very useful concept. There is a difference between "component X is part of Y" than "component X works / interacts with Y" or "component X has exclusive access to Y in this particular span", similarly how it is often very important to know if "component X can be safely shared" - and Rust allows to express that, while languages like Java are quite blind to that. Consider a simple Java method signature: public static MyCustomFileReader open(FileHandle someFile) { ... } Who is responsible for closing the file after you're done with reading it? Does the returned reader close the file handle on close, or should I issue another close on the file handle afterwards? Can I open multiple readers on the same file? The only way to know is to check the javadoc comment, if someone took time to write it. In Rust there are no doubts like that.
- maxbond 2y agoCould you give an example?
- SoKamil 2y agoDo you know similar sources for C?
- legends2k 2y agohttps://exercism.io/tracks/c https://exercism.io/tracks/c Exercism.io is not just limited to one language it has a track for most popular languages. I've used it a couple of times and can definitely recommend it. You get to compare your solution with other (top-rated) solutions and you also get mentorship at times (depending on availability I guess).
- squarefoot 2y agoObligatory reference to the Rosetta Code site which shows a good number of popular and less known problems with solutions in many programming languages. https://rosettacode.org/wiki/Rosetta_Code https://rosettacode.org/wiki/Rosetta_Code Edit: it seems the "more..." label down the 1st list to see all examples is broken. To explore the full list use this link instead: https://rosettacode.org/wiki/Category:Solutions_by_Programming_Task https://rosettacode.org/wiki/Category:Solutions_by_Programmi...
- sformisano 2y agoI skimmed through a few sections in which my rust knowledge is basic at best (threads). Really enjoy the no-nonsense, not-a-word-wasted style. My first thought after spending 30 minutes with this that it could easily become my new no. 1 recommendation for rust onboarding, toppling the zero2prod book. Then I figured out why this felt familiar, it's from the same author :D Will definitely dive into it this weekend.
- sk11001 2y agoZero2prod doesn't really teach Rust, it's more about setting up an API for people who already know Rust.
- ramon156 2y agoAnd it also isn't 100% best-practise, more like all-practise. It tries to show any and every way you could approach a problem while somehow keeping it minimal. Really like it but if you follow the tutorial 1:1 you'll most likely get frustrated
- sk11001 2y ago> Really like it but if you follow the tutorial 1:1 you'll most likely get frustrated I went through it and hated it. It's very tedious and I felt like I learned nothing from following along.
- not_a_dane 2y agowell, you don't need to know Rust to complete Zero2prod but you need to have a backend background for sure.
- sformisano 2y agoI read the book while knowing almost nothing about the language, but yes I did complement it with "the rust programming language" book at the same time. I still think zero2prod is extremely beginner friendly, you don't need to know rust well to follow it. You can just dive into rust concepts as you read it.
- wdb 2y agoDoes anyone have good examples how to write a service that does a lot of data fetching? E.g. analytics events?
- maxbond 2y agoI don't, but I'm doing something similar, and I've been using this video [1] about axum as a guide. My thinking is that I'll write something similar, but instead of kicking off a task when a request comes in I'll kick them off on a schedule. [1] https://www.youtube.com/watch?v=Wnb_n5YktO8 https://www.youtube.com/watch?v=Wnb_n5YktO8
- mariozski 2y agoKudos to the create and maintainer https://github.com/LukeMathWalker https://github.com/LukeMathWalker. He could easily monetize this if he wanted and chosen to share it for free.
- adius 2y agoYou might also enjoy following flashcard deck I built. It’s based on the official book. https://github.com/ad-si/Rust-Flashcards https://github.com/ad-si/Rust-Flashcards
- deleted 2y ago[deleted]
- OptionOfT 2y agoI've been doing Rust personally for a while now, and now that I got laid off I'm really looking for a job where I can do Rust. Sad part is that there just aren't that many jobs out there that use Rust, let alone for someone with extremely little technical experience.
- maxbond 2y agoHope you find something! There's a thread on the subreddit. https://www.reddit.com/r/rust/comments/1cixuzr/official_rrust_whos_hiring_thread_for_jobseekers/ https://www.reddit.com/r/rust/comments/1cixuzr/official_rrus... See also this account on HN. https://news.ycombinator.com/user?id=whoishiring https://news.ycombinator.com/user?id=whoishiring