Feel like everyone’s been telling me it’s the best thing since sliced bread. I’m just a hobbyist with like a single big project I’m maintaining but I’m starting to hate the code and idk maybe I should rewrite it in rust. Idk

  • WhyJiffie@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    2
    ·
    7 days ago

    e.g. you can’t have global mutable variables because that can cause race conditions on multi-threading applications.

    well, you can, can’t you? there are several different synchronization primitives in the standard library that lets you do that if you really want it

    • Nibodhika@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      7 days ago

      Yes, you can have a OnceLock<RWLock<T>> which is something that will get initialized at some point, and then you can acquire a lock on it for either read or write. To use it you need to check and handle both conditions, so it’s really secure.

      However if you’re writing a single threaded application a global let mut should also be safe, but Rust doesn’t know if your application is single-threaded, or even if it is, it might get imported by others that isn’t. So you’re forced to consider the safety of things that might not even be related to what you’re doing. Which was my point, if you’re writing a simple CLI single threaded app, the compiler blocking you from having a global variable seems pedantic and unnecessary, because it’s forcing you to deal with the edge case of your app becoming multi-threaded in the future.

      Don’t get me wrong, this is part of what makes Rust great, but it can be EXTREMELY pedantic.