A Simpler Way to See Results

  Рет қаралды 117,248

Logan Smith

Logan Smith

Күн бұрын

Пікірлер: 265
@jackkendall6420
@jackkendall6420 Жыл бұрын
I wish your channel had twenty hours of videos like this to binge.
@_noisecode
@_noisecode Жыл бұрын
on it
@mikkelens
@mikkelens Жыл бұрын
@@_noisecodeim excited but don’t rush them, they’re so good at this level of quality :)
@Pumpekk
@Pumpekk Жыл бұрын
same, these might be the most interesting Rust videos on YT (sorry Jon, sorry No Boilerplate)
@NoBoilerplate
@NoBoilerplate Жыл бұрын
Glorious! The Haskell aside is the cherry on top for me 👌👌
@heh2393
@heh2393 5 ай бұрын
St Rust 🙏
@antonpieper
@antonpieper Жыл бұрын
Your videos are all so high quality! Your Videos are Result
@_noisecode
@_noisecode Жыл бұрын
legendary comment
@zyansheep
@zyansheep Жыл бұрын
@@_noisecode let your_videos = Result
@RenderingUser
@RenderingUser Жыл бұрын
When he uploads, there is only one Option, until there is none
@nathanoy_
@nathanoy_ Жыл бұрын
Your channel has a bright future ahead and I will watch your success with great pleasure.
@bcsb1001
@bcsb1001 Жыл бұрын
9:40: As a fellow Haskell and Rust enthusiast, I must point out for extra completeness that liftA2 (from Control.Applicative) and on (from Data.Function) can shorten this even further: wnew = liftA2 W `on` fallible Great video, and the Haskell reference was the cherry on top.
@AbelShields
@AbelShields Жыл бұрын
Thiserror is a good choice for libraries, anyhow makes sense for applications. You shouldn't use anyhow in libraries (since it forces any applications using it to use anyhow too), but thiserror produces normal enums, it just saves boilerplate. You can use it for applications too, although some people prefer the ease and context capabilites of anyhow.
@Luxalpa
@Luxalpa Жыл бұрын
The advantage of anyhow is that you don't need to handle specific error cases, you can just say "oh whatever, when there's an error I'm just gonna print the error message to the screen" or something like that. On the other hand, if you actually do care about which specific error you get (say for example you want to recover from the error in different ways depending on what happened), then using enums / thiserr is the way to go! And for library code, probably very rarely want to use anyhow (and never want to have an anyhow::Result exposed from your API!).
@orciument
@orciument Жыл бұрын
I actually didn't know what Infallible was for, this was definitely a good video!
@KohuGaly
@KohuGaly Жыл бұрын
Infallible is actually a workaround for the limitations of Rust syntax and type system when it comes to the ! never type. It is likely one of them will be made just alias for the other somewhere down the line.
@spaghettiking653
@spaghettiking653 Жыл бұрын
@@KohuGaly Glad to see I'm not the only one who sees an issue with this. I find it bizarre to use a special type just to circumvent a problem that's fundamentally caused by forcing a function to return an error when it's not possible for that to happen.
@ETBCOR
@ETBCOR Жыл бұрын
I love your Rust videos so much! You're so so good at helping my mental model of things improve. Keep up the banger work!
@mattshnoop
@mattshnoop Жыл бұрын
5:32 I was going to bring up this exact point and then you said it. There have been tons of times where I've returned `Result` specifically because, to me, there is a semantic difference between "I have nothing to return to you" and "this operation *failed*."
@JoQeZzZ
@JoQeZzZ 7 ай бұрын
If you don't have a need for an error type, then you should use an Option Imo. If there is a semantic difference between 'I have nothing to return to you' and 'This operation failed', return Result or if there are multiple ways to return fail specify an error. If there is only a single way by which the function does not return a value you should use an option.
@Xld3beats
@Xld3beats Жыл бұрын
That last line blew my mind! A result without an err is isomorphic to a bool. Pretty new to rust, but result and option seem a LOT better than doing "if data != null" checks in JS
@saxpy
@saxpy Жыл бұрын
You have a flavor of type theory / category theory when describing the isomorphisms of Result ~ Option and Result ~ T. I'm curious to watch your other videos if you would cover things such as dependent types, const N: usize, and what GATs map to in the realm of type theory. Thanks for the video!
@danielgysi5729
@danielgysi5729 Жыл бұрын
These videos are incredible. It's so hard to find programming videos for any language that don't assume the viewer is a total noob to programming in general. I love this because, even though I'm already familiar with Option, Result, From, and all the other std library stuff, I didn't know about thiserror and anyhow. I just hope the rust community is a big enough audience to sustain this channel.
@istathar
@istathar Жыл бұрын
Coming to Rust from Haskell just wanted to say I appreciated the brief digression into what this would look like with do-notion in the Either monad. I really respect Rust's unified approach to using Result everywhere rather than exceptions. Got a lot out of your video, thanks.
@tXJwu005JORffQs30mQKJRnT
@tXJwu005JORffQs30mQKJRnT Жыл бұрын
I'm way too deep into this cult I mean programming language
@Rudxain
@Rudxain 3 ай бұрын
ULTIMATE CRABIFICATION 🦀🦀🦀
@ToumalRakesh
@ToumalRakesh 3 ай бұрын
People who claim that Rust is a cult, are in a cult.
@DucBanal
@DucBanal Жыл бұрын
I will chime in with the other comments. You have found a great niche in the Rust educational landscape. Please continue exploring more and more tricky Rust concepts. Thank you for your work !
@bobbybyrne1899
@bobbybyrne1899 Жыл бұрын
Thanks Logan, great video. I'm currently working in another language and miss using Rust, and videos like these keep me fresh and up-to-date on the language.
@theshooter89
@theshooter89 6 ай бұрын
I love the way you talk about hugely complex things in such a clean and clear, understandable way. One of the best rust channel on yt
@computerfan1079
@computerfan1079 11 ай бұрын
This video finally fully cleared up the use of Result for me. Thank you!
@natashavartanian
@natashavartanian Жыл бұрын
I WILL take what you present today as some added nuance for my existing understanding
@m_ish_y
@m_ish_y Жыл бұрын
I have to confess I am always learning so much about Rust with all your videos!! The more I watch, the more I realise how wonderful and powerful Rust is as a programming language. Thank you so much!
@jacksonbourne
@jacksonbourne Жыл бұрын
Note that the compiler will correctly infer Vec for T in the Result if you just provide Result. You could simplify Ok(Self(v?)) even further with a `.map(Self)` instead :)
@Goras147
@Goras147 11 ай бұрын
I love watching your videos about Rust! So clean and elegantly explained, with personal remarks so that you're not like a robot. Amazing!
@baobaobiz
@baobaobiz Жыл бұрын
Your content is absolutely top-notch. Please keep making more.
@LuckyNumberKevin
@LuckyNumberKevin Жыл бұрын
Your content is VERY helpful in reasoning about rust! I shared your channel on 2 disc servers to potentially help others...and to give back to you for your hard work! Keep it up, man
@HyperFocusMarshmallow
@HyperFocusMarshmallow Жыл бұрын
The real gem in this video is as the derive_more crate. The rest was great as well, but I felt at home with most of this. It’s nice to just hear it in detail and twist and turn concepts around an look at them from different direction. It really helps solidify the ideas.
@Raiment57
@Raiment57 Жыл бұрын
Your Rust videos remind me very much of Meyers' two Effective C++ books (which I devoured when they came out). I really look forward to more in this vein. Thank you.
@CorneliusCornbread
@CorneliusCornbread Жыл бұрын
One thing that drives me absolutely bonkers nuts is when libraries have custom error types that don't implement the Error trait. PLEASE if you are making a library implement the Error trait on your error types, without it being able to generically handle errors is literally impossible.
@rileydavidjesus
@rileydavidjesus Жыл бұрын
I usually just use std when I need something to handle the generic error.
@raidensama1511
@raidensama1511 Жыл бұрын
Use thiserror
@angeldude101
@angeldude101 10 ай бұрын
Small problem with implementing std::error::Error: Sometimes std does not exist. For libraries intended to be usable in low level environments where many of the functions of std don't even make sense, it's common to target core instead. Now, the Error trait is not one of those things that doesn't make sense in a low level context... which is why so many have wondered why it wasn't in core to begin with, and now it's _beginning_ to appear in core, but also nightly only.
@MrMoonCraft
@MrMoonCraft 10 ай бұрын
@@angeldude101 In the meantime, what is the general rule for low level envs? I'm starting to do some embedded systems stuff with rust on a pi pico. Just panic?
@TheRedAstro
@TheRedAstro Жыл бұрын
You have one of the best Rust channels on youtube. Thanks so much for the clarity and quality of all your videos.
@rsnively
@rsnively Жыл бұрын
Wow this is exactly the kind of video I was hoping for. Thinking about Result as a generalization of Option is a really helpful mindset for thinking about the two types. Even if it's a little more nuanced than that, I think I get too hung up on the extra context that the "Ok" and "Err" names seem to provide. It always bothered me that it's accepted convention for Either to be used this way in Haskell, and I always thought it was just a chance for a "Right is right, lol" joke. But this video really clearly illustrated how helpful of a framework that is.
@totallycarbon2106
@totallycarbon2106 Жыл бұрын
Clicked on the rust, pleasantly surprised by the haskell!
@J-Kimble
@J-Kimble Жыл бұрын
Maaan.. I've said this on a previous video of yours, but still the best rust content on YT! I think what makes your videos great is that you actually explain the theory and logic behind the type system, so that what-s and why-s are a lot simpler to understand.
@cole.maxwell
@cole.maxwell Жыл бұрын
What a FANTASTIC video! Thank you for putting this together. Loved the animation and color coding as you explain!
@d1agnozdi860
@d1agnozdi860 Жыл бұрын
One more channel, telling so much useful information about Rust! I'm all for it, thank you for your videos!
@DrIngo1980
@DrIngo1980 Жыл бұрын
Great video about error handling with Result and Option. I even learned a few things new to me. Top notch content. Subscribed!
@petrusion2827
@petrusion2827 Жыл бұрын
I just discovered your rust videos and I absolutely love them. Subbed and liked
@SemRosto-b9z
@SemRosto-b9z Жыл бұрын
Your Rust videos is a gem. Errors is a hot topic and extremely undervalued here on youtube. It should be covered more deeply. Errors in Rust is something that very much differs from other languages
@coffeetimefun
@coffeetimefun Жыл бұрын
Come for the rust, stay for the category theory! Good pace by the way 😊
@JoeNathan-tl8up
@JoeNathan-tl8up Жыл бұрын
Awesome video, I'm learning rust, I struggle a lot when working with results (how to unwrap, chain them...etc), videos like yours are very helpful. Thanks 🙏
@csalmeida
@csalmeida Жыл бұрын
Thank you so much for putting this video together. It's very rare to see videos where programmers dive into the mental models of how to use features and patterns so that alone is incredibly helpful. That aside, the relationship you've established between Result and Option makes a lot of sense, I was wondering if the difference between the two was just that in one of the them you can access an `Err` but this explanation made me understand the uses much better. Hope you continue to make cool videos like these! 🙏
@keokawasaki7833
@keokawasaki7833 Жыл бұрын
I am loving this in depth discussion of rust types and their usage. Keep up the good work!
@asp-uwu
@asp-uwu Жыл бұрын
Awesome video as always! It should be noted that Result also has a rich meaning - if this function returns, there has been an error. For example, a program that loops forever until some error occurs, like a server. Although, in this context, the name "Infallible" leaves a lot to be desired x)
@PeterAuto1
@PeterAuto1 Жыл бұрын
you can do Result
@asp-uwu
@asp-uwu Жыл бұрын
@@PeterAuto1 Never type is my beloved, but unfortunately it's not in stable :,(
@the-pink-hacker
@the-pink-hacker Жыл бұрын
Creating and dealing with errors in Rust has always been difficult for me. Thanks to you, I know understand them a lot better. I'll definitely look into the thiserror and anyhow crates.
@Sean-of9rs
@Sean-of9rs 5 ай бұрын
This might be pedantic but it was fun to learn about: Never isn't exactly the bottom type. It is an empty type, with no values, but the RFC making it a full type explicitly says that it isn't the bottom type, because it's not a subtype of anything. It coerces to any type, but it isn't a subtype of anything. And this is awesome news for us: otherwise Vec would be a subtype of Vec for any T, and that means we wouldn't be able to write to a Vec. Subtyping in Rust is pretty much limited to lifetimes, which is what seems to allow stuff like Vec to be covariant in rust even though it would have to be invariant in a language like Kotlin.
@jorgedelgado.mp4
@jorgedelgado.mp4 Жыл бұрын
Great video (an channel)! Very well explained. You use some great examples.
@user-zz6fk8bc8u
@user-zz6fk8bc8u Жыл бұрын
The best Rust videos on KZbin. Amazing. Thank you so much for the videos and the time you take to make them.
@nathanoy_
@nathanoy_ Жыл бұрын
Awesome video, as always! You sir, are criminally undersubscribed.
@bigmandan
@bigmandan Жыл бұрын
Great video! I'm new to Rust and this helped me out a lot with a toy project I'm working on where I was having issues chaining iterators and dealing with flip flopping between Options and Results. Looking forward to future Rust videos!
@Skeleton-wn2zu
@Skeleton-wn2zu 11 ай бұрын
Have I ever used Rust?: No. Will I ever use Rust?: Probably not. Do I know most of the things he is talking about?: Definitely not. Did I enjoy the video?: Yes
@djkato-personal2242
@djkato-personal2242 6 ай бұрын
I watch this video every time I'm about to write a library to remind myself how to do it right, and every time I learn and remember a bit more. My journey of errors was Box, anyhow, thiserror, and I'm probably right in that most crustaceans go through this cycle lol. Great vid, thanks. Wish I had it when I started with rust.
@oliverwooding2066
@oliverwooding2066 Ай бұрын
brief note on 10:40 - the return type of a `return` expression has historically been a compiler magic version of unit, and in fact that’s one of the things that has been holding back the stabilisation of `!` as a language feature
@michaelaboah1322
@michaelaboah1322 Жыл бұрын
That was really really good stuff. I’ve never used any of the error crates because I’ve always strived to handle/propagate errors before they hit main. Some notes that I think would have been very beneficial (albeit philosophically) is that errors and the result/option enums are there to represent invalid state. I think you are correct that None should be inferable, but the purpose of Err is to announce and detail what erroneous state has occurred. Take serde_json::Error the error is a enum but for relevant variants it contains line and column information. Great for syntax error but useless or EOF (empty json) The last thing IMHO is that not all errors should be recoverable. For example if you are converting a &[u8] to a string and a non-UTF8 character is provided perhaps you can gracefully handle the error but if recovery is impossible then failure should be defined by the program. Rust is a systems language an OS is not a guarantee. If failure is inevitable then provide relevant context before panic!(). I believe this is the reason why .unwrap and .expect panic on Err. How do you allocate E to T when the variable is expecting T. A case of UB for sure better to panic than let it hit memory
@zacharymontgomery9328
@zacharymontgomery9328 Жыл бұрын
I really love these videos. The precision with which you approach analyzing how and why a piece of code does what it does, and in describing it, is something I have actually been looking for. Along with explaining computation theory with things like data structure isomorphisms, explaining side points like the Never type and the reasons for its existence... it feels like what you are going for is a combination of 'know your tools', with knowing the topic and the practical application. Impressed by how you combine these three. Looking forward to seeing more. Definitely subscribing.
@PrestonThorpe-d1x
@PrestonThorpe-d1x Жыл бұрын
Great content, you explain it very clearly. Hope you continue with stuff like this
@gabrielnuetzi
@gabrielnuetzi Жыл бұрын
Sooo good explained: Love how you peak into architectural misshapes everybody should take to their heart when writing return types. One writes for the usage, and being sloppy on return types and even returning too little sometimes just frustrates the users, so some extra thought is always welcome :) ❤
@sunitjoshi3573
@sunitjoshi3573 11 ай бұрын
Came back to the video after reading some more Rust…makes more sense now. Appreciate making this for us.
@kameko_exe
@kameko_exe Жыл бұрын
In the higher-level areas of my code, I like to separate errors (which now represent many processes) as being in two classes: recoverable and unrecoverable (which is a bit of a misnomer). Unrecoverable errors are when the code/program is missing some vital information, and, unless it's somehow fixed by something or that entire subsystem is reset, the program must fail. Recoverable errors are like I/O errors, where it's more like "This is a benign error that I can just try again at, but if it persists, then there might be a problem with the environment that the user should know about, which is why I'm telling you now." I also prototype with anyhow::Error, I just use it blindly everywhere in a module as I design it. Once I've built up the design, I go in, look at all possible errors, and design my error types properly, rather than trying to guess what I think my API might look like when it's done. I specifically do not represent an invalid program state in errors, as Rust makes invalid program states impossible. So if you wrote code that can have an invalid state, you're just writing buggy code and should remodel your design. Sometimes if you catch yourself writing an error, you can stop yourself to think "wait, do I really need to make this a runtime decision?"
@CYBERNETIC-ANGEL
@CYBERNETIC-ANGEL 8 ай бұрын
concise, easy to understand and straight to the point, you earned yourself a sub
@Proprogrammer001
@Proprogrammer001 Жыл бұрын
I saw this - and I thought: "WOW I NEEDED THIS I WISH I WAS SUBBED TO THIS BIG CHANNEL YEARS AGO". They I saw that I am subbed already. Then I saw that your channel is one of those small channels which I felt needed 20 hours to binge. Then I saw the comment underneath which also says the same T_T This channel will be huge. I'm calling it.
@Proprogrammer001
@Proprogrammer001 Жыл бұрын
I am at 11:20. That blew my mind. Its SO crystal clear I actually had to pause.
@Proprogrammer001
@Proprogrammer001 Жыл бұрын
The thing at 15:56 also blew my mind. What a video sheesh.
@benjaminkinga7797
@benjaminkinga7797 Жыл бұрын
These videos are so good!! Love the use of the Manim library
@dyslexicsoap7605
@dyslexicsoap7605 Жыл бұрын
I subscribed only a short while ago and I see your subscriber count is climbing FAST. These are really good videos.
@parkermcmullin9108
@parkermcmullin9108 Жыл бұрын
Wow, I really appreciated how you stepped through these. Thank you!
@tactical_hen
@tactical_hen Жыл бұрын
You should write a book about Rust. The way you are explaining it is simply amazing ❤
@viktorshinkevich3169
@viktorshinkevich3169 Жыл бұрын
12:12 wow damn, thanks for the traversal tip, I did not know Rust has it🎉 It’s very useful in cases when you actually don’t need to accumulate all potential errors from the collection and just fail when first occurred
@eckelfresh
@eckelfresh Жыл бұрын
That is awesome! I learn more details about the mechanics behind Rust. And you have a very nice style.
@doce3609
@doce3609 Жыл бұрын
These videos are always great explanations and I learn something new everytime.
@lukerogers1440
@lukerogers1440 Жыл бұрын
So clear and easy to follow. Great explanation, thank you!
@TheInspctrcat
@TheInspctrcat Жыл бұрын
One of the best channels about Rust lang
@Galakyllz
@Galakyllz Жыл бұрын
This video was so good. Please, please create more videos. Thank you.
@SyamaMishra
@SyamaMishra Жыл бұрын
Great video on one of the more complex parts of Rust. Thanks so much!
@Starwort
@Starwort Жыл бұрын
A little note at the end - Result can be freely destructured as Ok(foo) is an irrefutable pattern (due to the Err variant being impossible to construct), and (at least IIRC) the optimiser (possibly as a result of NVO?) will unwrap the result at binary level in this case as well
@_noisecode
@_noisecode Жыл бұрын
Result probably has the same size/alignment/ABI as T, so unwrapping is just a no-op at the machine code level: rust.godbolt.org/z/bzssYq57T
@Starwort
@Starwort Жыл бұрын
@@_noisecode yes, that's one of the things I'm saying - the other is that you are allowed to do it by the rules of *Rust* as well (e.g. Ok(true) and Ok(false) are typically bit-compatible with true and false (as long as E is 1 byte or smaller, and fits into NVO), but you aren't allowed to destructure them freely if E is not !) I see how the second part of my statement is confusing; I meant it is unwrapped _at all times_ - due, as you note, to the transparency of the ABI in this case
Жыл бұрын
Please keep that quality Rust content! Thanks for sharing!
@davidweber525
@davidweber525 Жыл бұрын
Coming from Scala, I found this to be really helpful insight into errors in Rust.
@RenaudDenis
@RenaudDenis 10 ай бұрын
Great tutorial, thank you. I second other comments: we would like more videos like this
@TheEnde124
@TheEnde124 Жыл бұрын
Love the comparison to haskell! Haskell has many super cool and useful features.
@fuuman5
@fuuman5 10 ай бұрын
These explanations are high quality stuff.
@mraviteja22
@mraviteja22 Жыл бұрын
this is what exactly I thought about about result and option by my intuition when I first gone through the rust docs, especially for the result enum as generalization, nevertheless great video, keep going.
@bigskyhunter
@bigskyhunter Жыл бұрын
There is a really nice pattern that a Result can be used for even if it conveys the same amount of information as an option. fn
@kirglow4639
@kirglow4639 Жыл бұрын
I hope your channel takes off even more! Awesome informative videos
@jacksonhenry1489
@jacksonhenry1489 Жыл бұрын
I need more so glad I found your channel
@bissbort
@bissbort Жыл бұрын
Your explanations are great. Please keep up the good work!
@batsdk
@batsdk Жыл бұрын
Good Video, Keep up the good work. Really like to see more videos like this
@gergelypaless5042
@gergelypaless5042 Жыл бұрын
Love the 3blue1brown animations! Great video, keep it up. You've just earned one subscriber btw 😊
@conaticus
@conaticus Жыл бұрын
I love the idea of having a safe program, but it really irritates me when I have a tree of functions that returns a result, and I have to set every single function to return a result. Maybe I'm just a bad at Rust though 😂
@theherk
@theherk Жыл бұрын
Very useful information and excellent production.
@hl2mukkel
@hl2mukkel Жыл бұрын
Amazingly well done videos, can't wait for more!
@bob450v4
@bob450v4 Жыл бұрын
Your rust videos are incredible 🎉
@XxFlashTuTorialsxX
@XxFlashTuTorialsxX Жыл бұрын
I appreciate that you styled your animations after 3Blue1Brown - maybe using his graphics library? - excellently put, super well documented on screen, and interesting. Keep them coming!
@AK-vx4dy
@AK-vx4dy Жыл бұрын
Very comprehensive video and good share of experience.
@evlogiy
@evlogiy Жыл бұрын
Thanks for your content! ❤
@ronminnich
@ronminnich 3 ай бұрын
this is very helpful, things I thought I knew I learned more about ... :-)
@yaksher
@yaksher Жыл бұрын
@4:00 I honestly disagree with this framing quite a bit. HashMap::get(...) returning nothing is _not_ an "error." You just looked up the value, and there was nothing corresponding to that key. There is I think a meaningful semantic difference between "something went wrong" and "everything went fine, but no value exists", and that's especially important in cases where you start combining the two, i.e., something like Result. The way I see it, this is a perfectly reasonable type for, for example, looking up a key in a database. The database lookup might _fail,_ in which case this would be Err(_), or it might succeed, and if it succeeds, the value might be there or not. This seems like a much more natural separation to me then, for example, the error type here being a union of "database lookup failed" and "database lookup succeeded, but there was no value." I would argue, on the other hand, that the type signature Result is basically never the right answer, because a function that can fail in two different ways should not be nesting these failures, it should just use some kind of unified type, whether that's a trait object or an enum or some other standardized error type. For completeness, I think Option also theoretically makes sense (for example, if you have an Option for whatever reason, then this very naturally converts to an Option via a mapped lookup function), though I feel like it's less likely and natural than Result. Basically, I think Result represents "something went wrong." For file IO errors, or network errors, or whatever else, this often means "you should try again" or whatever else. In general, I'd say it means one of two things: "there is a contract on the values you can give this function, and the error represents a violation of that contract" and "there is some part that's outside the callers control and may go wrong." On the other hand, an Option represents a function where the answer to its question might just be "there's nothing." The difference, I'd say, between opening a file which doesn't exist (which is and should be some sort of NotFound error) (of course, there are numerous other reasons a file might fail to open which are undeniable errors, but one could argue that under my model, opening should be a Result, but I'd disagree) and looking up an nonexistent value in a hash map (which is and should be a None value) is that the file system is outside the program. Who knows why the file wasn't found. Maybe the working directory of the process is wrong, maybe somebody else already deleted the file-or someone responsible for making it failed to do so-or whatever else. On the other hand, a hash map exists (under the vast majority of cases, anyway) entirely with the control of the caller, and if something isn't there, it's just because they never put it in there.
@MichaFita
@MichaFita Жыл бұрын
I was going to write more less the same.
@_noisecode
@_noisecode Жыл бұрын
I very much appreciate this perspective and I think this does a good job capturing a lot of the nuance that I admitted that my "overly simplistic" framing lacked. I do think there is a LOT of subjectivity on this topic though. You mention that one reason to return an error is that "there is a contract on the values you can give this function, and the error represents a violation of that contract" (I like the way you put this for the record). But - how does that not apply to the HashMap case? You could just as easily argue that the contract of HashMap::get is that you provide a key that exists, and if you violate that contract, you get back a None. Or the other thing you say -- "there is some part that's outside the caller's control and may go wrong"; maybe in my function I'm writing I'm not the one putting the values into the HashMap, I'm just reading them, so the key not existing was outside my control. Differentiating HashMap::get from a file-not-exists error requires placing a line in the sand between the two at a point that's going to be arbitrary and subjective. You say they are fundamentally different because one exists outside the process and can fail for many reasons, whereas the other exists inside the process and can fail for just one reason; I argue that those are not fundamentally different things, they're two flavors of the same larger concept of failure. I like what you say about Result and Option and I agree there is a place for both if and when they make APIs clearer. I wouldn't necessarily rule out Result from the same discussion--there could be situations where it could fit the model of the problem quite well (like loading a binary file _and then_ converting it to UTF-8? Where each layer of errors represents a phase of the process?). Personally I do find it "ugly" and I might steer clear of it on those grounds, but this is what I'm saying about these things being largely arbitrary and subjective.
@MichaFita
@MichaFita Жыл бұрын
@@_noisecode there's matter of certain convention most people would stick to. I've seen code where Option was used where Result should be. By design and nature of it HashMap may not have element at given key and that fact remain under our control. Convention say we have errors for situations beyond our controls (however, there are people using exceptions for normal control flow), for situations we have to react to, but which break a normal flow execution (hence usefulness of ? operator). I see the point in your simplification for whole context, and probably any experienced programmer sees one, but it may be very misleading and confusing for beginners.
@_noisecode
@_noisecode Жыл бұрын
Thanks for the added nuance here; again I appreciate digging into the details of why what I presented might be overly simplistic. On the topic of being misleading for beginners--I guess I don't see it that way. The guideline I present in the video isn't _incorrect_ and it guides you toward writing good APIs in my opinion. It might be an overly simple way to get a beginner started in thinking about this stuff, but it's not false or harmful in any way. The shades of nuance for a beginner can come after some time and experience building good habits brought about by giving them a simple mental model to get started with.
@yaksher
@yaksher Жыл бұрын
@@_noisecode I'll admit there's a lot of subjectivity on what you consider "everything went fine: the answer is there's nothing there" and what you consider "something went wrong", but I think even with subjectivity on _where_ each one applies, the distinction between these two is relatively concrete.
@kamertonaudiophileplayer847
@kamertonaudiophileplayer847 Жыл бұрын
Your take is correct.
@niilojoukanen106
@niilojoukanen106 Жыл бұрын
These kind of more advanced but easily followed videos are incredibly valuable. Thank you so much, keep doing what you are doing!
@RakavyYuval
@RakavyYuval Жыл бұрын
Excllent content, learned quite a bit! Thanks
@lexer_
@lexer_ 9 ай бұрын
I was uncompfortable with Result for some time because of the confusing builtin syntax around the type not the Result type itself. There are so many different way of interacting with them like unwrap and ? and OK() and all that. It took me some time to actually find a resource that packed all the different ways of interacting with Result in one place and when to use which one. The result type propagation of ? was particularly confusing at first because it seems like implicit magic if you don't know what you are looking at. That was some time ago now though (4 years or so). So I assume the explanations of this are probably much easier to find and well written compared to back then.
@James-the-elder
@James-the-elder Жыл бұрын
This is more like Rust for Computer Science Students and I love it
@Turalcar
@Turalcar Жыл бұрын
One of the annoying things about using Infallible is that Ok(v) is not considered exhaustive for Result so I have to do r.unwrap_or_else(|e| match e {}) or something like that.
@benibachmann9274
@benibachmann9274 Жыл бұрын
anyhow rocks! I‘m using it in every project
@dzuchun
@dzuchun Жыл бұрын
10:33, OH SO THAT'S WHAT NEVER IS USED FOR! I've had a trouble understanding, why would you even need a bottom type, but now I see. makes sence.
@aleksanderkrauze9304
@aleksanderkrauze9304 Жыл бұрын
Great video as always! I would love to hear you talking about manual v-table implementations. I know about two crates that use them internally (anyhow and bytes) and have some general idea of how it works, but I would love to hear an in-depth explanation of its tradeoffs, details, design patterns and maybe event unstable std APIs `std::ptr::{Pointee, DynMetadata}`.
Rust Functions Are Weird (But Be Glad)
19:52
Logan Smith
Рет қаралды 143 М.
Constructors Are Broken
18:16
Logan Smith
Рет қаралды 112 М.
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН
Embracing Recoverable Errors: Rust Error Handling Uncovered - Lesson 19
11:42
Hyperbolic Time Academy
Рет қаралды 717
but what is 'a lifetime?
12:20
leddoo
Рет қаралды 81 М.
Async Rust Is The Bane Of My Existence | Prime Reacts
35:36
ThePrimeTime
Рет қаралды 109 М.
Rust Error Handling - Best Practices
21:33
Jeremy Chone
Рет қаралды 26 М.
Choose the Right Option
18:14
Logan Smith
Рет қаралды 74 М.
Cursed C++ Casts
17:41
Logan Smith
Рет қаралды 75 М.
Two Ways To Do Dynamic Dispatch
19:54
Logan Smith
Рет қаралды 80 М.
Rust's Most Important Containers 📦 10 Useful Patterns
17:11
Code to the Moon
Рет қаралды 130 М.
Arc instead of Vec? | Prime Reacts
37:18
ThePrimeTime
Рет қаралды 68 М.
Why JavaScript Devs are Switching to Rust in 2024
10:35
warpdotdev
Рет қаралды 263 М.
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН