Python is memory safe? Can’t you access/address memory with C bindings?

  • Traister101@lemmy.today
    link
    fedilink
    arrow-up
    9
    arrow-down
    3
    ·
    edit-2
    9 months ago

    What Rust provides is statically guaranteed memory safety. Some C++ types will prevent memory issues however the language itself is unsafe. Playing with raw pointers is just as valid as using std::unique_ptr. In Rust however you must sign a contact (using unsafe) in order to play with raw pointers. Unsafe is you the programmer promising that you followed the rules. This is like how C++ says it’s illegal to write UB and your program will break (and it’s your fault) but enforced through a special type of block

    • Dark Arc@social.packetloss.gg
      link
      fedilink
      English
      arrow-up
      7
      ·
      9 months ago

      In Rust however you must sign a contact (using unsafe) in order to play with raw pointers. Unsafe is you the programmer promising that you followed the rules. This is like how C++ says it’s illegal to write UB and your program will break (and it’s your fault) but enforced through a special type of block

      Which is what I said, this is about the default.

      My issue is not that I don’t understand Rust provides static guarantees. My issue is that you raised a comparison between unsafe Rust and C++ code. In that comparison, you’re basically saying “writing an entire program in a rust unsafe block would be better than writing an entire program in C++” and I think that is very wrong.

      Rust unsafe is not better than normal C++ while following best practices for maintaining memory safety.

      • arendjr@programming.dev
        link
        fedilink
        arrow-up
        4
        ·
        9 months ago

        I wouldn’t be so sure myself. Even unsafe Rust still uses the borrow checker, for instance. And you still get stricter checks around overflows and such as well. What unsafe does is that it unlocks the ability to use raw pointers and call other unsafe functions (among a few other things), but importantly it doesn’t disable the safety features that Rust has built-in. While unsafe Rust does indeed have some gotchas on its own, I think in general you’re still better off even with unsafe Rust than with C++.