A Deep Dive Into the Rust Ownership Model

Поделиться
HTML-код
  • Опубликовано: 28 июн 2024
  • In this video, I’ll take a closer look at how Rust manages memory with an ownership model, which is quite unique and really powerful. Stay tuned to find out what it is exactly, how it works, and why you need it.
    👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis
    🔥 GitHub Repository: git.arjan.codes/2024/rustown
    💻 ArjanCodes Blog: www.arjancodes.com/blog
    ✍🏻 Take a quiz on this topic: www.learntail.com/quiz/twmfal
    Try Learntail for FREE ➡️ www.learntail.com/
    🎓 Courses:
    The Software Designer Mindset: www.arjancodes.com/mindset
    The Software Architect Mindset: Pre-register now! www.arjancodes.com/architect
    Next Level Python: Become a Python Expert: www.arjancodes.com/next-level...
    The 30-Day Design Challenge: www.arjancodes.com/30ddc
    🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
    👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!
    Social channels:
    💬 Discord: discord.arjan.codes
    🐦Twitter: / arjancodes
    🌍LinkedIn: / arjancodes
    🕵Facebook: / arjancodes
    📱Instagram: / arjancodes
    ♪ Tiktok: / arjancodes
    👀 Code reviewers:
    - Yoriz
    - Ryan Laursen
    - Dale Hagglund
    - Kit Hygh
    - Alexander Milden
    - Bean
    🎥 Video edited by Mark Bacskai: / bacskaimark
    🔖 Chapters:
    0:00 Intro
    1:07 Basic rules of ownership in Rust
    1:58 Borrowing
    6:14 Stack Vs Heap
    9:44 Lifetimes
    11:23 Conclusion
    12:43 Outro
    #arjancodes #softwaredesign #python
    DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

Комментарии • 70

  • @ArjanCodes
    @ArjanCodes  2 месяца назад +1

    👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis

  • @srivatsa1193
    @srivatsa1193 2 месяца назад +62

    would love to see more rust content similar to your python content - design patterns, etc.

    • @Fran-kc2gu
      @Fran-kc2gu 2 месяца назад

      Design patterns are the same no matter the language lol

    • @TheiPodMac1234
      @TheiPodMac1234 2 месяца назад +3

      Actually, they can be quite different if you do not use OOP. Compositional programming or data driven programming requires different patterns that are more suitable if you can't use for example inheritance. And usually they are a bit more counter intuitive. So for me as well:
      I would love to see some Rust design pattern content ! 🎉

  • @Far_Gem
    @Far_Gem 2 месяца назад +18

    6:36 What you say here is not quite right: s2 is not allocated on the stack. On the stack, there is only a "fat pointer" which points into the contents of s1 on the heap. I.e. it references data within s1. This is also made clear by the "&" in its type.

  • @iduran
    @iduran 2 месяца назад +8

    Thank you for this intro to Rust ownership model. Would love to see more Rust content in your Channel.

  • @tyler209459023523
    @tyler209459023523 2 месяца назад +8

    Super excited that you've started creating Rust content!

  • @_baco
    @_baco 2 месяца назад +6

    Now that you mention it... I've actually started following the channel for the Architecture content like 2 and a half years ago. But I am a Python developer and I enjoy the “Python view” on architecture designs in contrast of the Java view of many bibliography.
    I am also liking Rust, so having **also** content in Rust is also a plus 😄

    • @ArjanCodes
      @ArjanCodes  2 месяца назад

      That's really cool to hear! I'm glad you're enjoying the content, thank you for the support!

  • @damymetzke514
    @damymetzke514 2 месяца назад +7

    Rust has been my favorite language for a while now, and the reason is actually one of your latest points. I realized that the borrow checker was making me structure my code better. I've actually picked up Python after this happened, and learned that I liked it more than I realized. Not really the language to be honest, more so the tooling and ecosystem. I'm planning on trying out pyo3 in the future on my projects, so hopefully that will go well.

  • @RobertoDuransh
    @RobertoDuransh 2 месяца назад +11

    love to see more rust

  • @wizardfix
    @wizardfix 2 месяца назад +1

    Thanks for a great explanation of ownership in Rust, which I've just begun to learn. In other languages I kind of assume anything declared inside a function is stored on the stack, and global variables on the heap. Having control over memory in this way is something new to me.
    Great stuff Arjan - more on Rust would be great!

  • @shibak4
    @shibak4 2 месяца назад

    Thank you for doing this great video. You explained a great deal of the main concepts in the first chapters of the Rust book in very simple examples. I'll be keeping this as a reference.

  • @ajmash9745
    @ajmash9745 2 месяца назад +12

    love the rust content!!

    • @ArjanCodes
      @ArjanCodes  2 месяца назад +2

      Glad to hear that! :)

    • @takis88
      @takis88 2 месяца назад +2

      From comments it is pretty obvious that the community craves for a mix of Rust and Python content.
      Another idea is to walk through common Rust/Py projects: Modin, PyO3, Polars, Pydantic 2 etc

  • @SuperHardik12345
    @SuperHardik12345 2 месяца назад

    Thank you, for explaining ownership and borrowing content in such an easy way, But if possible, then please make more descriptive video for particularly Rust content.

  • @PhunkyBob
    @PhunkyBob 2 месяца назад

    I can't wait for next April 1st to see if Arjan do a Visual Basic video 😇

  • @naveenr4030
    @naveenr4030 2 месяца назад

    Seeking for more rust videos from you

  • @GOTHICforLIFE1
    @GOTHICforLIFE1 2 месяца назад

    I honestly would love if you did segments and concepts for most of the more used languages. This channel always felt like one tailored to the more intermediate devs. And i think getting an insight in how other languages solves certain problems helps greatly in appreciate how things are built and why certain patterns work better in one language over another.

  • @masiarek
    @masiarek 2 месяца назад

    yes, more Rust please. Thank you Arjan!

  • @nothiiiiiiiing
    @nothiiiiiiiing 2 месяца назад +2

    The perfect length

  • @AceofSpades5757
    @AceofSpades5757 2 месяца назад

    Rust is incredible. I started learning it as a way to speed up my Python and fell in love. I try to use it when I can.

    • @ArjanCodes
      @ArjanCodes  2 месяца назад

      I'm glad you're having a positive experience with Rust! Thank you for the comment

  • @djl3009
    @djl3009 2 месяца назад

    Nice. Essential/fundamental Rust concepts like this are a great addition to your channel and works well alongside your Python content. 👌👍

  • @pjgierz
    @pjgierz 2 месяца назад

    Hi @ArjanCodes,
    as always, a great video! I have learned a lot from you over all the videos over the years, both for conceptual design, and for specific languages. One thing that might be considered (maybe just for me) and what was a little distracting: all the "hover-over" pop ups that come (which are of course totally useful for the programmer) make it (for me at least) difficult to follow along. Maybe you can selectively disable these for your presentations.
    I look forward to learning more about program design -- and Rust, my new favourite thing ;-) -- from you in the future!
    Cheers from Bremen!
    Paul

  • @alexanderzikal7244
    @alexanderzikal7244 2 месяца назад

    I am working a lot with Python and the sklearn-Library. For analysing labels of 1 plant my resources are enough (16GB, M1 Apple). Connecting data from all plants is a story for the future. Now I am learning and refreshing my knowledge about Linear Algebra.
    Reducing dependencies works perfect with Random Forest Regressors and is running very fast.
    Work with flax, Tensorflow or rewrite Important critical functions in Zig or Rust? Apple also delievered mlx at 2024-02. A lot of questions…
    I love and enjoy Your Rust-Stuff also as Python-Videos!👍

  • @phortheman
    @phortheman 2 месяца назад

    I love the Rust content on the channel! Hope to see more!

  • @sebastiencourtois8696
    @sebastiencourtois8696 2 месяца назад +2

    Mojo uses the same ownership model except that values are borrowed by default. Value copy must be explicit and, coming from python, I prefer that approach.
    Thank you, Rust

  •  2 месяца назад

    Thank you. Sure, I would like to see more of your content about Rust, even pyo3 for Python-Rust

  • @kkollsga
    @kkollsga 2 месяца назад +2

    At 9:21 why can you move the string inside the fuction from the heap to the stack without making any changes to the reference? Isnt the String variable still on the heap? Confused 🤪

    • @Far_Gem
      @Far_Gem 2 месяца назад +2

      Arjan's explanation is unfortunately wrong here. &str is not a stack-allocated type, it's a reference to a slice of a string which lives on the heap. The Rust compiler is clever enough to be able to accept a reference to an "entire" string ("&String") in place of a reference to a slice ("&str"). If you want to know more details, you can google "Deref Coercion".

  • @TheEvertw
    @TheEvertw 2 месяца назад

    "This leads to better design"
    This is EXACTLY why I think that Rust has a great future in store. Rust is the first significant innovation in programming languages in three decades, after the invention of the object-oriented paradigm.
    OK, support for co-operative multi-tasking through `async` functions was also a significant innovation. But Rust has revolutionized type systems and memory models, which I think is a bigger innovation, and definitely of greater importance for the future of programming.

  • @tobidegnon4409
    @tobidegnon4409 2 месяца назад +2

    Heap, stack, would love a deep video about how memory works, seems like a fundamental.
    Great video as usual

  • @jamesarthurkimbell
    @jamesarthurkimbell 2 месяца назад

    I remember once hearing someone say "Python doesn't have variables," and I had no idea what they meant. But in contrast, you can really tell the difference between this model, where a name owns a value directly, and Python's, where names and values float in completely separate worlds.

  • @UNgineering
    @UNgineering 2 месяца назад +1

    Visual Basic for the win!
    I'm willing to bet, a good chunk of our economy relies on excel "databases" with some half-baked VBA scripts.

  • @Far_Gem
    @Far_Gem 2 месяца назад

    7:25 Borrowing and references work just the same for stack-allocated data. For example, &i32 is a valid type, although i32 is a primitive and fully stack-allocated (implementing the Copy trait).

  • @AbdolaMike
    @AbdolaMike 2 месяца назад +1

    Loved your Python content but definitely excited to see more Rust content and design patterns!

  • @AbhishekYT-ht9ul
    @AbhishekYT-ht9ul 2 месяца назад

    Please please please... more rust !!!

  • @dirkschannel5817
    @dirkschannel5817 2 месяца назад +1

    Python and Rust, powerful buddies.

  • @internetrush
    @internetrush 2 месяца назад

    I also do not think of your channel as a Python channel, came up as a python dev but write JavaScript now and its equally valid!

  • @miguelangelsalazardetroya9476
    @miguelangelsalazardetroya9476 2 месяца назад +3

    This video is exactly 13:37 (leet). That was clearly on purpose

    • @ArjanCodes
      @ArjanCodes  2 месяца назад +2

      It's the optimal video length 😎

  • @personabrahamaudu
    @personabrahamaudu 2 месяца назад

    So does that mean when I assign a variable to another variable in python, it really just creates a pointer to that original variable under the hood if I don’t use something like copy.deepcopy()?

  • @maroben225
    @maroben225 2 месяца назад

    look man we are here to learn pattern and concepts and Rust has one of the best paradigm go at it we LOVE python we need it, and we Love most of all learning about paradigms and other languages, what would be amazing is if you try to dig in Go, Rust,Python and do some kind of comparing their concepts, I love python and Go they have alot of similarities even the author of Go confessed that they got inspored alot of python and that python is the closest language to Go

  • @DataPastor
    @DataPastor 2 месяца назад

    Great video as always. I only comment this because there are lots of pro-rust comments below, but I would prefer Mojo content instead. 🔥 There are enough Rust channels on RUclips, but high quality Python and esp. Cython, Zig or Mojo is scarce.

  • @deadlooop
    @deadlooop 2 месяца назад

    How did you make the terminal as a page?

  • @sinancetinkaya
    @sinancetinkaya 2 месяца назад

    What do you think about Zig?

  • @79SagiTaurus
    @79SagiTaurus 2 месяца назад

    More RUST content please, Arjan :)

  • @Bryen89
    @Bryen89 2 месяца назад

    More rust content!

  • @hubstrangers3450
    @hubstrangers3450 2 месяца назад

    Thank you....

  • @Singlton
    @Singlton 2 месяца назад

    please reply : data science and machine learning with rust , is it possible ?

  • @adriancruz2822
    @adriancruz2822 2 месяца назад

    Great video. However, please clear your console before you compile each time

  • @alexanderzikal7244
    @alexanderzikal7244 2 месяца назад

    Which GUI do You prefer in Rust?

  • @bocckoka
    @bocckoka 2 месяца назад

    Rust in practice manages memory with Arc

  • @JT-mr3db
    @JT-mr3db 2 месяца назад

    Ill take rust content with a python bias over typescript any day of the week.

  • @cherry-55
    @cherry-55 2 месяца назад

    I was thinking about Rust Foundation and was like wtf...

  • @greob
    @greob 2 месяца назад

    I love your videos, but may I suggest not doing so many cuts to show yourself in fullscreen? It’s very distracting, and surely it adds more work for you to edit. ;)

  • @alifakhar3687
    @alifakhar3687 2 месяца назад

    I got 60% of this video😂, but anyway, I'm nothing understanding about lifetime now🤦‍♂

  • @Aceptron
    @Aceptron 2 месяца назад +1

    TURN IT INTO A RUST BASED CHANNEL PLEASE

  • @alejandroioio6784
    @alejandroioio6784 2 месяца назад +1

    Yes, great, but why? This whole thing about rust just because C has "memory problems" so complicated is C or c++ that they had to invent a new language to avoid having to learn xd, I still don't see the great advantages of switching to Rust. What about Mojo 🔥btw.

    • @Michallote
      @Michallote 2 месяца назад

      Mojo does not seem very useful to be honest. Dictionaries in mojo are slower than in python.

    • @Heater-v1.0.0
      @Heater-v1.0.0 2 месяца назад +1

      Good question.
      Because we want a language that can provide the performance of C and C++. A language one can use anywhere one might use C and C++, for example writing operating systems., writing games, embedded systems, operating systems, compilers/interpreters for other languages, web services that need efficiency. In fact pretty much anything.
      Because we want a language that while as performant as C and C++ helps prevent all the problems that can happen with those languages, problems that cause random failures, crashes and security issues. Which Rust does if we look at the statistics and success stories published by the likes of Microsoft, Google and others.
      Because we want a languages that helps us to build well designed and maintainable code.
      "What about Mojo?". Well Rust was conceived a long time ago, I suspect before Mojo. If Mojo can do all the above then why not. I have no idea really but from what little I have seen of it I suspect it cannot.

    • @alejandroioio6784
      @alejandroioio6784 2 месяца назад

      ​@Michallote you can always implement your custom dictionary

    • @alejandroioio6784
      @alejandroioio6784 2 месяца назад

      ​@Heater-v1.0.0 yes but this doesn't answer my question c++ can do all of that in fact if you want success examples omg c++ has plenty of that with c++ you have a enormous code base an enormous community and 40 years of experience the world is basically writing in c or c++ at this point of the game doesn't make sense change to another language that "does the same" something that we can't be 100% because RUST is very new and all that for what just because is more "safety" come on that is madness and what means security any ways most of the attacks came from people to people c++ is the best choice for sure.

    • @Heater-v1.0.0
      @Heater-v1.0.0 2 месяца назад

      @@alejandroioio6784 All that you say is true. C and C++ have been used to create countless wonderful things over many years. Of course there is a huge community of users. I love C, been using it since 1984, because it's a small, simple, fast language that one man can write a compiler for in a reasonable amount of time. C++ not so much, been using it since 1998. It is huge and complex and ugly and seems to be getting worse with every new standards release.
      What I disagree with is the "just because of more safety come on that is madness". As you know C and C++ allow the programmer to make all kinds of silly mistakes in thousands of ways. That is why those languages have "coding guidelines" and "coding standards". huge documents with hundreds of pages and thousands of rules and do's and don'ts that one has to code against and review code against. That is a lot of tedious mental effort. Most of it is because of the lax way the languages allow programmers to make silly memory use mistakes. Not many can afford the time or have the stamina to do all that so we end up with code that is buggy and requires a great deal of testing and debugging, even after it has gone into production. And code that is prone to security issues.
      Enter Rust. Rust is far more rigorous about type and memory use errors. It checks so many things. It removes the need for most of those coding standards, it removes the need for all that review and debugging. Basically it automates away a lot of tedious drudgery. Programmers love to be lazy and automate as much as possible, how could they not love what Rust offers them?
      No sensible Rust head is suggesting rewriting everything in Rust, that is obviously a silly idea, lost of work for not much gain that will likely create more problems than it fixes. But for new projects or extending old projects Rust is very desirable for the reasons given. C and C++ are not going away any time soon but it's not a zero-sum game, Rust can thrive and work with other languages.