我想实现一个类似于标准库定义的debug builders的构建器.它们是使用如下 struct 定义的:
struct DebugFoo<'a, 'b: 'a> {
fmt: &'a mut std::fmt::Formatter<'b>
}
因为我不明白表格<'a, 'b: 'a>
的意思,也找不到铁 rust 书或铁 rust 参考文献中提到的表格(至少是关于生命周期 的),我只是试图删除我不明白的内容,看看会发生什么:
struct DebugFoo<'a, 'b> {
fmt: &'a mut std::fmt::Formatter<'b>
}
编译它时,我会遇到以下错误:
in type `&'a mut core::fmt::Formatter<'b>`, reference has a longer
lifetime than the data it references
这张便条:
the pointer is valid for the lifetime 'a as defined on the struct at 1:0
but the referenced data is only valid for the lifetime 'b as defined on
the struct at 1:0
这对我来说很有意义:'a
和'b
是不同的生命周期,所以,为了安全起见,Rust (借书判断?)假设'a
比'b
长寿,并抛出错误.
现在我可以猜<'a, 'b: 'a>
意味着'b
的生命周期 必须比'a
的生命周期 长.我猜对了?还是还有更多?我怎样才能找到文件?