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
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.
@@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
@@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.
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.
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 :)
"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.
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
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)
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
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 ?.
@@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 } ```
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 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.
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.
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!
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.
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
@@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.
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?
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.
@@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!
@@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`
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
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
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.
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...
@@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
@@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.
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.
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!
@@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
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
nice what type of compiler are you writing? Something for an existing language or for a new one?
EXACTLY. Always sorely disappointed when languages get so close but so, so far. (Looking at you, Kotlin)
@@AndrewBrownK looking at kotlin, looking at dart 3.x
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.
I have really grown to love the new let else construct. I found it very useful when manually writing a parser.
this actually wasn't on my radar when I was making the video, I wish I had included it!
@@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
Thanks, good lesson. If I need to take my phone with me, it's hardly used. Welcome to the world of happily unplugged people. 😄
thanks, glad you liked it. props for staying unplugged, that requires willpower that many don't have 😎
So how did you watch the video ? am just curious
@@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.
I used my first "if let" today because I remembered this video from a few months ago.
Thanks!
nice!! 😎
That intro is fantastic
thanks, I actually almost trimmed it out right before publishing lol
@@codetothemoonI literally subscribed because of that intro :D thanks for not trimming it!
First few seconds of this vid, resonated with me on a , 'bro are you stalking me' , kind of level 😅
hah! 🙃
Nice job as always! Super happy to see this kind of videos. Helps me a lot learning Rust.
Glad to help!
being able to specify a condition is so helpful! I didn't know I could do that!
The unwrap_or is class, love to see proper error handling. More!
😎
Yes I am absolutely watching this sitting on the
nice 🚀
Really cool explanation, short but informative.
thanks, glad you got something out of it!
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.
thanks for the feedback! I definitely could have explored the "why" a bit more.
Wow that font is big.
🤣
As big as what people are making 😂
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 :)
ahh good point, yeah I suppose I could have done it that way too!
"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.
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
I'd probably go With "if let Ok(res) if !res.is_empty()" for grammar which looks a bit silly
Yep you got that phone thing right! love it.
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)
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
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 ?.
@@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
}
```
@@Ruhrpottpatriot thanks, great example, I see the point 😊
@@Ruhrpottpatriot ooh I do have to admit I like that. yeah circumstantial formatting is probably a good factor to consider in any situation
thankyou so much 🙏👍
thanks for watching!
Oooh.... you got a nice big font! - thanks! :)
you're welcome!
Let's not forget the lovely "while let"
ahh yes, I probably should have covered that in this video too!
Maybe `let else` would have fit into this video nicely as well?
probably should have included this as well!
very good explanation i just subscribed
thank you, very happy to have you onboard!
Pretty good video! It would have been cool explaining Some => and None => which are quite common patterns.
Great vid!
Thank you! 🙏
3:50 Your password is visible!
thanks for pointing this out - fortunately this particular database is long gone :)
Thanks man👍!
thanks for watching!
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.
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
@@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.
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.
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!
@@codetothemoon I find all your videos extremely helpful and well done, but am with @theroboman727 on this
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.
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
@@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.
when watching on desktop, I need to make it a small size of my browser
hah! it's hard to cater to both desktop and mobile...
I think I just been called out the first 5 seconds of the vid XD
indeed you have been! 🙃
Another nice one, Please what's the name of this IDE?
thank you! DOOM emacs. might do a video on it at some point.
@@codetothemoon it would be great to see that happen
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?
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.
@@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!
@@codetothemoon How did you make it look so good? I definitely need that video. What's the font you're using, by the way?
Is there a way to easily access the error struct in else block?
I think the easiest way would be to add "if let Err(e)" after the else keyword.
@@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`
Hahaha. As I sit on a plane watching this on my phone….
Bet the big font came in handy! 🙃
@@codetothemoon it was perfect. But honestly, your videos always are. You do a great job.
Can you please post your doom emacs config?
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
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
You use unicode-normalization crate
What is the configuration for your highlight syntax?
It's the default Rust configuration in DOOM Emacs with the Monokai Pro theme 😎
@@codetothemoon Thanks!
3:48 is where i got lost.
What exactly is happening when `if let Ok(res) = get_result` is run?
Somebody else told me the syntax is meaningless and could be anything. That helps.
What keyboard do you use? It sounds nice.
Thanks! it's a Corne v3 with Gateron Pro Red switches. Might be the topic of a video at some point.
You gave up on helix and switched to DOOM Emacs ? How is your experience ?
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.
How are you putting images inline the code?
Your keyboard sounds so incredibly nice! What are you using?
Thanks! It's a Corne with Gateron Red Pro switches. shop.beekeeb.com/product/pre-soldered-crkbd-v3-mx-corne-keyboard/
@@codetothemoon do you have a link to the keyboard layout you are using? Just curious
Storing password in env variable but then printing connection string including the password in next line?
yeah, wouldn't do that in production code. I'll know I forgot to rotate my passwords when AWS bills skyrocket 🙃
if let looks quite powerful and concise operator. However it isn't natively obvious.
can I get your nvim config? I use arch btw.
I'm actually using DOOM emacs in this video!
@@codetothemoon thanks, I didn't notice that. and can you make a video about web3 and how to start with rust? Thanks again.
Which font do you use?
I believe it is Monaco
1sec in my head screams WAY TOO BIG
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...
New keyboard? :D
Relatively new, it's a Corne github.com/foostan/crkbd
@@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?
@@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
Hey hey, looks like somebody’s learning Emacs.
indeed! is my newbie status that obvious?
@@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.
what font :))
Monaco!
Lmao the debug showed the password
😱
Rust influxdb2
why did you log the password 🤣🐧
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.
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!
@@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
I honestly the enjoy the typing sounds, they are quite satisfying
The
don't leave us hanging there!
Ok. thought any action would lead to an improvement for your channel.