有没有一种方法可以从Rust编译器获取淘汰的生存期参数? [英] Is there a way to obtain elided lifetime parameters from the Rust compiler?
问题描述
给出一个正确编译的Rust程序,我能否让编译器告诉我推断的寿命减少是什么?
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.
在可以选择的情况下,编译器不接受有效期.方法中是一个例外,但是将所有生存期都绑定到self
几乎总是所要实现的,因此进行此假设是有意义的.
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]如果Rust的未来版本对淘汰寿命进行了更复杂的推断,那么这个问题的答案可能就不那么重要了.例如,编译器可以分析整个代码库,以得出所有功能(或impl
s或struct
s,如果也允许省略的话)的一组连贯的生存期.
[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).
这篇关于有没有一种方法可以从Rust编译器获取淘汰的生存期参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!