This Is How Rust Stops Memory Leaks

157,635
0
Published 2022-02-13
LOW LEVEL RUSTACEANS! Welcome back! In today's video we discuss Rust Ownership. Rust Ownership is a concept that prevents variables from being lost in runtime, ultimately preventing memory leaks. By assigning ownership and allowing for the borrowing of variable access, Rust allows for consistent allocation and freeing of memory.

LINKS:
LLL Merch: linktr.ee/lowlevellearning
Embedded Rust: docs.rust-embedded.org/book/

SOCIALS:

Follow me on Twitter: twitter.com/lowlevellearni1
Follow me on Twitch: twitch.tv/lowlevellearning
Join me on Discord!: discord.gg/gZhRXDdBYY

All Comments (21)
  • @aqdasak
    5:12 can be referenced multiple times if the variable is not referenced as mutable.
  • @notruff
    Just a small nitpick: the "Good Rust" code at 04:00 doesn't compile since you'll need to pass `&mut s` instead of only `&s`. But otherwise it's good!
  • @cysia3683
    Rust does NOT prevent memory leaks. You can introduce them accidentally via cyclic references when using Rc or Arc. There is also a dedicated function in the standard library, see std::mem::forget
  • Nitpick: What you are calling static memory allocations (stack allocation) is actually automatic memory allocation. Static allocation is for variables whose lifetime is the lifetime of the program.
  • Straight and to the point! Small nitpick: you can assign to the same name since the old name is no longer valid. No need to append numbers or invent new monikers.
  • @oconnor663
    I think there's a little bit of conflation here between "use-after-free" bugs and "memory leak" bugs. The former lead to full-blown undefined behavior, which is a common problem in C and C++, and the way Rust prevents them with ownership, borrowing, and lifetimes is truly new among mainstream languages. The latter lead to gradual memory exhaustion, as you described in the video, but the way Rust prevents them isn't new. It's actually almost identical to what (modern) C++ does. In both Rust and C++, heap memory leaks are prevented by making sure every allocation is owned by some object(s) on the stack(s) that will eventually run a destructor to free that memory. Rust's Vec/Box/Arc are very close mirrors of C++'s vector/unique_ptr/shared_ptr.
  • @gotbread2
    0:10 "in a couple of edge cases". Well since there is no "free" anywhere, these couple of edge cases are 100%
  • @xelaxander
    Thanks for the video. Coming from higher level Programming languages, the C and Rust comparisons are quite interesting. With the knowledge of C's issues, many design choices in Rust make a lot more sense. I'd love to see a video about the inner workings of some Rust stdlib features, like Vec, Rc, Arc etc.
  • @KadeDev
    all I got from this video was that rust forces you to use good habbits.
  • @Mike-123
    Thanks for posting this one. I'm enjoying as your stepping into Rust for the embedded world. It's something that I've been interested in digging more into, but it's been a struggle at times.
  • @jomy10-games
    About the Double Borrow: you can borrow as many time as want, but you can only borrow mutable once
  • Hey man, I really appreciate these videos. One thing that I think could make them even more helpful to beginners (like me, at least with Rust) is to either highlight or animate specific sections of text when referencing jargon-heavy syntax explanations. If you highlighted each part of the syntax when describing what was going on, it would allow the video to also help people learn the language itself rather than just its features. Lmk what you think :)
  • @dorktales254
    Most of the code examples in this video won’t actually compile. The very first example uses a never return type yet it returns just that it returns no data. Also there is an example where you pass an immutable reference to a function that expects a mutable reference
  • @MrSmitheroons
    Thanks for explaining that implicit "drop()" function. That explains a lot about the limits on where you can pass variables/data around. It makes sense in hindsight, but I would probably not have waded into the docs to find this. As rather much of a noob to low-level languages and under-the-hood data management concepts.
  • @1Maklak
    This makes me appreciate automatic pointers from CPP. They use reference counting and the destructor frees memory when going out of scope.
  • @DrewryPope
    i read a whole bunch of books on it and written a thousand or 2 lines, but something clicked finally when you described in terms of double free so thanks
  • @theana5550
    I would personally also talk about the box type, because that's pretty important.
  • @xavhow
    Great explaination, really helps to clarify the concept of ownership and borrowing...
  • Variables can also be cloned if they’re of a type that implements the “Clone” trait.
  • The code presented at 00:05 is so bad that the title should be "This is how Rust Stops People Who Dont Know C from using C" (I'm not even sure it works in normal cases, let alone corner cases)