5 ms·
Elision never assumes 'static, since that's an unusual case that has very few real-world uses. This leaves the only remaining possibility that references in th
by pornel 6y ago
Elision never assumes 'static, since that's an unusual case that has very few real-world uses.
This leaves the only remaining possibility that references in the return type must have the same lifetime as references in function's arguments. Without a GC, or leaking memory, you can't make a Rust reference/lifetime out of thin air, so there's always such relationship.
When a function takes multiple arguments by reference, the compiler will ask to disambiguate which one is used for the return type. Borrow checker intentionally checks borrowing against function prototypes, not function bodies.
- garmaine 6y agoI believe ‘static is assumed if there are no inputs.
- Manishearth 6y agoNo, not quite, the signature will be `fn foo<'a>() -> &'a u8` (there is no way to elide this) However, Rust lifetimes are stretchy-squeezy, and a free lifetime is basically the same as 'static when it comes to covariant lifetimes, since both are "I can be whatever you want me to be". However if the lifetime is invariant (e.g. in https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=33cfb6dfde60bb44e36a71b461e66724 https://play.rust-lang.org/?version=stable&mode=debug&editio...), it is actually not 'static and will map to whatever lifetime is requested by the context.
- brundolf 6y agoWibbly-wobbly timey-wimey? :)
- josalhor 6y agoThat's not the case. Proof: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0fed983276f36448315d36ec48d0f823 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- IshKebab 6y ago> Elision never assumes 'static I'm pretty sure boxed trait objects (maybe all boxed objects?) are assumed to be 'static. That is, Box<dyn MyTrait> is shorthand for Box<dyn MyTrait<'static>). If you want non-static lifetimes you have to do something like Box<dyn MyTrait<'_> + '_> (I only just learnt this so take it with a pinch of salt.)
- estebank 6y agoYou might be interested to know that the + '_ is inferred from the lifetime of the trait bound it comes from. By default if no lifetime is involved what will be inferred is 'static (namely, an owned thing). This means that if you write Box<dyn My trait<'_>> that's enough. If MyTrait doesn't have a lifetime parameter then you might have to write Box<dyn MyTrait + '_> to "flip the inference" from the default, but that only comes up in practice on return types, particularly with impl Trait. TL;DR: Trait<'a> is actually understood as Trait<'a> + 'a.
- IshKebab 6y agoI don't think so. Try removing the `+ '_` from this code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=97692026a1d749b0e3fba760a2b1759a https://play.rust-lang.org/?version=stable&mode=debug&editio...
- estebank 6y agoOh, you're right. Clearly I'd forgotten the rule. `'a` is not inferred for `dyn Trait<'a>`, it is inferred for `&'a dyn Trait`[1]. [1]: https://github.com/rust-lang/rfcs/blob/master/text/0599-default-object-bound.md https://github.com/rust-lang/rfcs/blob/master/text/0599-defa...
- IshKebab 6y agoAha! Thanks for the link. Why isn't this in the official documentation anywhere? Also I'm going to point people here next time someone tries to claim that Rust is simple.