]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0746.md
Fix error index test
[rust.git] / src / librustc_error_codes / error_codes / E0746.md
1 Return types cannot be `dyn Trait`s as they must be `Sized`.
2
3 Erroneous code example:
4
5 ```compile_fail,E0277
6 # // FIXME: after E0746 is in beta, change the above
7 trait T {
8     fn bar(&self);
9 }
10 struct S(usize);
11 impl T for S {
12     fn bar(&self) {}
13 }
14
15 // Having the trait `T` as return type is invalid because bare traits do not
16 // have a statically known size:
17 fn foo() -> dyn T {
18     S(42)
19 }
20 ```
21
22 To avoid the error there are a couple of options.
23
24 If there is a single type involved, you can use [`impl Trait`]:
25
26 ```
27 # trait T {
28 #     fn bar(&self);
29 # }
30 # struct S(usize);
31 # impl T for S {
32 #     fn bar(&self) {}
33 # }
34 // The compiler will select `S(usize)` as the materialized return type of this
35 // function, but callers will only be able to access associated items from `T`.
36 fn foo() -> impl T {
37     S(42)
38 }
39 ```
40
41 If there are multiple types involved, the only way you care to interact with
42 them is through the trait's interface and having to rely on dynamic dispatch is
43 acceptable, then you can use [trait objects] with `Box`, or other container
44 types like `Rc` or `Arc`:
45
46 ```
47 # trait T {
48 #     fn bar(&self);
49 # }
50 # struct S(usize);
51 # impl T for S {
52 #     fn bar(&self) {}
53 # }
54 struct O(&'static str);
55 impl T for O {
56     fn bar(&self) {}
57 }
58
59 // This now returns a "trait object" and callers are only be able to access
60 // associated items from `T`.
61 fn foo(x: bool) -> Box<dyn T> {
62     if x {
63         Box::new(S(42))
64     } else {
65         Box::new(O("val"))
66     }
67 }
68 ```
69
70 Finally, if you wish to still be able to access the original type, you can
71 create a new `enum` with a variant for each type:
72
73 ```
74 # trait T {
75 #     fn bar(&self);
76 # }
77 # struct S(usize);
78 # impl T for S {
79 #     fn bar(&self) {}
80 # }
81 # struct O(&'static str);
82 # impl T for O {
83 #     fn bar(&self) {}
84 # }
85 enum E {
86     S(S),
87     O(O),
88 }
89
90 // The caller can access the original types directly, but it needs to match on
91 // the returned `enum E`.
92 fn foo(x: bool) -> E {
93     if x {
94         E::S(S(42))
95     } else {
96         E::O(O("val"))
97     }
98 }
99 ```
100
101 You can even implement the `trait` on the returned `enum` so the callers
102 *don't* have to match on the returned value to invoke the associated items:
103
104 ```
105 # trait T {
106 #     fn bar(&self);
107 # }
108 # struct S(usize);
109 # impl T for S {
110 #     fn bar(&self) {}
111 # }
112 # struct O(&'static str);
113 # impl T for O {
114 #     fn bar(&self) {}
115 # }
116 # enum E {
117 #     S(S),
118 #     O(O),
119 # }
120 impl T for E {
121     fn bar(&self) {
122         match self {
123             E::S(s) => s.bar(),
124             E::O(o) => o.bar(),
125         }
126     }
127 }
128 ```
129
130 If you decide to use trait objects, be aware that these rely on
131 [dynamic dispatch], which has performance implications, as the compiler needs
132 to emit code that will figure out which method to call *at runtime* instead of
133 during compilation. Using trait objects we are trading flexibility for
134 performance.
135
136 [`impl Trait`]: https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits
137 [trait objects]: https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types
138 [dynamic dispatch]: https://doc.rust-lang.org/book/ch17-02-trait-objects.html#trait-objects-perform-dynamic-dispatch