• Rednax@lemmy.world
    link
    fedilink
    arrow-up
    5
    arrow-down
    1
    ·
    1 day ago

    But how does the Rust compiler do that? What does it actually check? Could I write a compiler in C that does this check on a piece of Rust code?

    C is so simplictic, that if I can write a piece of functionality in C, I must understand its inner workings fully. Not just how to use the feature, but how the feature works under the hood.

    It is often pointless to actually implement the feature in C, since the feature already has a good implementation (see the Rust compiler for the memory safety). But understanding these features, and being able to mentally think about what it takes in C to implement them, is still helpfull for gaining an understanding of the feature.

    • jj4211@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      19 hours ago

      Could I write a compiler in C that does this check on a piece of Rust code?

      Well yes, but that code has to be written in Rust. The human has to follow rules to give the compiler a chance to check things.

      C is so simplictic, that if I can write a piece of functionality in C, I must understand its inner workings fully. Not just how to use the feature, but how the feature works under the hood.

      I don’t think that’s particularly more true of C than Rust or even Golang. In C you are frequently making function calls anyway for the real fun stuff. If you ever compile a “simplistic” chunk of C code that you think is obvious how it would compile to assembly and you open up the assembly output, you are likely to be very surprised with what the compiler chose to do. I’ve seen some professional C developers that never actually had a reason to fully understand how the stack works, since C abstracts that away and the implications of the stack don’t matter until you exceed some limitations.