the real power of pattern matching really shows when destructuring complex structs or slices. i am writing a compiler in rust and the pattern matching makes conditionals very readable
@codetothemoon Жыл бұрын
nice what type of compiler are you writing? Something for an existing language or for a new one?
@AndrewBrownK Жыл бұрын
EXACTLY. Always sorely disappointed when languages get so close but so, so far. (Looking at you, Kotlin)
@ewertonls_ Жыл бұрын
@@AndrewBrownK looking at kotlin, looking at dart 3.x
@diadetediotedio6918 Жыл бұрын
Precisely, rust has a wonderful pattern matching. I created my own text tokenization crate in the language and working with it using pattern matching is extremely simple, you just need to be expressive and concise and the logical structures unfold naturally.
@benjamingrant6869 Жыл бұрын
I have really grown to love the new let else construct. I found it very useful when manually writing a parser.
@codetothemoon Жыл бұрын
this actually wasn't on my radar when I was making the video, I wish I had included it!
@Yadobler Жыл бұрын
@@codetothemoon I have a question, is it wrong to say that "if let" is similar to python walrus operator (:=) in that it evaluates the rhs expression and then if it is of the type ok(x) then the condition is met if let ok(x) = getBobMood() { print("mood is {}", x); } vs if (x := getBobMood()) is not None: print(r"mood is {x}") Im guessing the difference is in rust enum and how ok(x) itself is a type of Result but x can is set? is that it? Gosh I think the main confusion here is in how Rust defines Enums, and how enums can have data in them
@Nintron Жыл бұрын
That intro is fantastic
@codetothemoon Жыл бұрын
thanks, I actually almost trimmed it out right before publishing lol
@_aurora6010 ай бұрын
@@codetothemoonI literally subscribed because of that intro :D thanks for not trimming it!
@mageprometheus Жыл бұрын
Thanks, good lesson. If I need to take my phone with me, it's hardly used. Welcome to the world of happily unplugged people. 😄
@codetothemoon Жыл бұрын
thanks, glad you liked it. props for staying unplugged, that requires willpower that many don't have 😎
@CliveStewart-bq3od Жыл бұрын
So how did you watch the video ? am just curious
@mageprometheus Жыл бұрын
@@CliveStewart-bq3od My lounge is 50% recording studio for music. I have broadband and watch Videos that I choose but avoid social media. For me, getting into the creative flow requires isolation and that's hard with any kind of tech addiction.
@sergiuoanes4635 Жыл бұрын
Nice job as always! Super happy to see this kind of videos. Helps me a lot learning Rust.
@codetothemoon Жыл бұрын
Glad to help!
@sergioquijanorey7426 Жыл бұрын
I don't know if other people thinks the same, but for me if let statement is less clear than using match statement for doing the same. Using your example, it would be: ``` let boob_mood = match get_result { Ok(mood) => { println!("got value from the database"); mood }, Err(error) => { println!("Couldn't get the value from the database"); println!("Err code was: {error:?}"); String::from("Neutral") } }; ``` It's clearer because I see that I do something when I got Ok, other thing when I got Err. Also, I got which is the value hold into the Err type, and I can do some logs, transforms or just raise the error directly. I am alone in this? Are there some good reasons why if let is superior (because I see that a lot more than the snippet I wrote)
@AndrewBrownK Жыл бұрын
One isn’t “superior” to the other but generally I use match for exhaustive matching or expressions, and I pretty much save if let for conditional effects or mutations that are not expressions. I’d scratch my head in confusion if someone used “if let { … } else { … }” though
@Ruhrpottpatriot Жыл бұрын
if-let and it's sibling let-else are great if you need to handle a particular case and don't care about the others. While in the case of Option or Result there isn't much benefit, it gets much cleaner if you have, say an HTTP Error code enum. If you wanted to calculate a value based on the code and and return it from the block your match statement would need to compute a dummy value or you'd need to use an option (which you need to destructure or pass along with ?.
@Ruhrpottpatriot Жыл бұрын
@@AndrewBrownK For these cases the new let-else statement would probably be better since we could just write ``` let Some(mood) = con.get("bob:mood) else { // Do stuff for the error/none case } ```
@sergioquijanorey7426 Жыл бұрын
@@Ruhrpottpatriot thanks, great example, I see the point 😊
@AndrewBrownK Жыл бұрын
@@Ruhrpottpatriot ooh I do have to admit I like that. yeah circumstantial formatting is probably a good factor to consider in any situation
@thisisscotts Жыл бұрын
I used my first "if let" today because I remembered this video from a few months ago. Thanks!
@codetothemoon Жыл бұрын
nice!! 😎
@Im_Ninooo8 ай бұрын
being able to specify a condition is so helpful! I didn't know I could do that!
@seannewell397 Жыл бұрын
The unwrap_or is class, love to see proper error handling. More!
@codetothemoon Жыл бұрын
😎
@phenanrithe Жыл бұрын
"if let" is a nice tool! I often miss the guard feature we have with "match", however, never understood why it has never been accepted ("if let Ok(res) && !res.is_empty()" for example). Without it, "match" can still be used instead, though I just find it less clear about the binary nature of the test.
@jrmoulton Жыл бұрын
From what I've read and remember it is just the current state of the implementation. There are tracking issues for this very thing and it could be added if it was implemented
@Turalcar11 ай бұрын
I'd probably go With "if let Ok(res) if !res.is_empty()" for grammar which looks a bit silly
@Ma1ne2 Жыл бұрын
Really cool video as usual! Would have been nice to see the moods as an enum and show how powerful they're together with match statements, but I understand then it wouldn't have been as straight forward to explain guards :)
@codetothemoon Жыл бұрын
ahh good point, yeah I suppose I could have done it that way too!
@hellovova Жыл бұрын
It would be nice to explain if let more deeply, what is it, why do we need it, what is benefits of using it, what is it's equivalency in "normal" coding . I know you can, I remember your great explanation of macros. Like from me.
@codetothemoon Жыл бұрын
thanks for the feedback! I definitely could have explored the "why" a bit more.
@alexdai_ Жыл бұрын
Really cool explanation, short but informative.
@codetothemoon Жыл бұрын
thanks, glad you got something out of it!
@wolfboyft Жыл бұрын
3:48 is where i got lost. What exactly is happening when `if let Ok(res) = get_result` is run?
@wolfboyft Жыл бұрын
Somebody else told me the syntax is meaningless and could be anything. That helps.
@havocthehobbit4 ай бұрын
First few seconds of this vid, resonated with me on a , 'bro are you stalking me' , kind of level 😅
@codetothemoon4 ай бұрын
hah! 🙃
@lamka02sk Жыл бұрын
Is there a way to easily access the error struct in else block?
@codetothemoon Жыл бұрын
I think the easiest way would be to add "if let Err(e)" after the else keyword.
@theroboman727 Жыл бұрын
@@codetothemoon you can do `else if let Err(e)`. But `if let` is only really meant for checking one case and ignoring the rest. in more complex cases like this you should use `match`
@Zeioth10 ай бұрын
Pretty good video! It would have been cool explaining Some => and None => which are quite common patterns.
@CliveStewart-bq3od Жыл бұрын
Yep you got that phone thing right! love it.
@SpektralJo Жыл бұрын
Yes I am absolutely watching this sitting on the
@codetothemoon Жыл бұрын
nice 🚀
@DeHSIFY Жыл бұрын
Can you please post your doom emacs config?
@codetothemoon Жыл бұрын
There's nothing particularly special in there (at least that is relevant to what you're seeing here), i just uncommented rust in init.el and changed the theme to monokai-pro
@theroboman727 Жыл бұрын
Maybe `let else` would have fit into this video nicely as well?
@codetothemoon Жыл бұрын
probably should have included this as well!
@edgeeffect Жыл бұрын
Oooh.... you got a nice big font! - thanks! :)
@codetothemoon Жыл бұрын
you're welcome!
@Pyro2352 Жыл бұрын
Thank you, sir, for the video. Keep up the excellent work. You are helping in a real meaningful way. Could you do a video on debugging and perhaps setting up your IDE debugger to see the content of a vector when using breakpoints? I'm using Clion and I'm struggling with the setup and with efficient debugging in general.
@codetothemoon Жыл бұрын
really happy that these videos are of help! I actually haven't delved too deeply into debugging Rust. I'm more of a "debug via log output" type, primarily because of how live debugging tends to cause network timeouts
@Pyro2352 Жыл бұрын
@@codetothemoon I usually don’t work with networking, so I haven’t thought of the timeout issues, but in that case, it makes total sense. At work we use Python, so I got used to live debugging mainly because of the dynamic typing. In a case that something breaks, the quickest way for me to understand function argument types, call structure and class methods, that are inherited from who knows where was with live debugger. In Rust, I guess it is not as needed thanks to the type system, but I still find it very convenient.
@repe0 Жыл бұрын
Storing password in env variable but then printing connection string including the password in next line?
@codetothemoon Жыл бұрын
yeah, wouldn't do that in production code. I'll know I forgot to rotate my passwords when AWS bills skyrocket 🙃
@yousefsaddeek7 ай бұрын
very good explanation i just subscribed
@codetothemoon6 ай бұрын
thank you, very happy to have you onboard!
@echobucket Жыл бұрын
Wow that font is big.
@codetothemoon Жыл бұрын
🤣
@catholic_zoomer_bro Жыл бұрын
As big as what people are making 😂
@noviriustomeisho6630 Жыл бұрын
What is the configuration for your highlight syntax?
@codetothemoon Жыл бұрын
It's the default Rust configuration in DOOM Emacs with the Monokai Pro theme 😎
@noviriustomeisho6630 Жыл бұрын
@@codetothemoon Thanks!
@erlangparasu6339 Жыл бұрын
thankyou so much 🙏👍
@codetothemoon Жыл бұрын
thanks for watching!
@soleboxy2 ай бұрын
isnt res already the value? why do you need to reassign it to bob_mood?
@konstantinrebrov675 Жыл бұрын
How are you putting images inline the code?
@Bvngee Жыл бұрын
Im fascinated about your editor choice!! I know you've at least used VSCode, neovim, helix, and now doom emacs?? What's your reasoning and/or what have you learned?
@codetothemoon Жыл бұрын
haha yeah last year was my year of experimenting with different editors. settled on doom emacs for the foreseeable future. there'll be a video on it at some point, once I'm able to adequately articulate my reasoning. The short answer is - org mode, literate programming, dired, and the M-x menu. I think much of this can be replicated in something like Neovim but I'm not entirely clear yet on where the limit is there.
@Bvngee Жыл бұрын
@@codetothemoon I would absolutely love a video on this. I started with neovim a while ago but there's always more and more work to do to keep it up to date, and I never quite feel like I'm "there" yet. Let alone not even being sure if neovim is the right choice! Anyways, love the content!
@exsesx Жыл бұрын
@@codetothemoon How did you make it look so good? I definitely need that video. What's the font you're using, by the way?
@jakubrembiasz8942 Жыл бұрын
Great vid!
@codetothemoon Жыл бұрын
Thank you! 🙏
@theroboman727 Жыл бұрын
cool video, but I have one gripe with it. I feel like the database thingy might throw off some beginners, because its inaccessible. Setting that up is kind of a big commitment, and beginner might not be able to find the relevant documentation very quickly. It's important to be able to see at least the function signature of everything you're doing and to be able to experiment with it I think. Another small gripe I've had across multiple videos is that you don't really show the terminal error messages. IDE error messages are often shortened and are missing a lot of info that you would get in the terminal, so I often tell beginners to always go run cargo check in the terminal when its not immediately obvious what you should do to fix an error message. A great example of this is No Boilerplate's video "Rust Is Easy", where they take a javascript function and convert it to rust based on only the compiler's suggestions. So maybe when an error message is vague, pull up the terminal and run cargo check, instead of explaining it with your own hindsight. that would be nice. Love your videos overall.
@vinos1629 Жыл бұрын
I feel like you really shouldnt be getting into rust if you cant understand a database connection or even how to connect to a database
@theroboman727 Жыл бұрын
@@vinos1629 you dont need databases for everything though? Im pretty decent with rust, and have written tools that are in active use in a community, but they do not need to store any data other than simple settings on the user's computer. I am just a hobbyist though, and should learn what databases are about at some point. My point was also not really exclusive to databases.
@phenanrithe Жыл бұрын
That was my feeling too. It felt the presentation was made more confusing with repeated excuses about this part of the code "to be please ignored", and this was not really necessary in the first place. On the other hand it shows other features that may awake the curiosity of the viewer. I think that's the sort of example I'd use in a book (with appropriate introduction and links) when the reader has the time, but I would avoid it in short videos because people are watching them to save time. Just got me a raised eyebrow though, not a big deal in an overall great video.
@codetothemoon Жыл бұрын
thanks for the great feedback @theroboman727 ! Yeah I wasn't sure I the database thing was a good call, I was hoping a more "realistic" example might intrigue people a bit more. But then I used completely contrived data in said database, and probably thwarted my own aim for realism 🙃 Re: showing terminal error messages, this is a great suggestion, I may implement this!
@erikyoung5139 Жыл бұрын
@@codetothemoon I find all your videos extremely helpful and well done, but am with @theroboman727 on this
@mr.norris3840 Жыл бұрын
3:50 Your password is visible!
@codetothemoon Жыл бұрын
thanks for pointing this out - fortunately this particular database is long gone :)
@JorgetePanete Жыл бұрын
how do we normalize utf8? i recently read about an example: é vs e + "combining accent" character, they are visually the same but different bytes, swift normalizes by default
@Turalcar11 ай бұрын
You use unicode-normalization crate
@linuswalker4552 Жыл бұрын
can I get your nvim config? I use arch btw.
@codetothemoon Жыл бұрын
I'm actually using DOOM emacs in this video!
@linuswalker4552 Жыл бұрын
@@codetothemoon thanks, I didn't notice that. and can you make a video about web3 and how to start with rust? Thanks again.
@TheInspctrcat Жыл бұрын
Which font do you use?
@codetothemoon Жыл бұрын
I believe it is Monaco
@kdmtv8188 Жыл бұрын
Another nice one, Please what's the name of this IDE?
@codetothemoon Жыл бұрын
thank you! DOOM emacs. might do a video on it at some point.
@kdmtv8188 Жыл бұрын
@@codetothemoon it would be great to see that happen
@AndrewBrownK Жыл бұрын
Let's not forget the lovely "while let"
@codetothemoon Жыл бұрын
ahh yes, I probably should have covered that in this video too!
@guilherme5094 Жыл бұрын
Thanks man👍!
@codetothemoon Жыл бұрын
thanks for watching!
@durchschnittlich Жыл бұрын
Your keyboard sounds so incredibly nice! What are you using?
@codetothemoon Жыл бұрын
Thanks! It's a Corne with Gateron Red Pro switches. shop.beekeeb.com/product/pre-soldered-crkbd-v3-mx-corne-keyboard/
@bottlerocketlabs Жыл бұрын
@@codetothemoon do you have a link to the keyboard layout you are using? Just curious
@patrick1532 Жыл бұрын
What keyboard do you use? It sounds nice.
@codetothemoon Жыл бұрын
Thanks! it's a Corne v3 with Gateron Pro Red switches. Might be the topic of a video at some point.
@AbhishekBajpaiHere Жыл бұрын
You gave up on helix and switched to DOOM Emacs ? How is your experience ?
@codetothemoon Жыл бұрын
still love helix, can't really go all in on it until they have plugin support though. DOOM is amazing, especially org mode and literate programming. No plans to switch away from it anytime soon.
@LALO-cv4ck Жыл бұрын
what font :))
@codetothemoon Жыл бұрын
Monaco!
@naranyala_dev Жыл бұрын
when watching on desktop, I need to make it a small size of my browser
@codetothemoon Жыл бұрын
hah! it's hard to cater to both desktop and mobile...
@JosephDalrymple Жыл бұрын
New keyboard? :D
@codetothemoon Жыл бұрын
Relatively new, it's a Corne github.com/foostan/crkbd
@JosephDalrymple Жыл бұрын
@@codetothemoon Nice! I've heard a lot about them, and I've been tempted, but I haven't made up my mind yet haha. How do you like yours?
@codetothemoon Жыл бұрын
@@JosephDalrymple It's incredible. But now I'm using a Chocofi because I realized I like home row mods and so I don't need the 3 outermost keys on each side anymore, so now I'm using a 36 key layout. Still going to be using the Corne (probably in all videos for the foreseeable future), just not the outermost key columns
@OrtinFargo Жыл бұрын
I think I just been called out the first 5 seconds of the vid XD
@codetothemoon Жыл бұрын
indeed you have been! 🙃
@Caldaron25 күн бұрын
lol could have just used a function that returns result or option^^ but yeah lets use redis^^
@romanstingler435 Жыл бұрын
1sec in my head screams WAY TOO BIG
@codetothemoon Жыл бұрын
i've finally gone too far in the opposite direction, usually the problem is that it looks too small in video even though it looked fine during recording...
@kamertonaudiophileplayer84710 ай бұрын
if let looks quite powerful and concise operator. However it isn't natively obvious.
@plrpilot Жыл бұрын
Hahaha. As I sit on a plane watching this on my phone….
@codetothemoon Жыл бұрын
Bet the big font came in handy! 🙃
@plrpilot Жыл бұрын
@@codetothemoon it was perfect. But honestly, your videos always are. You do a great job.
@saminamanatАй бұрын
how did you know i’m on my phone sitting on the …
@codetothemoonАй бұрын
clairvoyance 😎
@i007c Жыл бұрын
why did you log the password 🤣🐧
@justalawngnome7404 Жыл бұрын
Hey hey, looks like somebody’s learning Emacs.
@codetothemoon Жыл бұрын
indeed! is my newbie status that obvious?
@justalawngnome7404 Жыл бұрын
@@codetothemoon Haha, not really. I mean sure, you occasionally pause when switching out of INSERT mode, but I'd say you're past the point of being as fast with Doom Emacs as with any other editor. It's all downhill from here, man! A cool tip in case you haven't discovered it yet: use "Ctrl+[" instead of "ESC" to move out of INSERT mode. It works out of the box and doesn't require you to leave your home row.
@HyperCodec Жыл бұрын
Lmao the debug showed the password
@codetothemoon Жыл бұрын
😱
@Sahil-a-vim-user Жыл бұрын
It would make your videos much more engaging if you can remove the typing and just show the code, just highlight the part you added. This will decrease the video length but actually makes for much more interesting videos. The video was good, never knew about if statement inside the match clause. Keep up the good work.
@codetothemoon Жыл бұрын
thanks - i've gone back and forth on this and your feedback is a great data point to have. the theory was that the typing would give folks time to digest what is gong on, but that's inevitably going to make others bored. Often times I fast forward the typing, but I seemed to speak a lot while typing in this one. Glad you learned something from it!
@dj-maxus Жыл бұрын
@@codetothemoon As for me, your real-time typing really helps to digest all the information consciously, like, straight to the brain cortex. It feels like pair programming when you guide through the things and concepts being shown. At the same time, I can speed up your video if I need to
@houtamelocoding Жыл бұрын
I honestly the enjoy the typing sounds, they are quite satisfying
@scwan-ew8uh Жыл бұрын
Rust influxdb2
@pineappleexpress2307 Жыл бұрын
The
@codetothemoon Жыл бұрын
don't leave us hanging there!
@pineappleexpress2307 Жыл бұрын
Ok. thought any action would lead to an improvement for your channel.