3 ms·
What I never see discussed is how this borrowing system works in the presence of global state. Can't you have two global variables that reference the same value
by norswap 12y ago
What I never see discussed is how this borrowing system works in the presence of global state. Can't you have two global variables that reference the same value? How do you then go about building indexes and things like that, is there a form of weak reference?
I don't know Rust, so the question may be non-sensical, still, an explanation would be much appreciated :)
- kzrdude 12y ago* You can have thread-local storage, which is a form of "global" state for a whole task/thread. * You can use Arc (Atomically reference counted smart pointer), to share immutable data with all your tasks, without needing any locking. You may generate this data, and either hand tasks an Arc pointer at their creation, or send it out using channels. * You can use RWLock or Mutex to share owned data with limited/locked mutable access between several tasks. These types are smart wrappers that only allow access to their protected value while properly locked. Oh, and fyi the borrow checker is strictly local to a function or method. Other features of the Rust language, like the type kinds Send and Sync, are involved in checking which values are safe to share between tasks. Normal rust references (&T) only point across tasks if you get them through one of the primitives Arc, Mutex, RWLock or similar.