E0161 Cannot Move Unsized Value Fix
Written December 3, 2026 · Tested with Rust 1.99.1 · 17 min read
- The rule underneath: a move needs a size
- How to read the diagnostic
- The first failure: the index example
- The same error, reached through a reference or a shared pointer
Written December 3, 2026
Tested with Rust 1.99.1
What this guide is
Error E0161 means a value was moved whose size is not known at compile time - in the index's words, a value was moved whose size was not known at compile time. The error codes index example calls a method that takes self by value on a Box<dyn Bar> trait object, and its advice is to hide the value behind a reference, &x or &mut x, because a reference has a fixed size.
In the shapes probed in this guide, the index example verbatim drew exactly one E0161 and no other code - this premise is clean, with no ride-along to separate. The count follows the by-value trait-object calls, in the probed shape: two such calls in one main drew exactly two E0161 reports, one per call. Whether a second code joins the report depends on how the trait object is reached, in the probed shapes: the same call through a shared reference, &dyn Bar, and through an Rc<dyn Bar> each drew one E0161 and one E0507 on the same call, while the call through an owned Box drew E0161 alone. Most nearby unsized moves are not this error at all, in the probed shapes: moving out of a dereferenced trait object, a dereferenced string, or a dereferenced slice each drew E0277 alone, a bare dyn parameter drew E0277 with an E0308 alongside, a bare dyn return type drew E0746 with an E0277, and a method that carries a Sized bound drew an unnumbered report that the method cannot be invoked on a trait object. The ban is on moving the unsized value, not on using it: moving the box itself, and calling the same by-value method on a concrete value, both compiled and ran in the probed shapes. This guide shows the E0161 shapes, the neighboring shapes, the boundary, and the repairs.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts, the file names are the bare names shown on the command line.
The rule underneath: a move needs a size
Moving a value means copying its bytes to a new place, and the compiler can only lay that out when it knows how many bytes the value has. A trait object, dyn Bar, names an interface, not a layout: the value behind it can be any type that implements the trait, of any size, so its size is not known at compile time. A method that takes self by value asks for exactly that move, which is why the call is the place the error lands.
That is why every repair changes the receiver, not the call site logic. A reference has a fixed size, so a method taking &self or &mut self never asks for the unsized value itself. And two pointer receivers keep the by-value shape while still naming a fixed-size handle: self: Box<Self> and self: Rc<Self> hand the method a sized pointer that owns or shares the value, so the move that happens is the pointer's move. The trait object itself is never copied in any of the repairs.
How to read the diagnostic
The E0161 header is fixed wording: "cannot move a value of type dyn Bar", naming the trait. The report carries one span, the receiver expression of the call, underlined and labeled "the size of dyn Bar cannot be statically determined". There is no suggested fix in the report itself; the reference advice lives in the index entry.
The count follows the by-value trait-object calls, in the probed shape - one report per call that tries to move a trait object by value. Two calls in one main drew two reports, ending with "aborting due to 2 previous errors":
error[E0161]: cannot move a value of type `dyn Bar`
--> fail_two_moves.rs:12:5
|
12 | a.f();
| ^ the size of `dyn Bar` cannot be statically determined
error[E0161]: cannot move a value of type `dyn Bar`
--> fail_two_moves.rs:13:5
|
13 | b.f();
| ^ the size of `dyn Bar` cannot be statically determined
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0161`.
Fix every by-value trait-object call in the output before recompiling; a second one is already reported, not hidden behind the first.
The first failure: the index example
The index's erroneous example, verbatim - the method takes self by value, and the receiver is a trait object:
trait Bar {
fn f(self);
}
impl Bar for i32 {
fn f(self) {}
}
fn main() {
let b: Box<dyn Bar> = Box::new(0i32);
b.f();
// error: cannot move a value of type dyn Bar: the size of dyn Bar cannot
// be statically determined
}
error[E0161]: cannot move a value of type `dyn Bar`
--> fail_index.rs:11:5
|
11 | b.f();
| ^ the size of `dyn Bar` cannot be statically determined
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0161`.
Exactly one report, and it is the whole story: no second code rides along in this example. The separation checks confirm the by-value call on the trait object is the only defect. Moving the box itself - assigning b to a new binding and calling a reference method through it - compiled and ran, printing 8, in the file sep_move_box_itself.rs: the box is a sized pointer, so moving it is ordinary. And the method is not the problem either: calling the same by-value method on a concrete i32 value compiled and ran in the probed shape, shown in the boundary section below.
The same error, reached through a reference or a shared pointer
How the trait object is reached decides whether a second code joins the report. Through an owned box, the call drew E0161 alone, as the first failure shows. Through a shared reference, the same call drew E0161 and E0507 together, on the same span, in the probed shape:
error[E0161]: cannot move a value of type `dyn Bar`
--> fail_borrowed_call.rs:12:5
|
12 | r.f();
| ^ the size of `dyn Bar` cannot be statically determined
error[E0507]: cannot move out of `*r` which is behind a shared reference
--> fail_borrowed_call.rs:12:5
|
12 | r.f();
| ^ --- `*r` moved due to this method call
| |
| move occurs because `*r` has type `dyn Bar`, which does not implement the `Copy` trait
|
note: `Bar::f` takes ownership of the receiver `self`, which moves `*r`
--> fail_borrowed_call.rs:2:10
|
2 | fn f(self);
| ^^^^
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0161, E0507.
For more information about an error, try `rustc --explain E0161`.
The E0507 half is the borrow half of the same fact: the method wants to move the value out, and the value sits behind a shared reference, so it cannot be moved out of there either. Through an Rc<dyn Bar> the pairing repeats, in the probed shape - one E0161 and one E0507 ("cannot move out of an Rc") on the one call, in the file fail_rc_dyn_call.rs. Read the pair as one defect with two statements of it, not as two defects: changing the receiver clears both reports at once, as the repairs section shows.
One masking shape belongs here too. A program that carried the index call and an unresolved name in the same main reported only the unresolved name - exactly one E0425 and no E0161, in the file ride_unresolved.rs. The name error is reported first and the move error never surfaces in that shape; fix the name, recompile, and the E0161 appears.
The shapes that are not this error
Reaching for the unsized value directly, instead of through a by-value method call, is a different failure. Moving out of a dereferenced trait object - let x = *b; where b is a Box<dyn Bar> - drew exactly one E0277 and no E0161 in the probed shape:
error[E0277]: the size for values of type `dyn Bar` cannot be known at compilation time
--> fail_deref_move.rs:11:9
|
11 | let x = *b;
| ^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Bar`
= note: all local variables must have a statically known size
help: references are always `Sized`, even if they point to unsized data; consider not dereferencing the expression
|
11 - let x = *b;
11 + let x = b;
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.
The same dereference move on the other unsized types lands in the same place, in the probed shapes: let s: str = *b; on a Box<str> drew exactly one E0277 in the file fail_str_deref_move.rs, and let a: [i32] = *b; on a Box<[i32]> drew exactly one E0277 in the file fail_slice_deref_move.rs. A match that binds the dereferenced trait object drew exactly one E0277 as well, in the file fail_match_deref.rs - the binding is a local, and locals must have a known size. Read which code you have: E0161 is the by-value method call on a trait object; a direct move of an unsized value into a binding is E0277.
A bare dyn type in a signature is policed before any move is attempted, in the probed shapes. A parameter written as x: dyn Bar drew E0277 for the parameter and for the call argument, with an E0308 for the argument's type alongside, in the file fail_bare_dyn_param.rs. A return type written as -> dyn Bar drew exactly one E0746 - "return type cannot be a trait object without pointer indirection" - with an E0277 at the call site, in the file fail_return_dyn.rs. And a generic function that takes a ?Sized parameter by value drew E0277 at the parameter and at the call, in the file fail_generic_unsized_move.rs. None of these shapes drew E0161: the signature itself is rejected, so no move is ever judged.
A Sized bound on the method removes the call from the trait object entirely, under no code at all. Where the trait declares fn f(self) where Self: Sized;, calling it on a Box<dyn Bar> drew one unnumbered report in the probed shape:
error: the `f` method cannot be invoked on a trait object
--> fail_sized_bound_method.rs:11:7
|
2 | fn f(self) where Self: Sized;
| ----- this has a `Sized` requirement
...
11 | b.f();
| ^
error: aborting due to 1 previous error
That shape is a deliberate opt-out of trait-object calls, not a broken call: the method stays available on concrete types, and the report says plainly that it cannot be invoked on a trait object. Do not repair it as E0161 - if the method must run through a trait object, the bound has to go and one of the receiver repairs below takes its place.
The boundary: the pointer moves, the concrete value moves, references just work
The line is whether the thing being moved has a known size. On the other side of it, the same trait and the same method shape are ordinary code. The by-value method called on a concrete value - an i32 that implements the trait directly - compiled and ran in the probed shape, printing 10:
trait Bar {
fn f(self) -> i32;
}
impl Bar for i32 {
fn f(self) -> i32 { self + 1 }
}
fn main() {
let v = 9i32;
println!("{}", v.f());
}
10
Moving the box that holds the trait object compiled and ran in the probed shape, printing 8, in the file sep_move_box_itself.rs - the value that moves there is the pointer, which has a fixed size, and the trait object stays where it was allocated. Receivers written as references are the widest part of the boundary: an &self method called twice on the same Box<dyn Bar> compiled and ran, printing 11 twice, in the file boundary_ref_receiver.rs, and an &mut self method on a mutable box compiled and ran, printing 21, in the file boundary_mut_ref_receiver.rs. A reference receiver never moves the trait object, so the call can repeat - unlike the by-value call, which the error stops before it can consume anything.
Related guides
For a trait object whose size is demanded by a binding, a parameter, or a return type, the neighboring codes are E0277 and E0746. For a by-value trait-object call reached through a borrow or a shared pointer, the code that rides along is E0507. For an unresolved name elsewhere in a program that also carries a by-value trait-object call, the code reported first is E0425.
The repairs
Every repair below starts from the same observation: the call may keep its meaning, but the receiver must stop asking for a move of the unsized value. Either take a reference, the index's repair, or take a sized pointer that owns or shares the value.
Use the index's reference form. This is the index's own repaired example, verbatim: the method takes &self, and the same box call goes through:
trait Bar {
fn f(&self);
}
impl Bar for i32 {
fn f(&self) {}
}
fn main() {
let b: Box<dyn Bar> = Box::new(0i32);
b.f();
// ok!
}
The shape compiled in the probed shape. It produces no program output - the method body is empty - so there is no output to show; the runnable reference version, an &self method returning a value, compiled and ran in the probed shape, printing 11 on each of two calls, in the file boundary_ref_receiver.rs. Where the method needs to change the value, the same move with &mut self compiled and ran, printing 21, in the file boundary_mut_ref_receiver.rs.
Keep the by-value shape with a Box<Self> receiver. Where consuming the value is the point of the method, the receiver can name the box itself - self: Box<Self> is a sized pointer, so the move the call performs is the pointer's move, and the method still takes ownership of the value behind it. The shape compiled and ran in the probed shape, printing 31:
trait Bar {
fn f(self: Box<Self>) -> i32;
}
impl Bar for i32 {
fn f(self: Box<Self>) -> i32 { *self + 1 }
}
fn main() {
let b: Box<dyn Bar> = Box::new(30i32);
println!("{}", b.f());
}
31
Keep the by-value shape with an Rc<Self> receiver. Where the trait object is shared rather than owned outright, self: Rc<Self> does the same job for the shared pointer: the call moves the Rc, not the unsized value. The shape compiled and ran in the probed shape, printing 41:
use std::rc::Rc;
trait Bar {
fn f(self: Rc<Self>) -> i32;
}
impl Bar for i32 {
fn f(self: Rc<Self>) -> i32 { *self + 1 }
}
fn main() {
let r: Rc<dyn Bar> = Rc::new(40i32);
println!("{}", r.f());
}
41
Callers change with the receiver choice in one visible way: a reference receiver can be called as many times as the borrow allows, while the Box<Self> and Rc<Self> forms are called on the matching pointer type - a value held some other way has to be put behind that pointer first.
Fixes in reach order
First, read the header. "Cannot move a value of type dyn Bar" is E0161, a by-value method call on a trait object. A report that the size of a type cannot be known, naming Sized, is E0277, and a return type that cannot be a trait object is E0746 - both are signature and binding problems, repaired where they are written, not at a call.
Second, if the method does not need to consume the value, change the receiver to &self, or to &mut self when it mutates - the index's repair. That clears every E0161 on that method at once, and any E0507 that rode along on a borrowed call clears with it.
Third, if the method does need to consume the value, change the receiver to self: Box<Self> and hold the trait objects in boxes at the call sites, or to self: Rc<Self> where the objects are shared. The by-value meaning survives; only the thing that moves changes.
Fourth, if the report is unnumbered and says the method cannot be invoked on a trait object, a Sized bound is doing that on purpose: leave it if trait-object calls were never intended, or remove the bound and apply the receiver repairs if they were.
Fifth, if the output carries a second code, separate it before judging the program: an E0507 on the same span as an E0161 is the same defect stated twice and clears with the receiver change, while an E0425 for an unresolved name is reported first and hides the E0161 until the name is fixed.
Decision summary
E0161 fired in the probed shapes for the index example verbatim (exactly one E0161, no other code), and for two by-value trait-object calls in one main (exactly two, one per call). It rode along, in the probed shapes, with E0507 on the same call when the trait object was reached through a shared reference (one E0161, one E0507) and through an Rc (one E0161, one E0507). It did not fire, in the probed shapes, for moving the box itself (compiled and ran, printing 8), for the same by-value method on a concrete value (printing 10), for an &self receiver called twice on one box (printing 11 twice), or for an &mut self receiver (printing 21). The neighboring shapes drew other reports instead in the probed shapes: moving out of a dereferenced trait object, string, or slice, and binding a dereferenced trait object in a match, each drew exactly one E0277 and no E0161; a bare dyn parameter drew E0277 with an E0308; a bare dyn return type drew E0746 with an E0277; a by-value ?Sized generic parameter drew E0277; a method with a Sized bound called on a trait object drew one unnumbered cannot-be-invoked report; and an unresolved name in the same main as the index call drew exactly one E0425 alone, masking the E0161 in that shape. The repairs that compiled and ran in the probed shapes were the index's reference receiver verbatim (compiled; the method body is empty, so no output), an &self receiver returning a value (printing 11 twice), an &mut self receiver (printing 21), a Box<Self> receiver (printing 31), and an Rc<Self> receiver (printing 41).
Checklist
- The test is the move, in the probed shapes: E0161 is a by-value method call on a trait object - direct moves of unsized values into bindings are E0277, not E0161.
- The count is per call, in the probed shape: two by-value trait-object calls in one
maindrew two reports. - The handle decides the ride-along, in the probed shapes: an owned box drew E0161 alone, while a shared reference and an
Rceach added an E0507 on the same call. - The value itself is never the problem, in the probed shapes: moving the box, and calling the method on a concrete value, both compiled and ran.
- A
Sizedbound is a separate, unnumbered report, in the probed shape: the method cannot be invoked on a trait object at all, by design. - The receiver is the repair, in the probed shapes:
&self,&mut self,Box<Self>, andRc<Self>receivers all compiled, and the runnable ones ran.
Sources
- Rust error codes index, E0161 - a value was moved whose size was not known at compile time; the erroneous example calls a by-value
selfmethod on aBox<dyn Bar>, and the workaround hides the value behind a reference,&xor&mut x, since a reference has a fixed size. - Rust error codes index, E0277 - a type was used that does not implement a trait a place expected, including
Sized; the neighboring code drawn in the probed shapes by direct moves of unsized values into bindings, by a baredynparameter, and by a by-value?Sizedgeneric parameter. - Rust error codes index, E0507 - a borrowed value was moved out; the neighboring code drawn in the probed shapes alongside E0161 when a by-value trait-object call is reached through a shared reference or an
Rc. - Rust error codes index, E0746 - an unboxed trait object was used as a return value; the neighboring code drawn in the probed shape by a bare
dynreturn type, with the index's own options ofimpl Traitfor one type or a boxed trait object for several. - Rust error codes index, E0425 - an unresolved name was used; the neighboring code drawn in the probed shape by an unresolved name in the same
mainas the index call, reported alone and masking the E0161 in that shape.
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.