Smart Pointers in Rust - Interior Mutability

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 พ.ย. 2024

ความคิดเห็น • 46

  • @letsgetrusty
    @letsgetrusty  3 ปีที่แล้ว +6

    📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet

  • @arisweedler4703
    @arisweedler4703 3 ปีที่แล้ว +27

    Great stuff. Broken down so simply. Rust is fantastic. This is the type of stuff that C’s syntax lets your burn yourself on so easily. And this C++ was forced to lemming after it.
    The verbosity of syntax around this is a feature, and it makes your wonderful explanation as simple as just reading the code.
    Great language, great channel, great content! Thanks for saving me tons of time :D

  • @stephensurtees4026
    @stephensurtees4026 2 หลายเดือนก่อน

    I'm sitting here having a cocktail, and moving on to some nice whisky. This video is amazing! The content is very clear, and for me it's just reinforcing the idea that rust really isn't that different or special compared to other languages I know, it's just really explicit. You need to be absolutely clear about what you need a particular reference to do, and it's all built into the type system. Wonderful. Thankyou for this video.

  • @brianteague8031
    @brianteague8031 2 ปีที่แล้ว +48

    Borrow checking at runtime? I feel like if I have to use RefCell, there is something wrong with my design pattern.

    • @nicholasmanson8615
      @nicholasmanson8615 2 ปีที่แล้ว +15

      Or you just need a one off workaround you know won't fail and don't want to rewrite when a simple tool for the situation was already provided by rust staff. Otherwise though mostly yeah

  • @julianferres
    @julianferres 2 ปีที่แล้ว +26

    usize adapts to u32 or u64, depending on the architecture of the computer (32/64 bits). It's more "portable" to write it this way.

    • @alvaroefmota
      @alvaroefmota ปีที่แล้ว +1

      very interesting

    • @SharunKumar
      @SharunKumar ปีที่แล้ว +2

      Wouldn't this cause non-deterministic behavior like different overflow limits based on the target system 🤔

    • @TylerLarson
      @TylerLarson 10 หลายเดือนก่อน

      ​@@SharunKumar the idea is that your app's business logic already guarantees you won't overflow either value, and you just want the efficiency of using whatever type is native to your architecture.

  • @jakubwojciech1
    @jakubwojciech1 3 ปีที่แล้ว +2

    Hi Bogdan,
    thank you very much for all your videos !!! keep doing it, they are great!

  • @markmcdonnell
    @markmcdonnell 3 ปีที่แล้ว +1

    Another super useful and enlightening video. Great work as always. Thank you!

  • @herrxerex8484
    @herrxerex8484 3 ปีที่แล้ว +2

    Was waiting for this , keep it up bogdan!

  • @thegeniusfool
    @thegeniusfool 6 หลายเดือนก่อน

    The Halting Problem…. That was quite a curve ball in this context.
    Nice

  •  2 ปีที่แล้ว

    Excellent video. It's all about combining.

  • @Galakyllz
    @Galakyllz 2 ปีที่แล้ว +4

    Should we use smart pointers or is this an indication that we need to restructure our code? My question comes down to "Is this a cheat or bandaid for a problem with our project's architecture?"

    • @durnsidh6483
      @durnsidh6483 ปีที่แล้ว +3

      Unless you're using raw pointers, some type of smart pointer is necessary to put values on the heap which is needed if you don't know how much memory you need to allocate for an object at runtime. That being said, I don't like interior mutability that much and it's a good idea to see if you can refactor your code first.

    • @jonforhan9196
      @jonforhan9196 ปีที่แล้ว +2

      Multi threading is almost impossible without async ref counting

  • @technoflask4525
    @technoflask4525 3 หลายเดือนก่อน

    Great Video.💕
    But just one suggestion.
    You should use Cell instead of RefCell in the Cons-list example. As i32 is a copy type, Cell is a better choice as it doesn't have a runtime overhead like RefCell.
    I feel like for copy types RefCell is an overkill.

  • @climatechangedoesntbargain9140
    @climatechangedoesntbargain9140 3 ปีที่แล้ว +1

    Next one Cell? 😉

  • @bjugdbjk
    @bjugdbjk 2 ปีที่แล้ว

    I just wanna smash the like button infinite times bro !! Fantastic content 👍 need more rusty stuff

  • @sagnikbhattacharya1202
    @sagnikbhattacharya1202 3 ปีที่แล้ว +2

    Amazing video as always :D

  • @GolangDojo
    @GolangDojo 3 ปีที่แล้ว +1

    Fresh cut bro

  • @soft384
    @soft384 3 ปีที่แล้ว +3

    I think IAM getting rusty!!

  • @kampkrieger
    @kampkrieger 2 ปีที่แล้ว

    Great! But how can I handle the Panic and go on with my program?

  • @jjpp1993
    @jjpp1993 11 หลายเดือนก่อน +1

    sooo to know how to read the book

  • @exoticcoder5365
    @exoticcoder5365 3 ปีที่แล้ว

    Very useful Thanks !

  • @myronkipa2530
    @myronkipa2530 2 ปีที่แล้ว +1

    Could this be abstracted somehow? Seems unnecessarily complicated

  • @julkiewitz
    @julkiewitz 3 หลายเดือนก่อน

    Hmm, how is that manual ref counting actually better than something like a read write smart pointer in C++? It doesn't really check anything at compile time, so it's all runtime. And if it's all runtime it can still just panic. Which is functionally same as using a weak_ptr etc. Seems like there should be some very strong rules of thumb about when to use that. If I just sprincle RefCell all over the place, it seems to defeat the point of the language.

  • @CompletelyCovered3
    @CompletelyCovered3 3 ปีที่แล้ว +2

    Help! I am trying to write macros! A macro series would be great! 😁

    • @letsgetrusty
      @letsgetrusty  3 ปีที่แล้ว +7

      Coming soon!

    • @CompletelyCovered3
      @CompletelyCovered3 3 ปีที่แล้ว +1

      @@letsgetrusty Thank you so much. You are, in deed, converting a Gopher. Go's great, and I'll continue to use it. But at some point, the simplicity is limiting.

  • @solomon6823
    @solomon6823 27 วันที่ผ่านมา

    I'm a noob and I dont seem to understand why do we this instead of :
    pub trait Messenger {
    fn send(&mut self, msg: &str);
    }
    just making a mutable self reference

  • @NonsensGaming
    @NonsensGaming ปีที่แล้ว +1

    remember kids use Cell instead of RefCell for primitives that implement copy
    not a big fan of the Rc and RefCell example

  • @codeultima2386
    @codeultima2386 3 ปีที่แล้ว +2

    great easy to understand, can you do the Fearless concurrency chapter? thanks

  • @murugarajuperumalla5508
    @murugarajuperumalla5508 2 ปีที่แล้ว

    go go Rusty

  • @krtirtho
    @krtirtho 3 ปีที่แล้ว +2

    Smart Pointers in Rust are like shortcuts to avoid reference errors

  • @homelikebrick42
    @homelikebrick42 3 ปีที่แล้ว

    What is your vscode setup?

    • @letsgetrusty
      @letsgetrusty  3 ปีที่แล้ว +3

      it's very basic. I just use rust-analyzer and tabnine.

    • @DrIngo1980
      @DrIngo1980 3 ปีที่แล้ว +2

      @@letsgetrusty I bet that question will come up more often in the future and people do not necessarily read all the comments or your replies. So my suggestion for a quick win: make a short video. Doesn't have to be longer than 5 minutes, just to explain your dev setup. People will appreciate it. And it will make it easy for you in the future to just link to it. If your setup ever changes, great. Another opportunity for yet another video 😊 (This is just a suggestion. Feel free to ignore.)

  • @aaron6807
    @aaron6807 ปีที่แล้ว +3

    This seems pretty unsafe

    • @32zim32
      @32zim32 ปีที่แล้ว +1

      I think RUST is safe is the biggest misconception of all times. If there are tons of RefCell, Arc, Rc and others, all of them can panic at runtime. So I really can not understand how this can be called a safe language

    • @fiddle_n
      @fiddle_n ปีที่แล้ว +1

      ⁠@@32zim32Any language can panic at runtime. When people say Rust is safe, they are referring to MEMORY safety. Using Rc and RefCell may cause your code to panic but it won’t cause your program to have undefined behaviour

  • @cerulity32k
    @cerulity32k 2 ปีที่แล้ว

    i think ill call them smointers

  • @ヤッシン_y4ssin
    @ヤッシン_y4ssin 2 ปีที่แล้ว

    huh

  • @jeffg4686
    @jeffg4686 2 ปีที่แล้ว

    Good tutorial, but it looks like copy types got dissed