0

I am reading about Rust's ownership and it turns out that Rust's ownership, reference, and borrow features focus on preventing data race via some restrictions in reference mutability.

I wonder why Rust forces us to obey these restrictions? Is that true that Rust will execute my code automatically in multithreaded mode? Otherwise, why do I have to care about the restrictions even if my code only executes in single-threaded mode by default?

  • 2
    Not true, no automatic multithreading. You still care because rust's way prevents use-after-free, double-free and other memory-related bugs. – Sergio Tulentsev Jan 10 '22 at 17:51
  • 1
    "Multithreaded" is not a "mode" that you can just switch on. The threads in a multi-threaded application must _communicate_ with each other. They _coordinate_ their access to _shared data._ They _cooperate_ to solve a problem. You have to write explicit code to make those things happen. There may be some higher-level models of concurrency (streams, actors, ...) where you have a framework that makes some of that communication/coordination/cooperation more automatic, but when you're working with naked threads, you have to provide all of it for yourself. – Solomon Slow Jan 10 '22 at 17:51
  • 2
    Short answer: the ownership and borrowing mechanisms are in the language for more than just about multithreading. Reading the answers will explain why and show examples of single threaded code that could go very wrong if the compiler did not stop it. – E_net4 Jan 10 '22 at 18:01

0 Answers0