]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0161.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0161.md
1 A value was moved whose size was not known at compile time.
2
3 Erroneous code example:
4
5 ```compile_fail,E0161
6 #![feature(box_syntax)]
7 trait Bar {
8     fn f(self);
9 }
10
11 impl Bar for i32 {
12     fn f(self) {}
13 }
14
15 fn main() {
16     let b: Box<dyn Bar> = box (0 as i32);
17     b.f();
18     // error: cannot move a value of type dyn Bar: the size of dyn Bar cannot
19     //        be statically determined
20 }
21 ```
22
23 In Rust, you can only move a value when its size is known at compile time.
24
25 To work around this restriction, consider "hiding" the value behind a reference:
26 either `&x` or `&mut x`. Since a reference has a fixed size, this lets you move
27 it around as usual. Example:
28
29 ```
30 #![feature(box_syntax)]
31
32 trait Bar {
33     fn f(&self);
34 }
35
36 impl Bar for i32 {
37     fn f(&self) {}
38 }
39
40 fn main() {
41     let b: Box<dyn Bar> = box (0 as i32);
42     b.f();
43     // ok!
44 }
45 ```