Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0512: cannot transmute between types of different sizes, or dependently-sized types
What it meansTransmute with two differently sized types was attempted.
What rustc prints
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types --> main.rs:4:23 | 4 | unsafe { takes_u8(::std::mem::transmute(0u16)); } | ^^^^^^^^^^^^^^^^^^^^^ | = note: source type: `u16` (16 bits) = note: target type: `u8` (8 bits)
Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
The compiler team's explanation
Transmute with two differently sized types was attempted.
Erroneous code example:
fn takes_u8(_: u8) {}
fn main() {
unsafe { takes_u8(::std::mem::transmute(0u16)); }
// error: cannot transmute between types of different sizes,
// or dependently-sized types
}
Please use types with same size or use the expected type directly. Example:
fn takes_u8(_: u8) {}
fn main() {
unsafe { takes_u8(::std::mem::transmute(0i8)); } // ok!
// or:
unsafe { takes_u8(0u8); } // ok!
}From rustc --explain E0512 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0308mismatched types
- E0277the trait bound is not satisfied
- E0599no method named X found in the current scope
- E0433failed to resolve
- E0446private type `Bar` in public interface
- E0449visibility qualifiers are not permitted here
- E0520`fly` specializes an item from a parent `impl`, but that item is not marked `default`
- E0525expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
Up next
Often hit nextE0308: mismatched typesA related error from the same family.Read it next →Did this answer your question?