5 deadly Rust anti-patterns to avoid

Поділитися
Вставка
  • Опубліковано 3 гру 2024

КОМЕНТАРІ • 79

  • @letsgetrusty
    @letsgetrusty  18 годин тому +4

    Get your *FREE Rust training* :
    letsgetrusty.com/training

  • @realcundo
    @realcundo 16 годин тому +81

    Nitpick: `panic!()` doesn't terminate the program, it terminates the current thread only. Another thread can then observe the panic.

    • @linkernick5379
      @linkernick5379 16 годин тому +3

      Nice catch 👍

    • @jackthereaper9379
      @jackthereaper9379 14 годин тому +2

      can you tell me atleast briefly how any other thread can observe the panic? I wanna know, it might be useful for a project of mine.

    • @realcundo
      @realcundo 14 годин тому

      ​@@jackthereaper9379 The simplest is to look at docs for std::thread::Result, there's an example. In short, when you spawn a thread, you get a handle and when you join that handle, it'll return Err if the spawned thread has panicked. It'll return Ok if the thread finished without panicking.

    • @iceginlychee
      @iceginlychee 14 годин тому +1

      I would be interested to know as well

    • @realcundo
      @realcundo 14 годин тому +7

      Weird, I'm pretty sure I replied, maybe it'll appear twice. The simplest is to look at docs for std thread Result, there's an example. After spawning a child thread, you wait for its completion by calling join. Join will return that Result and if it's an Err, the child thread has panicked. Edit: it the result is Ok, it's the value that the function executed in the child thread returned.

  • @ahmetyazc7969
    @ahmetyazc7969 16 годин тому +32

    I think proper use of panics still has its place in applications, specifically if the error is unrecoverable and expected to never happen. Leaving the app running while the state of the application reached to a point where it should never reach is more problematic, and halting the whole program is the best option.

    • @rustisbae
      @rustisbae 13 годин тому +8

      I would also say its application dependent. For example, you wouldnt want a server just crashing out of nowhere, however a client app could deal with a crash. I just dont like to use panics personally lol 😅

    • @u-k
      @u-k 7 годин тому +1

      @@rustisbae But you WOULD want to use it if say, an API key gets unauthorized or a wrongly formatted environment variable is detected.

    • @rustisbae
      @rustisbae 5 годин тому

      @@u-k Right, that would be a reasonable to panic. But that's something that happens at startup, not while the server is up and running :p

    • @letsgetrusty
      @letsgetrusty  2 години тому +1

      Yes! There are certainly use cases for panic but they are typically infrequent. The problem is developer use panic outside of the specific circumstances you've mentioned.

  • @RoamingAdhocrat
    @RoamingAdhocrat 8 годин тому +4

    Just want to say how very very nice it is to see the five patterns in the title immediately listed when the video starts. Really helps me orient myself and understand what you're communicating!

  • @cunningham.s_law
    @cunningham.s_law 17 годин тому +26

    if the data is small cloning is faster than arc most of the time

    • @follantic
      @follantic 9 годин тому

      And clone uses less RAM than Java or JS. RAM is expensive while CPU is often not the bottleneck. And lifetimes can get annoying at times. YMMV

    • @eli1882
      @eli1882 5 годин тому

      Performant but more memory usage.

    • @letsgetrusty
      @letsgetrusty  2 години тому

      Yes good point but then you run into the issue of having multiple sources of truth

    • @IceQub3
      @IceQub3 Годину тому

      ​@letsgetrusty the data is immutable

  • @havefuntrading6263
    @havefuntrading6263 15 годин тому +7

    Sometimes (like 1 out of 10 situations), it's more ergonomic to use panic! rather than introduce bespoke error type. Also, quite a few crates will use panic! internally, so there is not much you can do about it (apart from catch_unwind), even though your crate handles all errors in a clean way.

    • @letsgetrusty
      @letsgetrusty  2 години тому

      Yes I agree with that when it comes to your own codebase. In terms of libraries, panicking should not be done unless there's a VERY good reason. I'm curious which crates you are referring to.

  • @clot27
    @clot27 17 годин тому +30

    I will keep cloning

    • @sutsuj6437
      @sutsuj6437 8 годин тому +2

      Oh you don't want to clone and instead use a reference? Just re-architect the entire codebase... Nah I'm gonna keep cloning. 'Life is too short for lifetimes

    • @affanyunas7693
      @affanyunas7693 Годину тому

      @@sutsuj6437 i love rust untill i meet lifetimes

  • @Shawnecy
    @Shawnecy 5 годин тому +1

    If you have only one reader and one writer on different threads, you may want to use Arc instead of Arc. Further for that case of a single reader and writer, if your type is integral or boolean, then you don't even need the Mutex (data that is 64 bits or less can already be read and written atomically via the Arc).

  • @awdsqe123
    @awdsqe123 16 годин тому +5

    5:26 Think you need to put `&self.settings` even when self is a reference.

  • @Mempler
    @Mempler 17 годин тому +8

    You can put if statements in matches?!?

    • @AndrewBrownK
      @AndrewBrownK 17 годин тому +1

      It’s the beeessssst

    • @Caldaron
      @Caldaron 16 годин тому

      the perfect question for claude/chatgpt

    • @pmmeurcatpics
      @pmmeurcatpics 11 годин тому +1

      Yup, they're called guard cases!

    • @follantic
      @follantic 9 годин тому

      I was genuinely confused by that. 7:55 for reference.
      Looks awesome.

  • @evols7028
    @evols7028 16 годин тому +3

    Be careful when sharing writable data between threads. It can lead to false sharing, which is a degradation of performance when two threads constantly write to data contained in the same cache lane at the same time. In this case, making copies then reconciling them after the fact is better

  • @gamekiller0123
    @gamekiller0123 17 годин тому +6

    Using unwrap/expect in production code is not an antipattern. Sometimes you know statically that something cannot be None/Err, but getting the type system to help is impractical or impossible. There are also functions with preconditions, and panicking when these aren't upheld to provide a nicer API is fair.

    • @orterves
      @orterves 12 годин тому +1

      Unwrap and expect should be avoided except as a last resort and it's better if the function preconditions can be expressed in the input types so that the function can't even be called with invalid inputs.

    • @Nesdac-k1l
      @Nesdac-k1l 11 годин тому

      yes, but avoid `unwrap` especially since it cannot be given a message. and prefer `assert` where possible.

    • @Nesdac-k1l
      @Nesdac-k1l 11 годин тому

      ​@@ortervesoften it cannot be expressed in the parameter types. eg. a parameter for an index in a slice. it could also js be incredibly unnecessary to create a custom type just to ensure some invariant. eg. a non empty vec

    • @pmmeurcatpics
      @pmmeurcatpics 11 годин тому

      ​@@Nesdac-k1lfunnily enough `NonEmptyVec` is actually a thing. I was in a situation where I actually needed that, and found out that there's a crate for that™

    • @pmmeurcatpics
      @pmmeurcatpics 11 годин тому +1

      "the variable cannot have this value" is better expressed with `unreachable!`

  • @qwerwasd1724
    @qwerwasd1724 16 годин тому +2

    Is panic! okay to use if there's no recovery possible?

    • @Nesdac-k1l
      @Nesdac-k1l 11 годин тому

      yes, it is usually recommended if unrecoverable.

  • @newplayer7743
    @newplayer7743 16 годин тому +1

    New pattern matching syntax that I didn't know about.

  • @circuitbreaker7860
    @circuitbreaker7860 29 хвилин тому

    Do you have recommendations for how to handle a result in a function that can't return a result? (E.g. closures or implementing trait methods of library crates, that you do not own, which have a overly simply return type like String)
    Having to discard the entire error stack up to that point always feels so wrong.

  • @TheNoirKamui
    @TheNoirKamui 8 годин тому

    Also using Box without any good reason. Most of the time it is better to use impl Trait in function properties for static dispatch. Or even better fn() pointers for your own code.

  • @oliverfoxi
    @oliverfoxi 14 годин тому +1

    I thought in Lazy-man error handling you will talk about mindless usage of `?` with anyhow lib

  • @iceginlychee
    @iceginlychee 14 годин тому

    Very informative stuff, thank you as always

  • @s1v7
    @s1v7 13 годин тому

    explain please how to create custom error types and what are the best practices there

    • @Nesdac-k1l
      @Nesdac-k1l 11 годин тому

      check out `thiserror` crate for a simple/easy solution.

  • @anlumo1
    @anlumo1 37 хвилин тому

    I miss the mentioning of the `Arc` anti-pattern, but maybe that's too complex for this kind of video and needs its own instead to explain why it's problematic.

  • @MasterHigure
    @MasterHigure 11 годин тому

    Just so it's clear, points 1 and 3 are entirely allowed in Leetcode, Project Euler, Advent of Code, and similar settings. Unwrap and clone everything. Just make it run and get your answer.

  • @henning.langhorst
    @henning.langhorst 10 годин тому +3

    The German translation is really awful.

  • @scheimong
    @scheimong 4 години тому

    5:28 I think you need `&self.settings` instead.

  • @01joja
    @01joja 13 годин тому

    At 9:07 you switched place on number 2, 3 and 4.

    • @letsgetrusty
      @letsgetrusty  2 години тому

      Good catch, editing mistake :)

  • @soberhippie
    @soberhippie 11 годин тому +1

    The biggest rust sin is not using Rust

  • @v-sig2389
    @v-sig2389 11 годин тому +8

    Please don't use auto-translate for your titles, the translation is unbearable.

    • @follantic
      @follantic 9 годин тому

      Didn't see an auto translated title, but I definitely still agree.

    • @letsgetrusty
      @letsgetrusty  2 години тому +1

      Fixed! YT started doing this automatically -_-

  • @DataPastor
    @DataPastor 2 години тому

    How is it possible? I thought, if it compiles, then it is good. /s

  • @LostRunner0213
    @LostRunner0213 13 годин тому

    very interesting even for a beginner like me.
    but youtube enabled the audio translation. what a pain in the a** !

  • @sighupcmd
    @sighupcmd 16 годин тому +3

    This missing enum variant problem can be huge problem

  • @tomkimsour
    @tomkimsour 10 годин тому +1

    la voix française est insupportable et on peut pas la changer sur browser de téléphone. Mais sinon good content

  • @pierreollivier1
    @pierreollivier1 10 годин тому

    Man I love how Rust is just becoming C++. You have a problem ? don't worry we have syntax to solve it. Kidding love rust :) (still plz don't become C++)

  • @naive_algorithm
    @naive_algorithm 2 години тому

    I am `unwrap_or` user 😂😂😂

  • @spaghettiking653
    @spaghettiking653 5 годин тому

    Rust propagandist try not to say "powerful" challenge (impossible)

  • @chonkusdonkus
    @chonkusdonkus 15 годин тому

    I really dislike the black background in this video