[Verse 1] Sometimes you need to change what's locked inside RefCell lets you modify when borrow rules collide Interior mutability, that's the special key To change immutable data safely as can be [Chorus] RefCell for single threads, borrow check at runtime Mutex when you're sharing between threads online Interior mutability breaks the normal way Lets you change the inside when the outside has to stay [Verse 2] Borrow mut and borrow, that's how RefCell works Runtime checks will panic if the borrowing rule gets hurt One mutable or many read-only at a time Cross that line and your program stops on a dime [Chorus] RefCell for single threads, borrow check at runtime Mutex when you're sharing between threads online Interior mutability breaks the normal way Lets you change the inside when the outside has to stay [Bridge] Mutex stands for mutual exclusion lock Only one thread gets in, the others have to block Arc and Mutex together make sharing thread-safe RefCell panics at runtime, Mutex makes threads wait [Verse 3] When you have shared ownership but need to mutate Interior mutability seals your data's fate Choose RefCell for single threads, simple and clean Mutex for concurrency, the safest you've seen [Chorus] RefCell for single threads, borrow check at runtime Mutex when you're sharing between threads online Interior mutability breaks the normal way Lets you change the inside when the outside has to stay [Outro] Interior mutability That's the Rust way to be free Change the inside safely
← Smart pointers (Box, Rc, Arc) | Threads and message passing →