-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow lifetime polymorphism in local bindings #1378
Labels
T-lang
Relevant to the language team, which will review and decide on the RFC.
Comments
The following works on master: fn main() {
fn id_type<T: Fn(&i32) -> &i32>(t: T) -> T { t }
let id = id_type(|x: &i32| x);
{
let a: Box<i32> = Box::new(42);
id(&a);
}
{
let b: Box<i32> = Box::new(43);
id(&b);
}
} I see two issues: one, rustc is terrible at inferring lifetimes for closures (rust-lang/rust#28092 rust-lang/rust#26937 etc.). Two, it would be nice to have more direct syntax for explicitly specifying the lifetimes you want. |
nrc
added
the
T-lang
Relevant to the language team, which will review and decide on the RFC.
label
Aug 19, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following example doesn't compile in current Rust:
This is because
a
andb
have non-overlapping lifetimes, soid
cannot be fixed to either of them.The typical solution is to lift
id
into afn
, making the lifetime parameter polymorphic:But then we lose the ability to close over the environment.
Ideally, I'd like the compiler to accept something like this:
that is, a closure which is polymorphic over the lifetime rather than fixed to a particular one.
The text was updated successfully, but these errors were encountered: