1 #### Note: this error code is no longer emitted by the compiler.
3 Return type involving a trait did not require `'static` lifetime.
5 Erroneous code examples:
10 fn foo(x: &i32) -> impl Debug { // error!
14 fn bar(x: &i32) -> Box<dyn Debug> { // error!
19 Add `'static` requirement to fix them:
22 # use std::fmt::Debug;
23 fn foo(x: &'static i32) -> impl Debug + 'static { // ok!
27 fn bar(x: &'static i32) -> Box<dyn Debug + 'static> { // ok!
32 Both [`dyn Trait`] and [`impl Trait`] in return types have an implicit
33 `'static` requirement, meaning that the value implementing them that is being
34 returned has to be either a `'static` borrow or an owned value.
36 In order to change the requirement from `'static` to be a lifetime derived from
37 its arguments, you can add an explicit bound, either to an anonymous lifetime
38 `'_` or some appropriate named lifetime.
41 # use std::fmt::Debug;
42 fn foo(x: &i32) -> impl Debug + '_ {
45 fn bar(x: &i32) -> Box<dyn Debug + '_> {
50 These are equivalent to the following explicit lifetime annotations:
53 # use std::fmt::Debug;
54 fn foo<'a>(x: &'a i32) -> impl Debug + 'a {
57 fn bar<'a>(x: &'a i32) -> Box<dyn Debug + 'a> {
62 [`dyn Trait`]: https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types
63 [`impl Trait`]: https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits