From BeepingComputer.

  • lloram239@feddit.de
    link
    fedilink
    arrow-up
    11
    ·
    edit-2
    1 year ago

    C has no memory protection. If you access to the 10th element of a 5 element array, you get to access whatever is in memory there, even if it has nothing to do with that array. Furthermore this doesn't just allow access to data you shouldn't be able to access, but also the execution of arbitrary code, as memory doesn't make a (big) difference between data and code.

    C++ provides a few classes to make it easier to avoid those issues, but still allows all of them.

    Ruby/Python/Java/… provide memory safety and will throw an exception, but they manually check it at runtime, which makes them slow.

    Rust on the other side tries to proof as much as it can at compile time. This makes it fast, but also requires some relearning, as it doesn't allow pointers without clearly defined ownership (e.g. the classic case of keeping a pointer to the parent element in a tree structure isn't allowed in Rust).

    Adding the safeties of Rust into C would be impossible, as C allows far to much freedom to reliably figure out if a given piece of code is safe (halting problem and all that). Rust purposefully throws that freedom away to make safe code possible.