Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upLifetimes should be inferred in simple cases #18620
Comments
This comment has been minimized.
This comment has been minimized.
zwarich
commented
Nov 4, 2014
|
It could also be |
This comment has been minimized.
This comment has been minimized.
|
@zwarich – ok, but could one make a case that if you wanted that then you'd make it explicit? I wouldn't have thought |
This comment has been minimized.
This comment has been minimized.
zwarich
commented
Nov 4, 2014
|
@mhart There's no default choice here. |
This comment has been minimized.
This comment has been minimized.
|
@zwarich Can you explain that a little more? |
This comment has been minimized.
This comment has been minimized.
|
As @zwarich said, there's no default choice that's right enough of the time. For example, you could require the lifetime of the string to be bound to the lifetime on a struct (I have done this in the past). In general, even the current rules for lifetime inference are not always right, and they are quite conservative. I'm not sure how you could do it properly in a case like this without it becoming confusing. |
This comment has been minimized.
This comment has been minimized.
|
Can you show me what you mean with being bound to the lifetime on a struct in this example? There's no struct involved, so I'm a bit confused. Saying "I have done this in the past" actually makes it sound as though it's a rare thing to do still. In my mind it's analogous to having default immutability for a variable vs the times when you need to explicitly declare it as mutable. I'm struggling to understand the actual problem with assuming the lifetime in the example given. Is there some way that things would become unsafe that the compiler wouldn't catch? This actually started from the IRC channel when I posed the question and got this:
So I'm not alone in thinking this is something that should be done. Can someone talk through the problems that would occur (that the compiler wouldn't catch) if it were to assume the lifetimes in the example I gave? I just worry that simple things like this will turn off newcomers – and if they can be fixed, then it will really help the adoption of the language. |
steveklabnik
referenced this issue
Jan 29, 2015
Closed
Lifetimes should be inferred in simple cases #762
This comment has been minimized.
This comment has been minimized.
|
I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized. This issue has been moved to the RFCs repo: rust-lang/rfcs#762 |
mhart commentedNov 4, 2014
Rust newbie here. This is very probably a dupe, but my GH foo failed me.
The following fails to compile:
With:
But it's unclear to me why the compiler can't just infer the lifetime here?
Shouldn't this be the default choice?
Edit: So it could also be
'static, but I wouldn't have thought that would be the default (or encouraged) case. If you did want it to be static, then you could declare it explicitly. Similar to how variables are immutable by default (without needing an explicit declaration), yet if you want them to be mutable you declare it.