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?

3
  • 2
    Not true, no automatic multithreading. You still care because rust's way prevents use-after-free, double-free and other memory-related bugs. Commented Jan 10, 2022 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. Commented Jan 10, 2022 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. Commented Jan 10, 2022 at 18:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.