4 ms·
Looking at Rust from the outside, I see the borrow checker as something I might slam into all the time, but if it’s all static, then it’s strictness I can get u
by chuckadams 2y ago
Looking at Rust from the outside, I see the borrow checker as something I might slam into all the time, but if it’s all static, then it’s strictness I can get used to; “if it compiles, it works,” right? But now I’m reading that there’s a dynamic borrow checker too, and I’m left wondering: does rust really crash at runtime if two scopes reference the same field at the same time? That sounds like the kind of infuriating footgun as NPEs in a non null-safe language. Does it really work like that?
- steveklabnik 2y agoThe borrow checker is fully static. There are types in the standard library that, if you use them, do runtime checks. If you run afoul of those checks, then yes, you’ll get a runtime failure. (The primary example being RefCell<T> linked in the article linked there, yes.) These types are not super common in Rust code generally, but they do exist and are useful at times.
- desdenova 2y agoThe "dynamic borrow checker" is called a RefCell, and it's a footgun you have to explicitly point at your foot and pull the trigger, not something you'd randomly stumble upon. It's something you'd use very situationally, after you really considered all your options and found out you couldn't do it by writing normal code instead, and this is a very rare situation. Think of them like Python's metaclasses.