Mem::transmute implementation source

Where can I find the source code for the implementation for std::mem::transmute. The src link in the documentation seems only to have the declaration of the function. I looked in the source from rust.git, but could not find it.


It’s a compiler instrinsic, i.e. compiler magic.

I am probably not ready to cast spells yet; but if I get curious, where can I learn about that?

1 Like

Any thing inside of the std::intrinsics module is certified compile magic. A vast majority of them are also perma-unstable and all are unsafe.

Also if you see

pub unsafe extern "rust-intrinsic" fn ...

before the function, then it is an intrinsic.

Basically these are functions whose body is generated by the compiler itself. Usually because it can’t be done by user code (with std::mem::transmute the compile time size check can’t be done in user code) or because of performance reasons (like std::intrinsic::copy, which could be implemented via user code, but would be terrible for performance if it was).

See rustc’s src/libcore/intrinsics.

If you really want to go down the rabbit hole I think you’d start with librustc_codegen_ssa/mir/ (you may first need to understand MIR to know what’s going on).

It contains the code to generate LLVM IR so it’s still not where the actual magic happens. For that you’d need to understand LLVM.


AFAICT the codegen side is basically just plain memory loads or pointer casts, depending on context. The validation part of transmute is done here.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.