Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Core Traits

The Sized trait and ?Sized

Reference for Rust Sized and ?Sized: statically sized types, dynamically sized types (str, slices, dyn Trait), implicit generic bounds, fat pointers and size_of_val.

Sized marks types whose size is known at compile time. Almost every type you write is Sized: integers, structs, enums, arrays, references and Box<T> itself. The compiler needs the size to put a value on the stack, pass it to a function or store it in a variable.

Types without a compile-time size are dynamically sized types (DSTs): str, slices [T] and trait objects dyn Trait. You can never hold one by value; you use them behind a pointer such as &str, Box<[T]> or &dyn Trait. Those pointers are "fat" and carry extra data (a length or a vtable pointer).

Rust
fn show<T: ?Sized + std::fmt::Debug>(value: &T) { println!("{value:?}"); }

Try it

Rust Sizes of sized types and fat pointers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Relaxing the implicit Sized bound

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
pub trait Sized {}          // a marker trait, implemented by the compiler

fn f<T>(x: T) {}            // means: T: Sized
fn g<T: ?Sized>(x: &T) {}   // opt out of the implicit bound
struct Wrapper<T: ?Sized> { value: Box<T> }
Part Description
T: Sized Implicit on every generic type parameter. You never write it.
T: ?Sized "May or may not be sized." The only ? bound in Rust.
Self: Sized An explicit bound, often placed on a trait method to keep the trait usable as dyn Trait.

Errors / panics / edge cases: E0277 "the size for values of type str cannot be known at compilation time" appears when you pass or return a DST by value, or use it for a plain generic parameter.

Usage notes

  • Every generic parameter is Sized by default. That is why fn f<T>(x: &T) rejects str and [T] unless you add ?Sized.
  • Traits are different: in a trait declaration Self is ?Sized by default, which is what allows dyn Trait and impls for str. A default method that takes self by value therefore needs where Self: Sized.
  • Where DSTs appear: str, [T], dyn Trait, and custom structs whose last field is a DST. Values live behind &, &mut, Box, Rc or Arc.
  • Fat pointers are two words: address plus length for str/[T], address plus vtable for dyn Trait.
  • Arrays are sized, slices are not. [i32; 3] is Sized and coerces to &[i32] by unsizing.
  • Local unsized values (let x: [i32] = ...) are not allowed on stable Rust, and parameters of unsized type are rejected.
  • size_of_val measures the real size of the pointee, including for DSTs; size_of::<T>() needs T: Sized.
  • Rule of thumb: write ?Sized on a generic only when your function takes &T, Box<T> or similar and genuinely supports str, slices or trait objects.