问题
Given a Rust program, which compiles correctly, can I get the compiler to tell me what the elided lifetimes were inferred to be?
回答1:
The cases where the compiler (currently1) can allow elided lifetimes are actually so simple that there isn't much the compiler could tell you about what it inferred:
Given a function, all elided lifetimes have the same value.
The compiler doesn't accept elided lifetimes in cases where it would have a choice to make. The exception is in methods, but tying all lifetimes to self
is nearly always what is intended, so it makes sense for it to make this assumption.
[1] If a future version of Rust performed more sophisticated inference on elided lifetimes, then this question might have a far less trivial answer. For example the compiler could analyse the entire codebase to deduce a coherent set of lifetimes for all functions (or impl
s or struct
s if elision was permitted there too).
来源:https://stackoverflow.com/questions/34704612/is-there-a-way-to-obtain-elided-lifetime-parameters-from-the-rust-compiler