#![allow(unused)]fnmain() {
// The following examples show situations where it is not allowed to elide the// lifetime parameter.traitExample {
// Cannot infer, because there are no parameters to infer from.fnget_str() -> &str; // ILLEGAL// Cannot infer, ambiguous if it is borrowed from the first or second parameter.fnfrob(s: &str, t: &str) -> &str; // ILLEGAL}
}
#![allow(unused)]fnmain() {
// For the following trait...traitFoo { }
// These two are the same because Box<T> has no lifetime bound on TtypeT1 = Box<dyn Foo>;
typeT2 = Box<dyn Foo + 'static>;
// ...and so are these:impldyn Foo {}
impldyn Foo + 'static {}
// ...so are these, because &'a T requires T: 'atypeT3<'a> = &'adyn Foo;
typeT4<'a> = &'a (dyn Foo + 'a);
// std::cell::Ref<'a, T> also requires T: 'a, so these are the sametypeT5<'a> = std::cell::Ref<'a, dyn Foo>;
typeT6<'a> = std::cell::Ref<'a, dyn Foo + 'a>;
}
#![allow(unused)]fnmain() {
// This is an example of an error.traitFoo { }
structTwoBounds<'a, 'b, T: ?Sized + 'a + 'b> {
f1: &'ai32,
f2: &'bi32,
f3: T,
}
typeT7<'a, 'b> = TwoBounds<'a, 'b, dyn Foo>;
// ^^^^^^^// Error: the lifetime bound for this object type cannot be deduced from context}
#![allow(unused)]fnmain() {
// For the following trait...traitBar<'a>: 'a { }
// ...these two are the same:typeT1<'a> = Box<dyn Bar<'a>>;
typeT2<'a> = Box<dyn Bar<'a> + 'a>;
// ...and so are these:impl<'a> dyn Bar<'a> {}
impl<'a> dyn Bar<'a> + 'a {}
}
#![allow(unused)]fnmain() {
structFoo;
structBar;
structBaz;
fnsomefunc<'a,'b>(a: &'a Foo, b: &'b Bar) -> &'a Baz {unimplemented!()}
// There is insufficient information to bound the return reference lifetime// relative to the argument lifetimes, so this is an error.const RESOLVED_STATIC: &dynFn(&Foo, &Bar) -> &Baz = &somefunc;
// ^// this function's return type contains a borrowed value, but the signature// does not say whether it is borrowed from argument 1 or argument 2}