Rust
E0607 Thin-to-Fat Pointer Cast Fix
A wide pointer is an address plus a piece of metadata - for a slice, its length. A thin pointer has only the address, so no cast can promote one into the other: the length has to come from somewhere, and `as` cannot invent it. This guide works the three shapes that produce E0607, shows where the metadata legitimately comes from in each repair, and separates the neighboring casts that compile from the ones that fail under different codes.
Written November 30, 2026 · Tested with Rust 1.99.1 · 14 min read
- The rule underneath: a cast cannot invent metadata
- How to read the diagnostic
- The first failure: a thin pointer cast to a slice pointer
- The same error on a mutable pointer and on str
Written November 30, 2026
Tested with Rust 1.99.1
What this guide is
Error E0607 is a cast from a thin pointer to a wide pointer. The error codes index states it in one line - "A cast between a thin and a wide pointer was attempted" - and its erroneous example is the first failure in this guide: a null *const u8 cast to *const [u8]. Its explanation defines the two kinds: thin pointers "are purely a reference to a memory address", while wide pointers reference dynamically sized types and "can only exist behind some kind of pointer that contains additional information" - for slices, that additional information is their size. Its repair instruction is blunt: "don't try to cast directly between thin and wide pointers."
The practical payoff is knowing where the missing information comes from instead. A wide slice pointer needs a length, and there are only two honest sources for it in the programs below: you state it, because you know how many elements the address actually covers, and pass it to a constructor such as core::ptr::slice_from_raw_parts; or you take the wide pointer from a value that already carries it, such as an array or a slice, so the metadata arrives with the value. Every repair in this guide is one of those two moves. This guide is about raw-pointer casts specifically: the neighboring guides on E0605 and E0606 cover the non-primitive and incompatible casts, and this one links them only where the same instinct lands under a different code.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: a cast cannot invent metadata
The type cast expressions section of the Rust Reference treats pointer casts as rearrangements of what is already there. Its discard-metadata rule covers the reverse direction of this error: if the source pointee is unsized and the target pointee is sized, "the cast discards all metadata that completes the wide pointer" and produces a thin pointer from the data part. Discarding is something a cast can do. The same section's rules for two unsized pointees require the metadata to be compatible and preserved. Nowhere does a cast rule create metadata: going from a sized pointee to an unsized one would require a length, or a vtable for a specific unsized target, that the thin pointer simply does not contain.
That asymmetry is the whole error. Wide to thin compiles, because dropping the length is allowed. Thin to thin compiles, because nothing is added or dropped. Thin to wide, where the target pointee is a slice or str, fails with E0607, because the length would have to appear from nowhere. One direction inside this guide does compile and surprises people: a thin pointer to a sized pointee cast to a trait-object pointer. There the compiler can build the vtable from the known sized type and the trait it implements, so the metadata has a source after all - that program is worked below, and it is not a counterexample to the rule, it is the rule applied with the metadata accounted for.
How to read the diagnostic
The header names both pointer types in full: "cannot cast thin pointer *const u8 to wide pointer *const [u8]". The word "thin" attaches to the source type and "wide" to the target type, so the header alone tells you which side is missing information. The single label underlines the whole cast expression, and there is no help block and no note in any of the three failing programs below - E0607 arrives alone, with the aborting line and the pointer to rustc --explain E0607. The absence of a suggested repair is itself the signal: the compiler cannot propose a length for you, because choosing one is a statement about memory only your code can make.
Compare the neighboring usize form in the traps section: there the E0606 diagnostic does carry an explanation - "creating a *const [u8] requires both an address and a length" - which is the same missing-metadata fact stated from the integer side. When a cast to a wide pointer fails, read whichever header you get as a question: where is the length supposed to come from?
The first failure: a thin pointer cast to a slice pointer
This is the error index's own example, as a complete program - a null thin pointer cast to a slice pointer:
fn main() { let v = core::ptr::null::<u8>(); let _p = v as *const [u8]; println!("never"); }
error[E0607]: cannot cast thin pointer `*const u8` to wide pointer `*const [u8]`
--> q1.rs:1:55
|
1 | fn main() { let v = core::ptr::null::<u8>(); let _p = v as *const [u8]; println!("never"); }
| ^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0607`.
The null address is not the problem - a null thin pointer is a legal value, and the same pointer cast to another thin pointer type compiles in a program below. The problem is the target: *const [u8] is two words of information, an address and a length, and the source supplies only the first.
The same error on a mutable pointer and on str
Mutability changes nothing. A mutable thin pointer taken from a real variable fails the same way when cast to a mutable slice pointer:
fn main() { let mut x: u8 = 7; let p: *mut u8 = &mut x; let _s = p as *mut [u8]; println!("never"); }
error[E0607]: cannot cast thin pointer `*mut u8` to wide pointer `*mut [u8]`
--> q7.rs:1:66
|
1 | fn main() { let mut x: u8 = 7; let p: *mut u8 = &mut x; let _s = p as *mut [u8]; println!("never"); }
| ^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0607`.
Here the temptation is strongest, because there is a real u8 behind the pointer and it is easy to feel the slice length should be one. The compiler does not infer it: x being a single value is a fact about this variable, not a fact the pointer type carries, and the cast is judged on the types alone. A str target fails identically - its wide pointer also carries a length, this time in bytes:
fn main() { let v = core::ptr::null::<u8>(); let _p = v as *const str; println!("never"); }
error[E0607]: cannot cast thin pointer `*const u8` to wide pointer `*const str`
--> q5.rs:1:55
|
1 | fn main() { let v = core::ptr::null::<u8>(); let _p = v as *const str; println!("never"); }
| ^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0607`.
Three targets, one shape: whenever the target pointee is dynamically sized and its metadata is a length the source does not have, the cast is E0607, with the target type named in the header.
The traps: neighbors that are not E0607
as rejects those pairs earlier, for different reasons.A shared reference to a single value cast to a slice reference is E0605, the non-primitive cast:
fn main() { let x: u8 = 7; let r = &x; let _s = r as &[u8]; println!("never"); }
error[E0605]: non-primitive cast: `&u8` as `&[u8]`
--> q2.rs:1:49
|
1 | fn main() { let x: u8 = 7; let r = &x; let _s = r as &[u8]; println!("never"); }
| ^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0605`.
The same reference cast to a raw wide pointer is E0606 instead - the pair is outside the cast table entirely:
fn main() { let x: u8 = 7; let _p = &x as *const [u8]; println!("never"); }
error[E0606]: casting `&u8` as `*const [u8]` is invalid
--> q9.rs:1:37
|
1 | fn main() { let x: u8 = 7; let _p = &x as *const [u8]; println!("never"); }
| ^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0606`.
An integer cast to a wide pointer is also E0606, and its label states the missing-metadata fact outright:
fn main() { let n: usize = 0; let _p = n as *const [u8]; println!("never"); }
error[E0606]: cannot cast `usize` to a pointer that is wide
--> q6.rs:1:45
|
1 | fn main() { let n: usize = 0; let _p = n as *const [u8]; println!("never"); }
| ^^^^^^^^^^^ creating a `*const [u8]` requires both an address and a length
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0606`.
The reverse trip through an integer fails too: a wide pointer cast directly to usize is E0606, with a help that points at the legal two-step route - through a thin pointer first:
fn main() { let a: [u8; 3] = [1, 2, 3]; let fat: *const [u8] = &a; println!("{}", fat as usize); }
error[E0606]: casting `*const [u8]` as `usize` is invalid
--> n3.rs:1:83
|
1 | fn main() { let a: [u8; 3] = [1, 2, 3]; let fat: *const [u8] = &a; println!("{}", fat as usize); }
| ^^^^^^^^^^^^
|
= help: cast through a thin pointer first
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0606`.
The line between the codes is the starting type: a thin raw pointer cast to a wide raw pointer whose metadata is a length - a slice or str target - is E0607; a reference or an integer aimed at that same wide pointer is E0606, and a reference aimed at a reference is E0605.
The casts that compile: the other directions
Three nearby casts compile and run, and each one confirms the metadata accounting. Wide to thin drops the length, which the Reference's discard-metadata rule permits - here it recovers the first element of the array:
fn main() { let a: [u8; 3] = [1, 2, 3]; let fat: *const [u8] = &a; let thin = fat as *const u8; unsafe { println!("{}", *thin); } }
1
The output is 1, the element at the address the wide pointer's data part names. Thin to thin changes only the pointee type and adds nothing:
fn main() { let v = core::ptr::null::<u8>(); let _p = v as *const u16; println!("thin-thin ok"); }
thin-thin ok
And a thin pointer to a sized pointee cast to a trait-object pointer compiles, because the vtable is built from the known pointee type - the metadata has a source:
trait Show { fn val(&self) -> i32; }
struct S;
impl Show for S { fn val(&self) -> i32 { 4 } }
fn main() { let p: *const S = &S; let o = p as *const dyn Show; println!("{}", unsafe { (*o).val() }); }
4
The output is 4, the S implementation's return, reached through the trait object the cast built. Do not generalize this program past what it shows: it is the sized-pointee-to-trait-object direction that compiles in this guide's probed set, while the slice and str targets - whose metadata is a length, not a vtable - are the ones that fail.
Fixes in reach order
When E0607 appears, supply the metadata instead of casting for it, in this order. First, if you know how many elements the address covers, state that length and build the wide pointer with core::ptr::slice_from_raw_parts, whose documentation describes it as forming "a raw slice from a pointer and a length". Applied to an array's data pointer and its real length:
fn main() { let a: [u8; 3] = [1, 2, 3]; let thin: *const u8 = a.as_ptr(); let fat = core::ptr::slice_from_raw_parts(thin, a.len()); println!("{} {}", fat.len(), unsafe { (*fat)[1] }); }
3 2
The output is 3 2: the length that was supplied, then the second element read back through the wide pointer. The length argument is a claim about memory - in this program it is the array's own length, taken from the array, not a guess.
Second, if a value that already carries the metadata is in scope, take the wide pointer from that value and skip the manual construction. An array reference coerces to a slice pointer with its length attached:
fn main() { let a: [u8; 3] = [1, 2, 3]; let fat: *const [u8] = &a; println!("{} {}", fat.len(), unsafe { (*fat)[1] }); }
3 2
The output is again 3 2, reached without naming a length at all - the array knew it. Prefer this form whenever the source value is still available; reach for slice_from_raw_parts when all you genuinely have is an address and a length from elsewhere.
Third, for a str target, build the byte slice first and let a checked conversion produce the string slice - a str needs valid UTF-8 as well as a length, and no cast supplies either:
fn main() { let bytes: [u8; 2] = [104, 105]; let fat = core::ptr::slice_from_raw_parts(bytes.as_ptr(), bytes.len()); let s = unsafe { core::str::from_utf8(&*fat).unwrap() }; println!("{}", s); }
hi
The output is hi, the two bytes read as text after from_utf8 accepted them. The reference form of the trait-object repair follows the same principle - take the wide pointer from a value, rather than casting a bare address upward:
trait Show { fn val(&self) -> i32; }
struct S;
impl Show for S { fn val(&self) -> i32 { 4 } }
fn main() { let s = S; let o: &dyn Show = &s; println!("{}", o.val()); }
4
The output is 4. Last, if the diagnostic is not E0607 at all, switch playbooks: a reference source is E0605 or E0606 territory (see the neighboring guides), an integer source aimed at a wide pointer is E0606, and a wide pointer aimed at an integer goes through a thin pointer first, exactly as its help line says.
Decision summary
E0607, "cannot cast thin pointer to wide pointer", is one shape in this guide's probed set: a thin raw pointer - shared or mutable - cast with as to a raw pointer whose pointee is a slice or str, types whose wide form carries a length the thin pointer does not have. The diagnostic names both pointer types, underlines the whole cast, and offers no help, because the missing length is a fact only the code can supply. The repairs supply it: state the length in core::ptr::slice_from_raw_parts, or take the wide pointer from an array, a slice, or another value that already carries the metadata - and for str, pass the bytes through a checked UTF-8 conversion as well. The reverse and sideways casts behave differently for the same reason: wide to thin compiles because discarding metadata is allowed, thin to thin compiles because nothing changes hands, and a thin pointer to a sized pointee cast to a trait object compiles because the vtable is built from the known type. A reference or an integer aimed at the same wide target is not E0607 - it is E0605 or E0606, depending on the pair.
Checklist
- A thin-to-wide cast is never repaired by another cast - the length is supplied, by a constructor or by a value that carries it.
- A length passed to
slice_from_raw_partscomes from the actual source - an array's or buffer's own length - never from the size the caller wishes the memory had. - A wide pointer is taken from an array or slice value in preference to manual construction whenever that value is still in scope.
- A
stris built through a checked conversion such ascore::str::from_utf8, never by casting a byte pointer to astrpointer. - Wide-to-thin and thin-to-thin casts are left as casts - they compile, and rewriting them adds nothing.
- E0607 is told apart from its neighbors by the source type, for slice and
strtargets: thin raw pointer is E0607, reference is E0605 or E0606, integer is E0606.
Related guides on this site
- Cast to Bool E0054 Fix
- E0604 Only u8 Cast as Char Fix
- E0606 Incompatible Cast Fix
- E0605 Invalid Cast Fix
Sources
- Rust error codes index, E0607 - a cast between a thin and a wide pointer was attempted, thin pointers as purely a reference to a memory address, wide pointers as pointers to dynamically sized types carrying additional information such as a slice's size, and the instruction not to cast directly between thin and wide pointers.
- The Rust Reference, Type cast expressions - the pointer-cast rules, including discarding all metadata when casting from an unsized pointee to a sized one, and preserving compatible metadata between two unsized pointees.
- core::ptr::slice_from_raw_parts - forms a raw slice from a pointer and a length, the constructor the first repair uses to supply the length a thin pointer lacks.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.